org.springframework.security.oauth2.client.OAuth2AuthorizedClientService Java Examples

The following examples show how to use org.springframework.security.oauth2.client.OAuth2AuthorizedClientService. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Oauth2ClientApplication.java    From training with Apache License 2.0 7 votes vote down vote up
@Bean
RestTemplate restTemplate(OAuth2AuthorizedClientService clientService) {
		return new RestTemplateBuilder()
			.interceptors((ClientHttpRequestInterceptor) (httpRequest, bytes, execution) -> {

					OAuth2AuthenticationToken token = OAuth2AuthenticationToken.class.cast(
						SecurityContextHolder.getContext().getAuthentication());

					OAuth2AuthorizedClient client = clientService.loadAuthorizedClient(
						token.getAuthorizedClientRegistrationId(),
						token.getName());

					httpRequest.getHeaders().add(HttpHeaders.AUTHORIZATION, "Bearer " + client.getAccessToken().getTokenValue());

					return execution.execute(httpRequest, bytes);
			})
			.build();
}
 
Example #2
Source File: DataFlowClientAutoConfiguration.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
private ClientHttpRequestInterceptor bearerTokenResolvingInterceptor(
		OAuth2ClientProperties properties, String username, String password, String clientRegistrationId) {
	ClientRegistrationRepository shellClientRegistrationRepository = shellClientRegistrationRepository(properties);
	OAuth2AuthorizedClientService shellAuthorizedClientService = shellAuthorizedClientService(shellClientRegistrationRepository);
	OAuth2AuthorizedClientManager authorizedClientManager = authorizedClientManager(
			shellClientRegistrationRepository, shellAuthorizedClientService);

	if (properties.getRegistration() != null && properties.getRegistration().size() == 1) {
		// if we have only one, use that
		clientRegistrationId = properties.getRegistration().entrySet().iterator().next().getKey();
	}

	OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId(clientRegistrationId)
			.principal(DEFAULT_PRINCIPAL)
			.attribute(OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME, username)
			.attribute(OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME, password)
			.build();

	return (request, body, execution) -> {
		OAuth2AuthorizedClient authorizedClient = authorizedClientManager.authorize(authorizeRequest);
		request.getHeaders().setBearerAuth(authorizedClient.getAccessToken().getTokenValue());
		return execution.execute(request, body);
	};
}
 
Example #3
Source File: DataFlowClientAutoConfiguration.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
private OAuth2AuthorizedClientManager authorizedClientManager(
		ClientRegistrationRepository shellClientRegistrationRepository,
		OAuth2AuthorizedClientService shellAuthorizedClientService) {
	AuthorizedClientServiceOAuth2AuthorizedClientManager manager = new AuthorizedClientServiceOAuth2AuthorizedClientManager(
		shellClientRegistrationRepository, shellAuthorizedClientService);
	OAuth2AuthorizedClientProvider authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
		.password()
		.refreshToken()
		.build();
	manager.setAuthorizedClientProvider(authorizedClientProvider);
	manager.setContextAttributesMapper(request -> {
		Map<String, Object> contextAttributes = new HashMap<>();
		request.getAttributes().forEach((k, v) -> {
			if (OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME.equals(k)
					|| OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME.equals(k)) {
				contextAttributes.put(k, v);
			}
		});
		return contextAttributes;
	});
	return manager;
}
 
