Springboot过滤器在permitAll urls的情况下也执行

Dan*_*ved 0 authentication spring-security spring-boot api-authorization

我是 springboot 的新手,并试图实现安全性,其中没有过滤器应用于我的登录、注册和主页 url。

我使用的是springboot 2.7.1

我希望antMatchers("/**/signup").permitAll()不受任何安全过滤器的影响。

调试后,我发现我的注册网址被命中,用户详细信息被保存,但我的 AuthorizationFilter 也被执行。

这是我的 SecurityFilterChain :

@Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

        AuthenticationManagerBuilder authenticationManagerBuilder=http.getSharedObject(AuthenticationManagerBuilder.class);
        authenticationManagerBuilder.userDetailsService(userLoginService).passwordEncoder(bCryptPasswordEncoder);
        AuthenticationManager authenticationManager=authenticationManagerBuilder.build();

        http.csrf().disable().authorizeHttpRequests()
                .antMatchers("/**/login").permitAll()
                .antMatchers("/**/signup").permitAll()
                .antMatchers("/home/**").permitAll()
                .anyRequest().authenticated().and()
                .addFilter(getAuthenticationFilter(authenticationManager))
                .addFilter(new AuthorizationFilter(authenticationManager))
                .authenticationManager(authenticationManager)
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        return http.build();
    }
Run Code Online (Sandbox Code Playgroud)

我不明白为什么会这样。

Mar*_*gio 5

当您使用时permitAll(),您并没有禁用过滤器,您只是指定您不想对此应用任何授权检查RequestMatcher。所有过滤器仍然有效。

AuthorizationFilter被调用,但由于您配置了permitAll()该端点,因此它将始终授予访问权限。