복붙노트

[SPRING] Spring OAuth2 + JWT 추가 정보를 포함하여 액세스 토큰에 JUST

SPRING

Spring OAuth2 + JWT 추가 정보를 포함하여 액세스 토큰에 JUST

본인의 TokenEnhancer를 구현하는 액세스 토큰에 추가 정보를 포함시킬 수 있지만 이러한 정보는 두 번 포함됩니다. 하나는 인코딩 된 access_token에 있고 다른 하나는 인증 서버 응답에 있습니다.

긴 이야기가 짧다! 올바른 자격 증명으로 액세스 토큰을 요청하면 다음과 같은 응답이 나타납니다.

{
    "access_token" : "eyJhbGciOiJSUzI1NiJ9.eyJjbGllbnRJZCI6Ik1ZX0NVU1RPTV9JTkZPX0NMSUVOVCIsInVzZXJfbmFtZSI6IlVTRVIiLCJzY29wZSI6WyJGT08iXSwiZXhwIjoxNTA2MzkwOTM5LCJhdXRob3JpdGllcyI6WyJST0xFX1VTRVIiXSwianRpIjoiZjJkYWFkM2ItYzkzOC00ZjExLWI3ODctMzExZDdlNjYzYzhhIiwiY2xpZW50X2lkIjoid2ViX2FwcCJ9.IdgYRxwZGRPR97nxHpAcJXNWDTShQE1tsg9NsBwlOk8eDWE1B-mjfGTaKiyTO1-m9GBpXnxt2PaOV7AbdLsCZ5xLPUR0_5ehuNB6WCXLSkdac5xbw-rmNdJHTe9gLJizOZAKF6J-_Xo9OOQISKBqliY5vo5y0btqIw4CX6-ukYoWZmwHThwnAsEA_PqGuEXsbXMGz-vqJaSVpvJeEOBNL0KOh-cNxc0ft-rJ3snjPerN_efAiZdFkzxdCeuoGmZvSyHRjYR8kQ3ZqZ5MOunw9YuTvidL1IK5TODHQ2BjiCTpbgDlYx-Oh5UxcYNrPOhD-tBjRuuqDSz8K6ddpke4RQ",
    "token_type" : "bearer",
    "refresh_token" : "eyJhbGciOiJSUzI1NiJ9.eyJjbGllbnRJZCI6Ik1ZX0NVU1RPTV9JTkZPX0NMSUVOVCIsInVzZXJfbmFtZSI6IlVTRVIiLCJzY29wZSI6WyJGT08iXSwiYXRpIjoiZjJkYWFkM2ItYzkzOC00ZjExLWI3ODctMzExZDdlNjYzYzhhIiwiZXhwIjoxNTA4OTM5NzM5LCJhdXRob3JpdGllcyI6WyJST0xFX1VTRVIiXSwianRpIjoiOGU2Zjc0OTEtMmQ3MC00NTUwLThhMDgtZjk0YjkzYTVkYWZmIiwiY2xpZW50X2lkIjoid2ViX2FwcCJ9.MqwMrYrofu7pUQu2mF33__h6M4OWSRrQ-lc8JzTn0DkpJ6a3-yjnjjppZ9fs3KBz_lpRIO8jo--eId449rEjP4M3_9lDRSW9_HyBAvd57OtyUHa5SPM9prD6ReXGCyiIw2gO07euIf-Vp4UHsjoKK0MdtfMmFIWms1JMGFBmzBha8kqKaMxKzppGy-jVdP7384K9oovD20H-NubjScfoO2Crp1cTM-SXc-0v6kwB1qV-cI6HKXmbkoFhbH2bL_nRvXTkLYI-UvRNTNLHzqhcqztLTrszcWa2BjNU2IofsNByFS8BHTDV1vu0BqZA4kfNCJcFJ89tBDt2L8vfFkYezQ",
    "expires_in" : 43199,
    "scope" : "FOO",
    "clientId" : "MY_CUSTOM_INFO_CLIENT",
    "jti" : "f2daad3b-c938-4f11-b787-311d7e663c8a"
}

응답에 clientId가 포함되어있는 것을 볼 수 있습니다 ... 이제 access_token을 복사하고 https://jwt.io/

그리고 페이로드에는 clientId도 포함됩니다 ...

내 질문은 : 서버 응답에서 추가 정보를 제거하고 토큰 (access_token 및 refresh_token)에 그대로 두는 방법은 무엇입니까?

아래 코드를 참조하십시오 :

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;