Example #4
Source File: DataFlowClientAutoConfiguration.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
private ClientHttpRequestInterceptor clientCredentialsTokenResolvingInterceptor(
		ClientRegistration clientRegistration, ClientRegistrationRepository clientRegistrationRepository,
		String clientId) {
	Authentication principal = createAuthentication(clientId);
	OAuth2AuthorizedClientService authorizedClientService = new InMemoryOAuth2AuthorizedClientService(
			clientRegistrationRepository);
	AuthorizedClientServiceOAuth2AuthorizedClientManager authorizedClientManager = new AuthorizedClientServiceOAuth2AuthorizedClientManager(
			clientRegistrationRepository, authorizedClientService);
	OAuth2AuthorizedClientProvider authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
			.clientCredentials().build();
	authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);

	OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest
			.withClientRegistrationId(DEFAULT_REGISTRATION_ID).principal(principal).build();

	return (request, body, execution) -> {
		OAuth2AuthorizedClient authorizedClient = authorizedClientManager.authorize(authorizeRequest);
		request.getHeaders().setBearerAuth(authorizedClient.getAccessToken().getTokenValue());
		return execution.execute(request, body);
	};
}
 
Example #5
Source File: ConfigCommands.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
private ClientHttpRequestInterceptor bearerTokenResolvingInterceptor(
		OAuth2ClientProperties properties, String username, String password, String clientRegistrationId) {
	ClientRegistrationRepository shellClientRegistrationRepository = shellClientRegistrationRepository(properties);
	OAuth2AuthorizedClientService shellAuthorizedClientService = shellAuthorizedClientService(shellClientRegistrationRepository);
	OAuth2AuthorizedClientManager authorizedClientManager = authorizedClientManager(
			shellClientRegistrationRepository, shellAuthorizedClientService);

	if (properties.getRegistration() != null && properties.getRegistration().size() == 1) {
		// if we have only one, use that
		clientRegistrationId = properties.getRegistration().entrySet().iterator().next().getKey();
	}

	OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId(clientRegistrationId)
			.principal(DEFAULT_PRINCIPAL)
			.attribute(OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME, username)
			.attribute(OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME, password)
			.build();

	return (request, body, execution) -> {
		OAuth2AuthorizedClient authorizedClient = authorizedClientManager.authorize(authorizeRequest);
		request.getHeaders().setBearerAuth(authorizedClient.getAccessToken().getTokenValue());
		return execution.execute(request, body);
	};
}
 
Example #6
Source File: ConfigCommands.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
private OAuth2AuthorizedClientManager authorizedClientManager(
		ClientRegistrationRepository shellClientRegistrationRepository,
		OAuth2AuthorizedClientService shellAuthorizedClientService) {
	AuthorizedClientServiceOAuth2AuthorizedClientManager manager = new AuthorizedClientServiceOAuth2AuthorizedClientManager(
		shellClientRegistrationRepository, shellAuthorizedClientService);
	OAuth2AuthorizedClientProvider authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
		.password()
		.refreshToken()
		.build();
	manager.setAuthorizedClientProvider(authorizedClientProvider);
	manager.setContextAttributesMapper(request -> {
		Map<String, Object> contextAttributes = new HashMap<>();
		request.getAttributes().forEach((k, v) -> {
			if (OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME.equals(k)
					|| OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME.equals(k)) {
				contextAttributes.put(k, v);
			}
		});
		return contextAttributes;
	});
	return manager;
}
 
Example #7
Source File: Oauth2ClientApplication.java    From training with Apache License 2.0 5 votes vote down vote up
@Bean
@RequestScope
OAuth2AuthorizedClient authorizedClient(
		OAuth2AuthorizedClientService authorizedClientService,
		OAuth2AuthenticationToken oauthToken) {
	return authorizedClientService.loadAuthorizedClient(
			oauthToken.getAuthorizedClientRegistrationId(),
			oauthToken.getName());
}
 
Example #8
Source File: CredHubSecurityConfiguration.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
@Bean
public AuthorizedClientServiceOAuth2AuthorizedClientManager reactiveClientManager(
		ClientRegistrationRepository clientRegistrationRepository,
		OAuth2AuthorizedClientService authorizedClientService) {
	AuthorizedClientServiceOAuth2AuthorizedClientManager clientManager = new AuthorizedClientServiceOAuth2AuthorizedClientManager(
			clientRegistrationRepository, authorizedClientService);
	clientManager.setAuthorizedClientProvider(new ClientCredentialsOAuth2AuthorizedClientProvider());
	return clientManager;
}
 
