Spring Security版本6 . permit()方法

eagi6jfj  于 2023-10-20  发布在  Spring
关注(0)|答案(1)|浏览(123)
@Configuration
@EnableWebSecurity
public class SecurityConfig  {
    @Bean
    public SecurityFilterChain securityFilterchain(HttpSecurity httpSecurity) throws Exception { 
        
        httpSecurity
            .authorizeHttpRequests()
                .requestMatchers(new AntPathRequestMatcher("/AddUser/**")).permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin();

        return httpSecurity.build();
    }

我正在使用这个配置类,我试图访问端点AddUser,但它仍然要求进行身份验证(请查看登录页面中的用户名和密码),这不应该发生,因为这个端点需要允许访问而无需身份验证。

8fsztsew

8fsztsew1#

我相信这里有一个Spring Security 5.x配置,对于6,你可以使用例如:

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            .csrf(Customizer.withDefaults())
            .authorizeHttpRequests(authorize -> authorize
                .anyRequest().permitAll()
            )
            .httpBasic(Customizer.withDefaults())
            .formLogin(Customizer.withDefaults());
        return http.build();
    }

}

文件:Spring Security docsGithub example configuration

相关问题