簡體   English   中英

Spring,Oauth2:刷新令牌后丟失了身份驗證詳細信息

[英]Spring, Oauth2: authentication details lost after refreshing the token

我有兩個Spring應用程序: 身份驗證服務業務服務

當Web服務用戶在Authentication Service上進行身份驗證時 ,他會獲得access_tokenrefresh_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 我怎樣才能做到這一點?

作為一種解決方法,我們不再使用details ,而是將其數據保存到UserDetails實現UserDetailsImplementation

AuthenticationProvider實現的Authentication authenticate(Authentication authentication)中,我們返回一個UsernamePasswordAuthenticationTokenprincipal設置為UserDetailsImplementation UserDetailsImplementation也在UserDetailsService實現中返回,該實現在刷新令牌時調用。

業務服務中,我們可以訪問所需的數據

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM