1

私のSpring Bootアプリケーションでは、Spring OAuth2サーバーにJWTトークンを設定しました。SpringブートOAuth2は手動で新しいJWTトークンを作成します

また、Twitter、Facebookなどのさまざまなソーシャルネットワークを介してユーザーを認証できるようにSpring Socialの設定を追加しました。

これは私のSpringSocial configです。また

@Configuration 
@EnableSocial 
public class SocialConfig extends SocialConfigurerAdapter { 

    @Bean 
    public ProviderSignInController providerSignInController(ConnectionFactoryLocator connectionFactoryLocator, UsersConnectionRepository usersConnectionRepository) { 
     return new ProviderSignInController(connectionFactoryLocator, usersConnectionRepository, new SimpleSignInAdapter(authTokenServices, "client_id", userService)); 
    } 

... 

} 

、私は3rdpartyソーシャルネットワークで成功認証を処理するためにSimpleSignInAdapterを実装しているIntegrate Spring Security OAuth2 and Spring Social folksingingの回答に基づいて:

public class SimpleSignInAdapter implements SignInAdapter { 

    final static Logger logger = LoggerFactory.getLogger(SimpleSignInAdapter.class); 

    public static final String REDIRECT_PATH_BASE = "/#/login"; 
    public static final String FIELD_TOKEN = "access_token"; 
    public static final String FIELD_EXPIRATION_SECS = "expires_in"; 

    private final AuthorizationServerTokenServices authTokenServices; 
    private final String localClientId; 
    private final UserService userService; 

    public SimpleSignInAdapter(AuthorizationServerTokenServices authTokenServices, String localClientId, UserService userService){ 
     this.authTokenServices = authTokenServices; 
     this.localClientId = localClientId; 
     this.userService = userService; 
    } 

    @Override 
    public String signIn(String userId, Connection<?> connection, NativeWebRequest request) { 

     UserDetails userDetails = loadUserById(Long.parseLong(userId)); 

     OAuth2AccessToken oauth2Token = authTokenServices.createAccessToken(convertAuthentication(userDetails)); 
     String redirectUrl = new StringBuilder(REDIRECT_PATH_BASE) 
      .append("?").append(FIELD_TOKEN).append("=") 
      .append(encode(oauth2Token.getValue())) 
      .append("&").append(FIELD_EXPIRATION_SECS).append("=") 
      .append(oauth2Token.getExpiresIn()) 
      .toString();  

     return redirectUrl; 
    } 

    private OAuth2Authentication convertAuthentication(UserDetails userDetails) { 
     OAuth2Request request = new OAuth2Request(null, localClientId, null, true, null, null, null, null, null); 
     return new OAuth2Authentication(request, new UsernamePasswordAuthenticationToken(userDetails, "N/A", userDetails.getAuthorities())); 
    } 

    private String encode(String in) { 
     String res = in; 
     try { 
      res = UriUtils.encode(in, "UTF-8"); 
     } catch(UnsupportedEncodingException e){ 
      logger.error("ERROR: unsupported encoding: " + "UTF-8", e); 
     } 
     return res; 
    } 

    public UserDetails loadUserById(Long id) throws UsernameNotFoundException { 
     User user = userService.findUserById(id); 
     if (user == null) { 
      throw new UsernameNotFoundException("User " + id + " not found."); 
     } 

     Set<Permission> permissions = userService.getUserPermissions(user); 
     return new DBUserDetails(user, permissions); 
    } 

} 

すべてが正常に動作します1つのことを除いて - 次のコード行は、プレーンなOAuth2アクセストークンを生成します:

OAuth2AccessToken oauth2Token = authTokenServices.createAccessToken(convertAuthentication(userDetails)); 

しかし、代わりにJWTトークンを作成する必要があります。

このトークンを作成してJWTに変換する方法は?私はこの目的のためにJwtAccessTokenConverterクラスを使用することができますが、この時点ではどうなっているのか分かりません。

答えて

1

をデバッグした後、私は解決策を見つけた:私は私自身のカスタムJWTトークンを望んでいた後

private final TokenEnhancer tokenEnhancer; 

... 
OAuth2Authentication authentication = convertAuthentication(userDetails); 
OAuth2AccessToken accessToken = authTokenServices.createAccessToken(authentication); 
accessToken = tokenEnhancer.enhance(accessToken, authentication); 
0

これは、私のために働きました。

DefaultTokenServices service = new DefaultTokenServices(); 
    service.setTokenStore(jwtAccessTokenConverter); 
    service.setTokenEnhancer(jwtAccessTokenConverter); 
    OAuth2AccessToken token = service.createAccessToken(authentication); 

Autowire jwtAccessTokenConverter

@Autowired 
private JwtAccessTokenConverter jwtAccessTokenConverter; 
関連する問題