Example #9
Source File: CredHubTemplateAutoConfigurationTests.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
@Bean
AuthorizedClientServiceOAuth2AuthorizedClientManager clientManager(OAuth2ClientProperties properties) {
	List<ClientRegistration> registrations = new ArrayList<>(
			OAuth2ClientPropertiesRegistrationAdapter.getClientRegistrations(properties).values());
	ClientRegistrationRepository clientRegistrationRepository = new InMemoryClientRegistrationRepository(
			registrations);
	OAuth2AuthorizedClientService authorizedClientService = new InMemoryOAuth2AuthorizedClientService(
			clientRegistrationRepository);
	return new AuthorizedClientServiceOAuth2AuthorizedClientManager(clientRegistrationRepository,
			authorizedClientService);
}
 
Example #10
Source File: UaaAuthorizationHeaderUtil.java    From jhipster-registry with Apache License 2.0 5 votes vote down vote up
public UaaAuthorizationHeaderUtil(ClientRegistrationRepository clientRegistrationRepository,
                                  OAuth2AuthorizedClientService clientRegistrationService,
                                  RestTemplate uaaRestTemplate) {
    this.uaaRestTemplate = uaaRestTemplate;
    this.clientRegistrationRepository = clientRegistrationRepository;
    this.clientRegistrationService = clientRegistrationService;
}
 
Example #11
Source File: TestSecurityConfiguration.java    From jhipster-registry with Apache License 2.0 4 votes vote down vote up
@Bean
public OAuth2AuthorizedClientRepository authorizedClientRepository(OAuth2AuthorizedClientService authorizedClientService) {
    return new AuthenticatedPrincipalOAuth2AuthorizedClientRepository(authorizedClientService);
}
 
Example #12
Source File: MainController.java    From okta-spring-security-5-example with Apache License 2.0 4 votes vote down vote up
public MainController(OAuth2AuthorizedClientService authorizedClientService) {
    this.authorizedClientService = authorizedClientService;
}
 
Example #13
Source File: AuthorizationHeaderUtil.java    From jhipster-registry with Apache License 2.0 4 votes vote down vote up
public AuthorizationHeaderUtil(OAuth2AuthorizedClientService clientService, RestTemplateBuilder restTemplateBuilder) {
    this.clientService = clientService;
    this.restTemplateBuilder = restTemplateBuilder;
}
 
Example #14
Source File: UaaTestSecurityConfiguration.java    From jhipster-registry with Apache License 2.0 4 votes vote down vote up
@Bean
public OAuth2AuthorizedClientService authorizedClientService(ClientRegistrationRepository clientRegistrationRepository) {
    return new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository);
}
 
Example #15
Source File: TestSecurityConfiguration.java    From jhipster-registry with Apache License 2.0 4 votes vote down vote up
@Bean
public OAuth2AuthorizedClientService authorizedClientService(ClientRegistrationRepository clientRegistrationRepository) {
    return new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository);
}
 
Example #16
Source File: CFUAAOAuth2ClientController.java    From tutorials with MIT License 4 votes vote down vote up
public CFUAAOAuth2ClientController(OAuth2AuthorizedClientService authorizedClientService) {
    this.authorizedClientService = authorizedClientService;
    this.restTemplate = new RestTemplate();
}
 
Example #17
Source File: ConfigCommands.java    From spring-cloud-dataflow with Apache License 2.0 4 votes vote down vote up
private OAuth2AuthorizedClientService shellAuthorizedClientService(ClientRegistrationRepository shellClientRegistrationRepository) {
	return new InMemoryOAuth2AuthorizedClientService(shellClientRegistrationRepository);
}
 
Example #18
Source File: DataFlowClientAutoConfiguration.java    From spring-cloud-dataflow with Apache License 2.0 4 votes vote down vote up
private OAuth2AuthorizedClientService shellAuthorizedClientService(ClientRegistrationRepository shellClientRegistrationRepository) {
	return new InMemoryOAuth2AuthorizedClientService(shellClientRegistrationRepository);
}
 
