Spring 安全配置过滤特定端点以外的任何请求?

Spring Security Configuration filter any requests except a specific endpoint?

我有以下代码

http.authorizeRequests()
                .antMatchers("/users/login","/token/refresh").permitAll()
                .anyRequest().authenticated()
                .and()
                .addFilterAfter(new JWTAuthenticationFilter(),UsernamePasswordAuthenticationFilter.class)

我认为这意味着不要过滤匹配 /users/login 或 /token/refresh 的请求,而是过滤任何不匹配的请求。 但它仍然过滤 /users/login.

试试这个。

.antMatchers("/users/login").permitAll()
.antMatchers("/token/refresh").permitAll()

我在旧项目中解决的方法是要求在每个端点上进行身份验证

http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .addFilterAfter(new JWTAuthenticationFilter(),UsernamePasswordAuthenticationFilter.class)

并忽略所需端点的安全性

 public void configure(WebSecurity web) {
        web.ignoring().antMatchers(HttpMethod.POST, "/users/login");
    }

csrf 由 Spring 安全自动启用。尝试禁用它。

http.csrf().disable().authorizeRequests().antMatchers("/users/login","/token/refresh").permitAll().anyRequest().authenticated( ).and().addFilterAfter(new JWTAuthenticationFilter(),UsernamePasswordAuthenticationFilter.class)