[英]Spring Security with JWT
我正在嘗試使用JWT開發Spring Security項目。 我想在沒有Spring Security的情況下(沒有JWT令牌)訪問Login api。 但是使用下面的配置,每次(對於登錄api也是如此)它正在檢查JWT令牌,從而給我403錯誤。
以下是我的WebSecurityConfig。
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private JwtAuthFilter jwtAuthFilter;
@Autowired
private TokenAuthenticationService jwtAuthenticationProvider;
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(jwtAuthenticationProvider);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().ignoringAntMatchers("/api/v1/login");
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/api/v1/login")
.permitAll()
.and()
.addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);
}
}
提前致謝
對於登錄路徑配置,可以使用如下所示的內容:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/**").hasRole("USER").and().formLogin()
.usernameParameter("username") // default is username
.passwordParameter("password") // default is password
.loginPage("/authentication/login") // default is /login with an HTTP get
.failureUrl("/authentication/login?failed") // default is /login?error
.loginProcessingUrl("/authentication/login/process"); // default is /login
// with an HTTP
// post
}
如果需要忽略某些路徑,則可以覆蓋configure(WebSecurity web)
:
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/api/v1/somepath").antMatchers("/static/**");
}
在調用每個服務之前,都有一個名為JwtAuthFilter的過濾器類正在執行。
.addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class)
該代碼提供了在每個請求之前執行過濾器的方法,但是沒關系,您必須看到此FilterClass必須進行一些檢查,如果令牌不存在,則必須返回過濾器類,並且請求將直接轉到登錄服務。 如果您可以顯示Filter類,我會為您提供幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.