用戶登錄運行良好,但我想向專案添加一個客戶模塊。我知道我需要撰寫一個自定義 UserDetails 類來獲取客戶用戶名,但我想問一下我是否需要為客戶登錄驗證撰寫另一個自定義 JWT 過濾器。目前這是我用于用戶登錄的過濾器類。我已向 Customer 物體添加了用戶名和密碼欄位。
@Component
public class JwtRequestFilter extends OncePerRequestFilter {
@Autowired
private JwtTokenUtil jwtTokenUtil;
@Autowired
private UserAccountService myUserDetailsService;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws ServletException, IOException {
final String requestTokenHeader = request.getHeader("Authorization");
String username = null;
String jwtToken = null;
if (requestTokenHeader != null) {
jwtToken = requestTokenHeader.substring(7);
try {
username = jwtTokenUtil.getUsernameFromToken(jwtToken);
} catch (IllegalArgumentException e) {
System.out.println("Unable to get JWT Token");
} catch (ExpiredJwtException e) {
System.out.println("JWT Token has expired");
}
}
if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
UserDetails userDetails = this.myUserDetailsService.loadUserByUsername(username);
if (jwtTokenUtil.validateToken(jwtToken, userDetails)) {
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(
userDetails, null, userDetails.getAuthorities());
String authorities = userDetails.getAuthorities().stream().map(GrantedAuthority::getAuthority)
.collect(Collectors.joining());
System.out.println("Authorities granted : " authorities);
usernamePasswordAuthenticationToken
.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
}
else {
System.out.println("Not Valid Token");
}
}
chain.doFilter(request, response);
}
}
如您所見,過濾器正在使用自定義 UserDetails 來驗證用戶名。如何將 Customer userdetails 服務添加到過濾器?這是我的第一個多登錄專案,請寬容我。
uj5u.com熱心網友回復:
在我看來,你已經這樣做了。
@Autowired
private UserAccountService myUserDetailsService;
但我建議使用建構式而不是 @Autowired。Spring 將完全相同地填充建構式引數。當您也使用 lombok 庫時,這可能會非常小。使用建構式也使得模擬這個更容易進行測驗。
@RequiredArgsConstructor
@Component
public class JwtRequestFilter extends OncePerRequestFilter {
private final JwtTokenUtil jwtTokenUtil;
private final UserAccountService myUserDetailsService;
...
uj5u.com熱心網友回復:
登錄時區分用戶和客戶。因此,呼叫不同的服務以獲取用戶詳細資訊。可以在這里找到更多資訊。 針對客戶和員工的 Spring Security 用戶身份驗證
uj5u.com熱心網友回復:
如何將 Customer userdetails 服務添加到過濾器?: 像你一樣注入它
UserAccountService。如果您這樣做,您將使用 1 個過濾器(當然,這個過濾器在 1 中SecurityFilterChain),您基本上可以實作您的過濾器,例如:嘗試通過以下方式驗證您的用戶myUserDetailsService,如果不成功,請繼續使用myCustomerDetailsService.對于多登錄專案。第二種方法是使用 2
SecurityFilterChain。例如,UserJwtFilter 為 1SecurityFilterChain,CustomJwtFilter 為 1SecurityFilterChain。人們通常為不同的登錄機制Basic、OAuth2、SAML2 使用這種方式。例如:
- 基本認證:
@Configuration
@Order(2)
public class BasicAuthenticationFilterChain extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.requestMatchers()
.antMatchers("/login", "/logout")
.and()
- OAuth2 認證:
@Configuration
@Order(3)
public class OAuth2AuthenticationFilterChain extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.requestMatchers()
.antMatchers("/oauth")
.and()
在這種情況下,當帶有“/login”的請求將被定向到BasicAuthenticationFilterChain,帶有“/oauth”的請求將被定向到OAuth2AuthenticationFilterChain。關于Order:越低優先級越高,一旦請求被一個 處理SecurityFilterChain,它就不會轉到另一個SecurityFilterChain。您可以通過這種方式實施您的專案。
結論:有很多方法可以使用 Spring Security 實作您的想法,這取決于您的選擇。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/337286.html
