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

The following examples show how to use org.springframework.security.oauth2.client.endpoint.OAuth2ClientCredentialsGrantRequest. 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: EurekaOAuth2ClientFilterAdapter.java    From spring-cloud-services-starters with Apache License 2.0 5 votes vote down vote up
@Override
public ClientResponse handle(ClientRequest cr) throws ClientHandlerException {
	Instant now = Clock.systemUTC().instant();
	if (accessToken == null || now.isAfter(accessToken.getExpiresAt())) {
		DefaultClientCredentialsTokenResponseClient tokenResponseClient = new DefaultClientCredentialsTokenResponseClient();
		OAuth2ClientCredentialsGrantRequest clientCredentialsGrantRequest = new OAuth2ClientCredentialsGrantRequest(
				clientRegistration);
		accessToken = tokenResponseClient.getTokenResponse(clientCredentialsGrantRequest).getAccessToken();
	}

	cr.getHeaders().add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken.getTokenValue());

	return getNext().handle(cr);
}
 
Example #2
Source File: OAuth2AuthorizedClientHttpRequestInterceptor.java    From spring-cloud-services-starters with Apache License 2.0 5 votes vote down vote up
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
		throws IOException {
	Instant now = Clock.systemUTC().instant();
	if (accessToken == null || now.isAfter(accessToken.getExpiresAt())) {
		DefaultClientCredentialsTokenResponseClient tokenResponseClient = new DefaultClientCredentialsTokenResponseClient();
		OAuth2ClientCredentialsGrantRequest clientCredentialsGrantRequest = new OAuth2ClientCredentialsGrantRequest(
				clientRegistration);
		accessToken = tokenResponseClient.getTokenResponse(clientCredentialsGrantRequest).getAccessToken();
	}

	request.getHeaders().add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken.getTokenValue());
	return execution.execute(request, body);
}
 
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();
}