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

The following examples show how to use org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider. 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: CredHubRestTemplateFactory.java    From spring-credhub with Apache License 2.0 6 votes vote down vote up
private static OAuth2AuthorizedClientManager buildClientManager(
		ClientRegistrationRepository clientRegistrationRepository,
		OAuth2AuthorizedClientRepository authorizedClientRepository,
		ClientHttpRequestFactory clientHttpRequestFactory) {

	OAuth2AuthorizedClientProvider authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
			.authorizationCode().clientCredentials(
					(b) -> b.accessTokenResponseClient(buildTokenResponseClient(clientHttpRequestFactory)))
			.build();

	DefaultOAuth2AuthorizedClientManager authorizedClientManager = new DefaultOAuth2AuthorizedClientManager(
			clientRegistrationRepository, authorizedClientRepository);
	authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);

	return authorizedClientManager;
}
 
Example #2
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 #3
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 #4
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;
}