[英]Spring Security: AnonymousAuthenticationToken vs UsernamePasswordAuthenticationToken
[英]Spring Security JWT validation without using UsernamePasswordAuthenticationToken
我需要編寫自定義 Security Spring 過濾器來驗證 JWT 令牌。
我有我的自定義 JWT 驗證器。 當令牌有效時,我必須授權用戶。
我像其他一些在線展示的示例一樣實現了這一點:
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(user_id, null, null);
usernamePasswordAuthenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
這工作正常,但我的問題是:JWT 驗證是否有專用的 AuthenticationToken 而不使用 UsernamePasswordAuthenticationToken?
我不喜歡使用 UsernamePasswordAuthenticationToken ......這是一個好習慣嗎?
當然,從干凈代碼的角度來看,它看起來並不好,因為它的名稱UsernamePasswordAuthenticationToken
與 object 的名稱不匹配。 JWT 與密碼無關。 就像如果您使用名稱橙色來指代蘋果一樣,也會令人困惑。
JWT 通常用作 Bearer 令牌, spring-security-oauth2-resource-server
提供了BearerTokenAuthenticationToken
。 但它會將所有其他 OAuth2 依賴項提取到您的項目中。 因此,如果您不需要 OAuth2 而只需要這個令牌 class,只需自己創建一個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.