Spring OAuth2 - Manually creating an access token in the token store

后端 未结 7 934
天涯浪人
天涯浪人 2020-12-07 17:00

I have a situation where I would like to create an access token myself (so not through the usual process). I have come up with something like this:

@Inject
p         


        
7条回答
  •  -上瘾入骨i
    2020-12-07 17:53

    In a spring boot 2.2.2 project I'm using the following code to do a pasword flow server side: I had to specify authorizedClientManager.setContextAttributesMapper since PasswordOAuth2AuthorizedClientProvider is expecting specific attributes in the context. Hope that helps.

    Config (application.yaml):

    spring:
      security:
        oauth2:
          client:
            provider:
              yourOauthProvider:
                user-info-uri: ...
                authorization-uri: ...
                token-uri: ...
    
            registration:
              regId:
                clientId: ...
                clientSecret: ...
                provider: yourOauthProvider
                authorization-grant-type: password
                redirect-uri-template: "{baseUrl}/login/oauth2/code/{registrationId}"
                scope:
    

    Wiring:

    @Configuration
    public class Oauth2ClientConfig {
    
        @Bean
        public OAuth2AuthorizedClientManager authorizedClientManager(
                ClientRegistrationRepository clientRegistrationRepository,
                OAuth2AuthorizedClientRepository authorizedClientRepository) {
    
            OAuth2AuthorizedClientProvider authorizedClientProvider =
                    OAuth2AuthorizedClientProviderBuilder.builder()
                            .password()
                            .build();
    
            DefaultOAuth2AuthorizedClientManager authorizedClientManager =
                    new DefaultOAuth2AuthorizedClientManager(
                            clientRegistrationRepository, authorizedClientRepository);
            authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
            authorizedClientManager.setContextAttributesMapper(r -> {
                Map m = new HashMap<>();
                m.put(OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME, r.getPrincipal().getPrincipal());
                m.put(OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME, r.getPrincipal().getCredentials());
                return m;
            });
    
            return authorizedClientManager;
        }
    }
    

    Service:

    class AuthService {
        @Autowired
        private OAuth2AuthorizedClientManager authorizedClientManager;
        public OAuth2AccessToken authenticate(String user, String password) {
    
            Authentication principal = new UsernamePasswordAuthenticationToken(
                    user,
                    password);
    
            OAuth2AuthorizeRequest authorizeRequest = 
                OAuth2AuthorizeRequest.withClientRegistrationId("regId")
                    .principal(principal)
                    .build();
    
            OAuth2AuthorizedClient authorizedClient =
                this.authorizedClientManager.authorize(authorizeRequest);
    
            return authorizedClient.getAccessToken();
        }
    }
    

提交回复
热议问题