Example #19
Source File: UndertowSpringSecurityAutoConfiguration.java    From camel-spring-boot with Apache License 2.0 4 votes vote down vote up
@Bean
public OAuth2AuthorizedClientService authorizedClientService(ClientRegistrationRepository repository) {
    return new InMemoryOAuth2AuthorizedClientService(repository);
}
 
Example #20
Source File: DemoApplicationTests.java    From keycloak-springsecurity5-sample with GNU General Public License v3.0 4 votes vote down vote up
@Bean
public OAuth2AuthorizedClientService authorizedClientService() {
	return new InMemoryOAuth2AuthorizedClientService(this.clientRegistrationRepository);
}
 
Example #21
Source File: Oauth2ClientApplication.java    From training with Apache License 2.0 4 votes vote down vote up
ProfileRestController(RestTemplate restTemplate, OAuth2AuthorizedClientService clientService) {
		this.restTemplate = restTemplate;
		this.clientService = clientService;
}
 
Example #22
Source File: DefaultControllerAdvice.java    From oauth2-protocol-patterns with Apache License 2.0 4 votes vote down vote up
@Autowired(required = false)
void setAuthorizedClientService(OAuth2AuthorizedClientService authorizedClientService) {
	this.authorizedClientService = authorizedClientService;
}
 
Example #23
Source File: ApiGatewayApplication.java    From java-microservices-examples with Apache License 2.0 4 votes vote down vote up
@Bean
public AuthorizationHeaderFilter authHeaderFilter(OAuth2AuthorizedClientService clientService) {
    return new AuthorizationHeaderFilter(clientService);
}
 
Example #24
Source File: ApiGatewayApplication.java    From java-microservices-examples with Apache License 2.0 4 votes vote down vote up
@Bean
public RequestInterceptor getUserFeignClientInterceptor(OAuth2AuthorizedClientService clientService) {
    return new UserFeignClientInterceptor(clientService);
}
 
Example #25
Source File: AuthorizationHeaderFilter.java    From java-microservices-examples with Apache License 2.0 4 votes vote down vote up
public AuthorizationHeaderFilter(OAuth2AuthorizedClientService clientService) {
    this.clientService = clientService;
}
 
Example #26
Source File: UserFeignClientInterceptor.java    From java-microservices-examples with Apache License 2.0 4 votes vote down vote up
public UserFeignClientInterceptor(OAuth2AuthorizedClientService clientService) {
    this.clientService = clientService;
}
 
Example #27
Source File: TestSecurityConfiguration.java    From java-microservices-examples with Apache License 2.0 4 votes vote down vote up
@Bean
public OAuth2AuthorizedClientRepository authorizedClientRepository(OAuth2AuthorizedClientService authorizedClientService) {
    return new AuthenticatedPrincipalOAuth2AuthorizedClientRepository(authorizedClientService);
}
 
Example #28
Source File: TestSecurityConfiguration.java    From java-microservices-examples with Apache License 2.0 4 votes vote down vote up
@Bean
public OAuth2AuthorizedClientService authorizedClientService(ClientRegistrationRepository clientRegistrationRepository) {
    return new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository);
}
 
Example #29
Source File: AuthorizationHeaderUtil.java    From java-microservices-examples with Apache License 2.0 4 votes vote down vote up
public AuthorizationHeaderUtil(OAuth2AuthorizedClientService clientService) {
    this.clientService = clientService;
}
 
Example #30
Source File: TestSecurityConfiguration.java    From java-microservices-examples with Apache License 2.0 4 votes vote down vote up
@Bean
public OAuth2AuthorizedClientRepository authorizedClientRepository(OAuth2AuthorizedClientService authorizedClientService) {
    return new AuthenticatedPrincipalOAuth2AuthorizedClientRepository(authorizedClientService);
}