Search in sources :

Example 16 with OAuth2RestTemplate

use of org.springframework.security.oauth2.client.OAuth2RestTemplate in project spring-boot by spring-projects.

the class UserInfoTokenServices method getMap.

@SuppressWarnings({ "unchecked" })
private Map<String, Object> getMap(String path, String accessToken) {
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("Getting user info from: " + path);
    }
    try {
        OAuth2RestOperations restTemplate = this.restTemplate;
        if (restTemplate == null) {
            BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails();
            resource.setClientId(this.clientId);
            restTemplate = new OAuth2RestTemplate(resource);
        }
        OAuth2AccessToken existingToken = restTemplate.getOAuth2ClientContext().getAccessToken();
        if (existingToken == null || !accessToken.equals(existingToken.getValue())) {
            DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(accessToken);
            token.setTokenType(this.tokenType);
            restTemplate.getOAuth2ClientContext().setAccessToken(token);
        }
        return restTemplate.getForEntity(path, Map.class).getBody();
    } catch (Exception ex) {
        this.logger.warn("Could not fetch user details: " + ex.getClass() + ", " + ex.getMessage());
        return Collections.<String, Object>singletonMap("error", "Could not fetch user details");
    }
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) BaseOAuth2ProtectedResourceDetails(org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails) OAuth2RestOperations(org.springframework.security.oauth2.client.OAuth2RestOperations) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate) Map(java.util.Map) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) AuthenticationException(org.springframework.security.core.AuthenticationException) InvalidTokenException(org.springframework.security.oauth2.common.exceptions.InvalidTokenException)

Example 17 with OAuth2RestTemplate

use of org.springframework.security.oauth2.client.OAuth2RestTemplate in project spring-security-oauth by spring-projects.

the class ClientApplication method restTemplate.

@Bean
@Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)
public OAuth2RestOperations restTemplate() {
    OAuth2RestTemplate template = new OAuth2RestTemplate(resource(), new DefaultOAuth2ClientContext(accessTokenRequest));
    AccessTokenProviderChain provider = new AccessTokenProviderChain(Arrays.asList(new AuthorizationCodeAccessTokenProvider()));
    provider.setClientTokenServices(clientTokenServices());
    return template;
}
Also used : DefaultOAuth2ClientContext(org.springframework.security.oauth2.client.DefaultOAuth2ClientContext) AuthorizationCodeAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider) AccessTokenProviderChain(org.springframework.security.oauth2.client.token.AccessTokenProviderChain) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate) Scope(org.springframework.context.annotation.Scope) Bean(org.springframework.context.annotation.Bean)

Aggregations

OAuth2RestTemplate (org.springframework.security.oauth2.client.OAuth2RestTemplate)13 Test (org.junit.Test)7 DefaultOAuth2ClientContext (org.springframework.security.oauth2.client.DefaultOAuth2ClientContext)7 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)4 AuthorizationCodeAccessTokenProvider (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider)4 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)4 OAuth2ClientContext (org.springframework.security.oauth2.client.OAuth2ClientContext)3 OAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails)3 DefaultAccessTokenRequest (org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest)3 ResourceOwnerPasswordResourceDetails (org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails)3 Date (java.util.Date)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Before (org.junit.Before)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 Bean (org.springframework.context.annotation.Bean)2 ResponseEntity (org.springframework.http.ResponseEntity)2 BaseOAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails)2 UserApprovalRequiredException (org.springframework.security.oauth2.client.resource.UserApprovalRequiredException)2 AccessTokenRequest (org.springframework.security.oauth2.client.token.AccessTokenRequest)2 AuthorizationCodeResourceDetails (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails)2