Java tutorial
/** * Copyright(c)2015 IntelCorporation * * LicensedundertheApacheLicense,Version2.0(the"License"); * youmaynotusethisfileexceptincompliancewiththeLicense. * YoumayobtainacopyoftheLicenseat * * http://www.apache.org/licenses/LICENSE-2.0 * * Unlessrequiredbyapplicablelaworagreedtoinwriting,software * distributedundertheLicenseisdistributedonan"ASIS"BASIS, * WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied. * SeetheLicenseforthespecificlanguagegoverningpermissionsand * limitationsundertheLicense. */ package org.trustedanalytics.user.common; import static org.springframework.context.annotation.ScopedProxyMode.TARGET_CLASS; import static org.springframework.web.context.WebApplicationContext.SCOPE_REQUEST; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.trustedanalytics.cloud.auth.AuthTokenRetriever; import org.trustedanalytics.cloud.auth.OAuth2TokenRetriever; import org.trustedanalytics.user.current.AuthDetailsFinder; import org.trustedanalytics.user.current.UserDetailsFinder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.Scope; import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext; import org.springframework.security.oauth2.client.OAuth2ClientContext; import org.springframework.security.oauth2.client.OAuth2RestTemplate; import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails; import org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest; import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider; import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails; import org.springframework.web.client.RestTemplate; import java.util.List; @Profile("cloud") @Configuration public class RestTemplatesConfiguration { @Value("#{'${smtp.forbidden_domains}'.split(',')}") private List<String> forbiddenDomains; @Bean public OAuth2ClientContext oauth2ClientContext() { return new DefaultOAuth2ClientContext(new DefaultAccessTokenRequest()); } @Bean @Scope(value = SCOPE_REQUEST, proxyMode = TARGET_CLASS) protected RestTemplate userRestTemplate() { HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); return new RestTemplate(factory); } @Bean @ConfigurationProperties("spring.oauth2.client") public OAuth2ProtectedResourceDetails clientCredentials() { return new ClientCredentialsResourceDetails(); } @Bean public OAuth2RestTemplate clientRestTemplate(OAuth2ClientContext oauth2ClientContext, OAuth2ProtectedResourceDetails clientCredentials) { OAuth2RestTemplate template = new OAuth2RestTemplate(clientCredentials, oauth2ClientContext); ClientCredentialsAccessTokenProvider provider = new ClientCredentialsAccessTokenProvider(); template.setAccessTokenProvider(provider); return template; } @Bean protected AuthTokenRetriever tokenRetriever() { return new OAuth2TokenRetriever(); } @Bean protected UserDetailsFinder detailsFinder() { return new AuthDetailsFinder(); } @Bean protected UserPasswordValidator userPasswordValidator() { return new UserPasswordValidator(); } @Bean protected BlacklistEmailValidator emailValidator() { return new BlacklistEmailValidator(forbiddenDomains); } }