Spring Boot + Spring Security permitAll() 和 addFilter() 配置没有效果

Sel*_*lva 6 spring spring-security

  1. 带有 /login 的 URL 模式应该通过验证用户 ID 和密码的 LoginFilter - 工作正常
  2. 带有 /users/register 的 URL 模式不应通过任何文件管理器,但它始终通过 JWTAuthentication 过滤器 - 无法正常工作
  3. 所有其他 URL 模式都应该通过 JWTAuthentication 过滤器进行授权 - 工作正常

下面是我的安全配置代码。请帮助我解决此代码中缺少的内容。如何配置过滤器,以便对 /login 和 /register 以外的 URL 模式进行 JWT 身份验证

弹簧安全核心:4.2.3,弹簧引导:1.5.4

 protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().authorizeRequests()
            .antMatchers("/").permitAll()
            .antMatchers(HttpMethod.POST, "/login").permitAll()
            .antMatchers(HttpMethod.POST, "/users/register").permitAll()
            .anyRequest().authenticated()
            .and()
            // We filter the api/login requests
            .addFilterBefore(new LoginFilter("/login", authenticationManager()),
                    UsernamePasswordAuthenticationFilter.class)
            // And filter other requests to check the presence of JWT in header
            .addFilterBefore(new NoLoginAuthenticationFilter("/users/register"), UsernamePasswordAuthenticationFilter.class)
            .addFilterBefore(new JWTAuthenticationFilter("/**", authenticationManager()),
                    UsernamePasswordAuthenticationFilter.class);

}
Run Code Online (Sandbox Code Playgroud)

Mah*_*oya 6

您想要的是忽略某些 URL。为此,覆盖采用 WebSecurity 对象并忽略模式的配置方法。

尝试将以下方法覆盖添加到您的配置类。

@Override
public void configure(WebSecurity web) throws Exception {
    web
    .ignoring()
    .antMatchers("/users/register/**");
}
Run Code Online (Sandbox Code Playgroud)