org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient Java Examples

The following examples show how to use org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient. 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: SecurityConfig.java    From messaging-app with Apache License 2.0 5 votes vote down vote up
private OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> authorizationCodeTokenResponseClient() {
	OAuth2AccessTokenResponseHttpMessageConverter tokenResponseHttpMessageConverter =
			new OAuth2AccessTokenResponseHttpMessageConverter();
	tokenResponseHttpMessageConverter.setTokenResponseConverter(new CustomAccessTokenResponseConverter());

	RestTemplate restTemplate = new RestTemplate(Arrays.asList(
			new FormHttpMessageConverter(), tokenResponseHttpMessageConverter));
	restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());

	DefaultAuthorizationCodeTokenResponseClient tokenResponseClient = new DefaultAuthorizationCodeTokenResponseClient();
	tokenResponseClient.setRestOperations(restTemplate);

	return tokenResponseClient;
}
 
Example #2
Source File: DemoApplicationTests.java    From keycloak-springsecurity5-sample with GNU General Public License v3.0 5 votes vote down vote up
private OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> mockAccessTokenResponseClient() {
	OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("access-token-1234")
		.tokenType(OAuth2AccessToken.TokenType.BEARER)
		.expiresIn(60 * 1000)
		.build();

	OAuth2AccessTokenResponseClient tokenResponseClient = mock(OAuth2AccessTokenResponseClient.class);
	when(tokenResponseClient.getTokenResponse(any())).thenReturn(accessTokenResponse);
	return tokenResponseClient;
}
 
Example #3
Source File: DataFlowConfiguration.java    From composed-task-runner with Apache License 2.0 4 votes vote down vote up
/**
 * @param clientRegistrations Can be null. Only required for Client Credentials Grant authentication
 * @param clientCredentialsTokenResponseClient Can be null. Only required for Client Credentials Grant authentication
 * @return DataFlowOperations
 */
@Bean
public DataFlowOperations dataFlowOperations(
	@Autowired(required = false) ClientRegistrationRepository  clientRegistrations,
	@Autowired(required = false) OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsTokenResponseClient) {

	final RestTemplate restTemplate = DataFlowTemplate.getDefaultDataflowRestTemplate();
	validateUsernamePassword(this.properties.getDataflowServerUsername(), this.properties.getDataflowServerPassword());

	HttpClientConfigurer clientHttpRequestFactoryBuilder = null;

	if (this.properties.getOauth2ClientCredentialsClientId() != null
			|| StringUtils.hasText(this.properties.getDataflowServerAccessToken())
			|| (StringUtils.hasText(this.properties.getDataflowServerUsername())
					&& StringUtils.hasText(this.properties.getDataflowServerPassword()))) {
		clientHttpRequestFactoryBuilder = HttpClientConfigurer.create(this.properties.getDataflowServerUri());
	}

	String accessTokenValue = null;

	if (this.properties.getOauth2ClientCredentialsClientId() != null) {
		final ClientRegistration clientRegistration = clientRegistrations.findByRegistrationId("default");
		final OAuth2ClientCredentialsGrantRequest grantRequest = new OAuth2ClientCredentialsGrantRequest(clientRegistration);
		final OAuth2AccessTokenResponse res = clientCredentialsTokenResponseClient.getTokenResponse(grantRequest);
		accessTokenValue = res.getAccessToken().getTokenValue();
		logger.debug("Configured OAuth2 Client Credentials for accessing the Data Flow Server");
	}
	else if (StringUtils.hasText(this.properties.getDataflowServerAccessToken())) {
		accessTokenValue = this.properties.getDataflowServerAccessToken();
		logger.debug("Configured OAuth2 Access Token for accessing the Data Flow Server");
	}
	else if (StringUtils.hasText(this.properties.getDataflowServerUsername())
			&& StringUtils.hasText(this.properties.getDataflowServerPassword())) {
		accessTokenValue = null;
		clientHttpRequestFactoryBuilder.basicAuthCredentials(properties.getDataflowServerUsername(), properties.getDataflowServerPassword());
		logger.debug("Configured basic security for accessing the Data Flow Server");
	}
	else {
		logger.debug("Not configuring basic security for accessing the Data Flow Server");
	}

	if (accessTokenValue != null) {
		restTemplate.getInterceptors().add(new OAuth2AccessTokenProvidingClientHttpRequestInterceptor(accessTokenValue));
	}

	if (clientHttpRequestFactoryBuilder != null) {
		restTemplate.setRequestFactory(clientHttpRequestFactoryBuilder.buildClientHttpRequestFactory());
	}

	return new DataFlowTemplate(this.properties.getDataflowServerUri(), restTemplate);
}
 
Example #4
Source File: DataFlowConfiguration.java    From composed-task-runner with Apache License 2.0 4 votes vote down vote up
@Bean
OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsTokenResponseClient() {
	return new DefaultClientCredentialsTokenResponseClient();
}
 
