Spring,Oauth2:刷新令牌后身份验证详细信息丢失

Spring, Oauth2: authentication details lost after refreshing the token

我有两个 Spring 应用程序:一个 身份验证服务 和一个 业务服务 .

当网络服务用户在 身份验证服务 进行身份验证时,他会得到一个 access_token 和一个 refresh_token。他可以通过向服务发送 refresh_token 来刷新他的 access_token。该服务实现 AuthenticationProvider,其中设置了身份验证的详细信息:

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException { 
    UsernamePasswordAuthenticationToken newAuthentication = ...;
    LinkedHashMap<String, Object> detailsMap = (LinkedHashMap<String, Object>) authentication.getDetails();
    detailsMap.put(...);
    newAuthentication.setDetails(detailsMap);
    return newAuthentication;
}

业务服务Oauth2 保护。它的控制器包含

@Secured({ SOME_ROLE })
@RequestMapping(...)
public ResponseEntity<?> doSomething(OAuth2Authentication authentication) {
    LinkedHashMap<String, String> detailsMap = (LinkedHashMap<String, String>) authentication
            .getUserAuthentication().getDetails();

如果 web 服务用户在 Authentication Service 进行身份验证并调用 Business ServicedetailsMap 将包含信息集在 authenticate()。但是如果他刷新令牌并再次调用 业务服务 detailsMap 将是 null.

我希望在令牌刷新后保留 detailsMap。我怎样才能做到这一点?

作为解决方法,我们不再使用 details,而是将他们的数据保存到 UserDetails 实现中 UserDetailsImplementation

AuthenticationProvider 实现的方法 Authentication authenticate(Authentication authentication) 中,我们 return 一个 UsernamePasswordAuthenticationTokenprincipal 设置为 UserDetailsImplementation。此 UserDetailsImplementation 也在 UserDetailsService 实现中 return 编辑,该实现在刷新令牌时调用。

业务服务中,我们可以通过

访问所需的数据
((UserDetailsImplementation) authentication.getPrincipal()).getDesiredData();