我正在尝试用JWT开发Spring Security项目。我想访问登录api没有Spring安全(没有JWT令牌)。但在下面的配置中,每次(对于登录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);
}}
提前感谢
发布于 2017-02-16 05:41:07
对于登录路径配置,可以使用类似以下内容:
@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/**");
}发布于 2018-12-14 19:01:23
在调用每个服务之前,都会执行一个名为JwtAuthFilter的筛选器类。
.addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class) 这段代码提供了在每次请求之前要执行的过滤器,但这是可以的,你必须看到这个FilterClass必须有一些检查,如果令牌不存在过滤器类必须返回,请求将直接转到登录服务。如果您可以显示Filter类,我将帮助您。
https://stackoverflow.com/questions/42257585
复制相似问题