【问题标题】:Spring, Oauth2: authentication details lost after refreshing the tokenSpring,Oauth2:刷新令牌后身份验证详细信息丢失
【发布时间】:2016-07-18 11:38:39
【问题描述】:

我有两个 Spring 应用程序:一个 Authentication Service 和一个 Business Service

当一个 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;
}

Business ServiceOauth2 保护。它的控制器包含

@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。我怎样才能做到这一点?

【问题讨论】:

    标签: java spring spring-security spring-security-oauth2


    【解决方案1】:

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

    AuthenticationProvider 实现的方法Authentication authenticate(Authentication authentication) 中,我们返回一个UsernamePasswordAuthenticationToken,其principal 设置为UserDetailsImplementation。这个UserDetailsImplementation 也在UserDetailsService 实现中返回,该实现在令牌刷新时被调用。

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

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

    【讨论】:

    • 我面临着类似的问题。但是,我不想刷新令牌,但我想在用户登录时在令牌中设置详细信息。但 OAuth2Authentication#setDetails 显然什么也没做,我设置的详细信息被覆盖。你能帮忙吗?
    • @PranjalGore:请打开一个新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2011-05-23
    • 2017-11-08
    • 2020-01-16
    • 2015-01-08
    • 2021-09-06
    相关资源
    最近更新 更多