带有过滤器的 Spring Security 允许所有不起作用

Mar*_* Ou 2 spring spring-security spring-boot

我有这个安全配置:

@Override
    public void configure(HttpSecurity http) throws Exception {
        http
            .addFilterBefore(
                    new JwtLoginFilter("/login", authenticationManager()),
                    UsernamePasswordAuthenticationFilter.class)
            .addFilterBefore(
                    new JwtAuthenticationFilter(),
                    UsernamePasswordAuthenticationFilter.class);
        http.csrf().disable()
                .authorizeRequests().antMatchers("/", "/register").permitAll()
                .and()
                .authorizeRequests().anyRequest().authenticated();
    }
Run Code Online (Sandbox Code Playgroud)

这两个过滤器正在执行身份验证工作:loginFilter 检查帖子正文中的凭据,然后将 cookie 添加到响应中。authenticationFilter 检查 auth cookie。

但是,permitAll 不会让根路由和“/register”路由通过(也就是仍然通过 authenticationFilter,我认为 permitAll 会让这些路由通过过滤器)

怎么了?

dse*_*sep 6

permitAll()不会忽略过滤器。它只是授予访问权限,无论在处理完所有过滤器后请求的安全上下文中是否存在身份验证

您应该检查您的过滤器和它们使用的任何AuthenticationProvider实现,以确保它们不会通过抛出未经检查/未捕获的异常或明确发送对失败身份验证的响应来破坏 Spring Security 的执行流程。