具有过滤器的Spring Security允​​许无法正常工作

时间:2017-09-06 06:39:55

标签: spring spring-boot spring-security

我有这个安全配置:

@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();
    }

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

然而,permitAll不允许根路由和“/ register”路由通过(也称为仍然通过authenticationFilter,我认为允许所有这些路由通过过滤器)

怎么了?

1 个答案:

答案 0 :(得分:4)

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

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

相关问题