Example #5
Source File: CredHubRestTemplateFactory.java    From spring-credhub with Apache License 2.0 4 votes vote down vote up
private static OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> buildTokenResponseClient(
		ClientHttpRequestFactory clientHttpRequestFactory) {
	DefaultClientCredentialsTokenResponseClient tokenResponseClient = new DefaultClientCredentialsTokenResponseClient();
	tokenResponseClient.setRestOperations(createTokenServerRestTemplate(clientHttpRequestFactory));
	return tokenResponseClient;
}
 
Example #6
Source File: DataFlowClientAutoConfiguration.java    From spring-cloud-dataflow with Apache License 2.0 4 votes vote down vote up
@Bean
OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsTokenResponseClient() {
	return new DefaultClientCredentialsTokenResponseClient();
}
 
Example #7
Source File: DataFlowConfiguration.java    From spring-cloud-dataflow with Apache License 2.0 4 votes vote down vote up
/**
 * @param clientRegistrations Can be null. Only required for Client Credentials Grant authentication
 * @param clientCredentialsTokenResponseClient Can be null. Only required for Client Credentials Grant authentication
 * @return DataFlowOperations
 */
@Bean
public DataFlowOperations dataFlowOperations(
	@Autowired(required = false) ClientRegistrationRepository clientRegistrations,
	@Autowired(required = false) OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsTokenResponseClient) {

	final RestTemplate restTemplate = DataFlowTemplate.getDefaultDataflowRestTemplate();
	validateUsernamePassword(this.properties.getDataflowServerUsername(), this.properties.getDataflowServerPassword());

	HttpClientConfigurer clientHttpRequestFactoryBuilder = null;

	if (this.properties.getOauth2ClientCredentialsClientId() != null
			|| StringUtils.hasText(this.properties.getDataflowServerAccessToken())
			|| (StringUtils.hasText(this.properties.getDataflowServerUsername())
					&& StringUtils.hasText(this.properties.getDataflowServerPassword()))) {
		clientHttpRequestFactoryBuilder = HttpClientConfigurer.create(this.properties.getDataflowServerUri());
	}

	String accessTokenValue = null;

	if (this.properties.getOauth2ClientCredentialsClientId() != null) {
		final ClientRegistration clientRegistration = clientRegistrations.findByRegistrationId("default");
		final OAuth2ClientCredentialsGrantRequest grantRequest = new OAuth2ClientCredentialsGrantRequest(clientRegistration);
		final OAuth2AccessTokenResponse res = clientCredentialsTokenResponseClient.getTokenResponse(grantRequest);
		accessTokenValue = res.getAccessToken().getTokenValue();
		logger.debug("Configured OAuth2 Client Credentials for accessing the Data Flow Server");
	}
	else if (StringUtils.hasText(this.properties.getDataflowServerAccessToken())) {
		accessTokenValue = this.properties.getDataflowServerAccessToken();
		logger.debug("Configured OAuth2 Access Token for accessing the Data Flow Server");
	}
	else if (StringUtils.hasText(this.properties.getDataflowServerUsername())
			&& StringUtils.hasText(this.properties.getDataflowServerPassword())) {
		accessTokenValue = null;
		clientHttpRequestFactoryBuilder.basicAuthCredentials(properties.getDataflowServerUsername(), properties.getDataflowServerPassword());
		logger.debug("Configured basic security for accessing the Data Flow Server");
	}
	else {
		logger.debug("Not configuring basic security for accessing the Data Flow Server");
	}

	if (accessTokenValue != null) {
		restTemplate.getInterceptors().add(new OAuth2AccessTokenProvidingClientHttpRequestInterceptor(accessTokenValue));
	}

	if (clientHttpRequestFactoryBuilder != null) {
		restTemplate.setRequestFactory(clientHttpRequestFactoryBuilder.buildClientHttpRequestFactory());
	}

	return new DataFlowTemplate(this.properties.getDataflowServerUri(), restTemplate);
}
 
Example #8
Source File: DataFlowConfiguration.java    From spring-cloud-dataflow with Apache License 2.0 4 votes vote down vote up
@Bean
OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsTokenResponseClient() {
	return new DefaultClientCredentialsTokenResponseClient();
}
 
Example #9
Source File: JwtBearerOAuth2AuthorizedClientProvider.java    From oauth2-protocol-patterns with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the client used when requesting an access token credential at the Token Endpoint for the {@code jwt-bearer} grant.
 *
 * @param accessTokenResponseClient the client used when requesting an access token credential at the Token Endpoint for the {@code jwt-bearer} grant
 */
public void setAccessTokenResponseClient(OAuth2AccessTokenResponseClient<JwtBearerGrantRequest> accessTokenResponseClient) {
	Assert.notNull(accessTokenResponseClient, "accessTokenResponseClient cannot be null");
	this.accessTokenResponseClient = accessTokenResponseClient;
}