@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
        tokenEnhancerChain.setTokenEnhancers(
                  Arrays.asList(tokenEnhancer(), accessTokenConverter()));
        endpoints
                .tokenStore(tokenStore())
                .authenticationManager(authenticationManager)
                .tokenEnhancer(tokenEnhancerChain);
    }
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

       clients.inMemory()
           .withClient("web_app")
           .secret("web_app123")
           .scopes("FOO")
           .autoApprove(true)
           .authorities("FOO_READ", "FOO_WRITE")
           .authorizedGrantTypes("refresh_token", "password");
 }
    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(accessTokenConverter());
    }

    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        KeyStoreKeyFactory keyStoreKeyFactory = 
          new KeyStoreKeyFactory(new ClassPathResource("mykey.jks"), "mykey123".toCharArray());
        converter.setKeyPair(keyStoreKeyFactory.getKeyPair("mykey"));
        return converter;
    }

    @Bean
    @Primary
    public DefaultTokenServices tokenServices() {
        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
        defaultTokenServices.setTokenStore(tokenStore());
        defaultTokenServices.setSupportRefreshToken(true);
        return defaultTokenServices;
    }

    @Bean
    public TokenEnhancer tokenEnhancer() {
        return new CustomTokenEnhancer();
    }
}

그리고 내 CustomTokenEnhancer :

import java.util.HashMap;
import java.util.Map;

import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import com.mapflow.ms.security.service.UserDetailInfo;

public class CustomTokenEnhancer implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken,
            OAuth2Authentication authentication) {
        UserDetailInfo user = (UserDetailInfo) authentication.getPrincipal();
        final Map<String, Object> additionalInfo = new HashMap<String, Object>();

        additionalInfo.put("clientId", user.getClientId());

        ((DefaultOAuth2AccessToken) accessToken)
                .setAdditionalInformation(additionalInfo);

        return accessToken;
    }
}

해결법

  1. ==============================

    1.잠시 후 나는 그것을 알아 냈습니다. JwtAccessTokenConverter는 TokenEnhaner도 구현합니다. 추가 정보를 포함하여 첫 번째 CustomTokenEnhaner.enhance가 호출됩니다. 그런 다음 JwtAccessTokenConverter.enhance, CustomTokenEnhaner.enhance에 의해 AccessToken을 인코딩하고 응답에 추가 정보를 포함시킵니다. 아이디어는 초기화 DefaultOAuth2AccessToken.additionalInformation은 일단 access_token으로 인코딩됩니다. 해결책은 다음과 같습니다.

    잠시 후 나는 그것을 알아 냈습니다. JwtAccessTokenConverter는 TokenEnhaner도 구현합니다. 추가 정보를 포함하여 첫 번째 CustomTokenEnhaner.enhance가 호출됩니다. 그런 다음 JwtAccessTokenConverter.enhance, CustomTokenEnhaner.enhance에 의해 AccessToken을 인코딩하고 응답에 추가 정보를 포함시킵니다. 아이디어는 초기화 DefaultOAuth2AccessToken.additionalInformation은 일단 access_token으로 인코딩됩니다. 해결책은 다음과 같습니다.

    먼저 CustomTokenEnhancer가 JwtAccessTokenConverter를 확장하고, 향상을 재정의하고, 추가 정보를 첨부하고, 부모로부터 향상을 호출하고 DefaultOAuth2AccessToken.additionalInformation을 초기화하십시오.

    public class CustomTokenConverter extends JwtAccessTokenConverter {
    
        @Override
        public OAuth2AccessToken enhance(OAuth2AccessToken accessToken,
                OAuth2Authentication authentication) {
            if(authentication.getOAuth2Request().getGrantType().equalsIgnoreCase("password")) {
                UserDetailInfo user = (UserDetailInfo) authentication.getPrincipal();
                final Map<String, Object> additionalInfo = new HashMap<String, Object>();
    
                additionalInfo.put("clientId", user.getClientId());
    
                ((DefaultOAuth2AccessToken) accessToken)
                        .setAdditionalInformation(additionalInfo);    
            } 
            accessToken = super.enhance(accessToken, authentication);
            ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(new HashMap<>());
            return accessToken;
        }
    }
    

    마지막 단계는 콩을 삭제하는 것입니다.

    @Bean
        public JwtAccessTokenConverter accessTokenConverter() {
            JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
            KeyStoreKeyFactory keyStoreKeyFactory = 
              new KeyStoreKeyFactory(new ClassPathResource("mykey.jks"), "mykey123".toCharArray());
            converter.setKeyPair(keyStoreKeyFactory.getKeyPair("mykey"));
            return converter;
        }
    

    그리고 CustomTokenEnhancer에 키를 추가하십시오

    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        CustomTokenConverter tokenConverter = new CustomTokenConverter();
        tokenConverter.setSigningKey("PswMapview2017");
        return tokenConverter;
    }
    

    그게 다야.

  2. from https://stackoverflow.com/questions/46406905/spring-oauth-2-jwt-inlcuding-additional-info-just-in-access-token by cc-by-sa and MIT license