[英]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.