org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository Java Examples

The following examples show how to use org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository. 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: CredHubTemplateAutoConfigurationTests.java    From spring-credhub with Apache License 2.0 6 votes vote down vote up
@Test
public void credHubTemplatesConfiguredWithOAuth2() {
	this.context.withPropertyValues("spring.credhub.url=https://localhost",
			"spring.credhub.oauth2.registration-id=credhub-client",

			"spring.security.oauth2.client.registration.credhub-client.provider=uaa",
			"spring.security.oauth2.client.registration.credhub-client.client-id=test-client",
			"spring.security.oauth2.client.registration.credhub-client.client-secret=test-secret",
			"spring.security.oauth2.client.registration.credhub-client.authorization-grant-type=client_credentials",
			"spring.security.oauth2.client.provider.uaa.token-uri=https://example.com/uaa/oauth/token")
			.run((context) -> {
				assertThat(context).hasSingleBean(CredHubTemplate.class);
				assertThat(context).hasSingleBean(ClientRegistrationRepository.class);
				assertThat(context).hasSingleBean(OAuth2AuthorizedClientRepository.class);
				assertThat(context).doesNotHaveBean(OAuth2AuthorizedClientManager.class);
				CredHubTemplate credHubTemplate = context.getBean(CredHubTemplate.class);
				assertThat(credHubTemplate.isUsingOAuth2()).isTrue();

				assertThat(context).hasSingleBean(ReactiveCredHubTemplate.class);
				assertThat(context).hasSingleBean(ReactiveClientRegistrationRepository.class);
				assertThat(context).hasSingleBean(ServerOAuth2AuthorizedClientRepository.class);
				assertThat(context).doesNotHaveBean(ReactiveOAuth2AuthorizedClientManager.class);
				ReactiveCredHubTemplate reactiveCredHubTemplate = context.getBean(ReactiveCredHubTemplate.class);
				assertThat(reactiveCredHubTemplate.isUsingOAuth2()).isTrue();
			});
}
 
Example #2
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 #3
Source File: WebClientConfig.java    From messaging-app with Apache License 2.0 5 votes vote down vote up
@Bean
public WebClient webClient(ClientRegistrationRepository clientRegistrationRepository,
							OAuth2AuthorizedClientRepository authorizedClientRepository) {
	ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2 = new ServletOAuth2AuthorizedClientExchangeFilterFunction(
			clientRegistrationRepository, authorizedClientRepository);
	return WebClient.builder()
			.apply(oauth2.oauth2Configuration())
			.build();
}
 
Example #4
Source File: CredHubTemplateConfiguration.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
/**
 * Create the {@link CredHubTemplate} that the application will use to interact
 * with CredHub.
 * @param credHubProperties {@link CredHubProperties} for CredHub
 * @param clientOptions client connection options
 * @param clientRegistrationRepository a repository of OAuth2 client registrations
 * @param authorizedClientRepository a repository of authorized OAuth2 clients
 * @return the {@link CredHubOperations} bean
 */
@Bean
@ConditionalOnMissingBean
CredHubOperations credHubTemplate(CredHubProperties credHubProperties, ClientOptions clientOptions,
		ClientRegistrationRepository clientRegistrationRepository,
		OAuth2AuthorizedClientRepository authorizedClientRepository) {

	return new CredHubTemplateFactory().credHubTemplate(credHubProperties, clientOptions,
			clientRegistrationRepository, authorizedClientRepository);
}
 
Example #5
Source File: CredHubOAuth2AutoConfiguration.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
/**
 * Create an {@code OAuth2AuthorizedClientRepository} bean for use with an
 * OAuth2-enabled {@code CredHubTemplate}.
 * @param clientRegistrationRepository a {@code ClientRegistrationRepository}
 * @return the {@code OAuth2AuthorizedClientRepository}
 */
@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(name = "javax.servlet.http.HttpServletRequest")
public OAuth2AuthorizedClientRepository credHubAuthorizedClientRepository(
		ClientRegistrationRepository clientRegistrationRepository) {
	return new AuthenticatedPrincipalOAuth2AuthorizedClientRepository(
			new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository));
}
 
Example #6
Source File: CredHubTemplateAutoConfigurationTests.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
@Test
public void credHubTemplatesConfiguredWithOAuth2AndCustomClientManager() {
	this.context.withPropertyValues("spring.credhub.url=https://localhost",
			"spring.credhub.oauth2.registration-id=credhub-client",

			"spring.security.oauth2.client.registration.credhub-client.provider=uaa",
			"spring.security.oauth2.client.registration.credhub-client.client-id=test-client",
			"spring.security.oauth2.client.registration.credhub-client.client-secret=test-secret",
			"spring.security.oauth2.client.registration.credhub-client.authorization-grant-type=client_credentials",
			"spring.security.oauth2.client.provider.uaa.token-uri=https://example.com/uaa/oauth/token")
			.withUserConfiguration(ClientManagerConfiguration.class).run((context) -> {
				assertThat(context).hasSingleBean(CredHubTemplate.class);
				assertThat(context).hasSingleBean(ClientRegistrationRepository.class);
				assertThat(context).hasSingleBean(OAuth2AuthorizedClientRepository.class);
				assertThat(context).hasSingleBean(AuthorizedClientServiceOAuth2AuthorizedClientManager.class);
				CredHubTemplate credHubTemplate = context.getBean(CredHubTemplate.class);
				assertThat(credHubTemplate.isUsingOAuth2()).isTrue();

				assertThat(context).hasSingleBean(ReactiveCredHubTemplate.class);
				assertThat(context).hasSingleBean(ReactiveClientRegistrationRepository.class);
				assertThat(context).hasSingleBean(ServerOAuth2AuthorizedClientRepository.class);
				assertThat(context)
						.hasSingleBean(AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager.class);
				ReactiveCredHubTemplate reactiveCredHubTemplate = context.getBean(ReactiveCredHubTemplate.class);
				assertThat(reactiveCredHubTemplate.isUsingOAuth2()).isTrue();
			});
}
 
