加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 02:14:00 所属栏目:Java 来源:网络整理
导读:我有两个 Spring应用程序:身份验证服务和业务服务. 当Web服务用户在Authentication Service上进行身份验证时,他会获得access_token和refresh_token.他可以通过将refresh_token发送到服务来刷新他的access_token.该服务实现AuthenticationProvider,其中设置
我有两个 Spring应用程序:身份验证服务和业务服务.

当Web服务用户在Authentication Service上进行身份验证时,他会获得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上进行身份验证并调用业务服务,则detailsMap将包含authenticate()中设置的信息.但是,如果他刷新令牌并再次调用业务服务,则detailsMap将为null.

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

解决方法

作为一种解决方法,我们不再使用细节,而是将其数据保存到UserDetails实现UserDetailsImplementation中.

在的AuthenticationProvider实现的方法验证身份验证(验证认证),我们返回UsernamePasswordAuthenticationToken,其主要设置为UserDetailsImplementation.此UserDetailsImplementation也在UserDetailsS??ervice实现中返回,该实现在刷新令牌时调用.

在业务服务中,我们可以访问所需的数据

((UserDetailsImplementation) authentication.getPrincipal()).getDesiredData();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读