[英]Spring, Oauth2: authentication details lost after refreshing the token
我有兩個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
。 我怎樣才能做到這一點?
作為一種解決方法,我們不再使用details
,而是將其數據保存到UserDetails
實現UserDetailsImplementation
。
在AuthenticationProvider
實現的Authentication authenticate(Authentication authentication)
中,我們返回一個UsernamePasswordAuthenticationToken
其principal
設置為UserDetailsImplementation
。 此UserDetailsImplementation
也在UserDetailsService
實現中返回,該實現在刷新令牌時調用。
在業務服務中,我們可以訪問所需的數據
((UserDetailsImplementation) authentication.getPrincipal()).getDesiredData();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.