Example #7
Source File: CredHubTemplate.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new {@link CredHubTemplate} using the provided connection properties,
 * {@link ClientHttpRequestFactory}, and OAuth2 support.
 * @param properties the CredHub connection properties; must not be {@literal null}
 * @param clientHttpRequestFactory the {@link ClientHttpRequestFactory} to use when
 * creating new connections
 * @param clientRegistrationRepository a repository of OAuth2 client registrations
 * @param authorizedClientRepository a repository of authorized OAuth2 clients
 */
public CredHubTemplate(CredHubProperties properties, ClientHttpRequestFactory clientHttpRequestFactory,
		ClientRegistrationRepository clientRegistrationRepository,
		OAuth2AuthorizedClientRepository authorizedClientRepository) {
	Assert.notNull(properties, "properties must not be null");
	Assert.notNull(clientHttpRequestFactory, "clientHttpRequestFactory must not be null");
	Assert.notNull(clientRegistrationRepository, "clientRegistrationRepository must not be null");

	this.restTemplate = CredHubRestTemplateFactory.createRestTemplate(properties, clientHttpRequestFactory,
			clientRegistrationRepository, authorizedClientRepository);
	this.usingOAuth2 = true;
}
 
Example #8
Source File: CredHubRestTemplateFactory.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
/**
 * Create a {@link RestTemplate} configured for communication with a CredHub server.
 * @param properties the CredHub connection properties
 * @param clientHttpRequestFactory the {@link ClientHttpRequestFactory} to use when
 * creating new connections
 * @param clientRegistrationRepository a repository of OAuth2 client registrations
 * @param authorizedClientRepository a repository of authorized OAuth2 clients
 * @return a configured {@link RestTemplate}
 */
static RestTemplate createRestTemplate(CredHubProperties properties,
		ClientHttpRequestFactory clientHttpRequestFactory,
		ClientRegistrationRepository clientRegistrationRepository,
		OAuth2AuthorizedClientRepository authorizedClientRepository) {
	RestTemplate restTemplate = new RestTemplate();

	configureRestTemplate(restTemplate, properties.getUrl(), clientHttpRequestFactory);
	configureOAuth2(restTemplate, properties.getOauth2().getRegistrationId(), clientRegistrationRepository,
			buildClientManager(clientRegistrationRepository, authorizedClientRepository, clientHttpRequestFactory));

	return restTemplate;
}
 
Example #9
Source File: UiSecurityConfig.java    From spring-security-oauth with MIT License 5 votes vote down vote up
@Bean
WebClient webClient(ClientRegistrationRepository clientRegistrationRepository,
		OAuth2AuthorizedClientRepository authorizedClientRepository) {
	ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2 = new ServletOAuth2AuthorizedClientExchangeFilterFunction(
			clientRegistrationRepository, authorizedClientRepository);
	oauth2.setDefaultOAuth2AuthorizedClient(true);
	return WebClient.builder().apply(oauth2.oauth2Configuration()).build();
}
 
Example #10
Source File: UiSecurityConfig.java    From spring-security-oauth with MIT License 5 votes vote down vote up
@Bean
WebClient webClient(ClientRegistrationRepository clientRegistrationRepository,
		OAuth2AuthorizedClientRepository authorizedClientRepository) {
	ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2 = new ServletOAuth2AuthorizedClientExchangeFilterFunction(
			clientRegistrationRepository, authorizedClientRepository);
	oauth2.setDefaultOAuth2AuthorizedClient(true);
	return WebClient.builder().apply(oauth2.oauth2Configuration()).build();
}
 
Example #11
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 #12
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 #13
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 #14
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 #15
Source File: CredHubTemplateFactory.java    From spring-credhub with Apache License 2.0 3 votes vote down vote up
/**
 * Create a {@link CredHubTemplate} for interaction with a CredHub server using OAuth2
 * for authentication.
 * @param credHubProperties connection properties
 * @param clientOptions connection options
 * @param clientRegistrationRepository a repository of OAuth2 client registrations
 * @param authorizedClientRepository a repository of authorized OAuth2 clients
 * @return a {@code CredHubTemplate}
 */
public CredHubTemplate credHubTemplate(CredHubProperties credHubProperties, ClientOptions clientOptions,
		ClientRegistrationRepository clientRegistrationRepository,
		OAuth2AuthorizedClientRepository authorizedClientRepository) {
	return new CredHubTemplate(credHubProperties, clientHttpRequestFactory(clientOptions),
			clientRegistrationRepository, authorizedClientRepository);
}