我无法在 Java Spring Boot 过滤器链中设置某些内容

问题描述 投票:0回答:1

我正在尝试在java中创建过滤器链(这是管理Java中最重要的安全选项的新方法,旧的东西现在已被弃用)。我试图禁用 cors 和 csrf 并使应用程序仅接受

/hello
/register
/login
地址,但总是出现问题。在第一种情况下,应用程序自动将每个请求重定向到
/login
地址(实际代码),在其他情况下,当我尝试更改代码中的某些内容时,我收到 403 错误。

@Configuration
public class WebSecurityConfig {

    private static final String[] URL_WHITE_LIST = {
            "/hello",
            "/register",
            "/login"
    };

    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .cors(Customizer.withDefaults()).csrf(AbstractHttpConfigurer::disable).authorizeHttpRequests(config -> config.requestMatchers(URL_WHITE_LIST).authenticated().anyRequest().permitAll()).formLogin(Customizer.withDefaults());

        return http.build();

    }}
java spring-boot spring-security authorization
1个回答
0
投票

要求是:

  1. 禁用 CORS
  2. 禁用 CSRF
  3. 允许访问 URL_WHITE_LIST
  4. 所有其他路径都应该经过身份验证
  5. 使用表单登录进行身份验证

尝试以下操作:

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            .cors().disable() // (1)
            .csrf().disable() // (2)
            .authorizeHttpRequests()
                .antMatchers(URL_WHITE_LIST).permitAll() // (3)
                .anyRequest().authenticated() // (4)
            .and()
            .formLogin(); // (5)
        return http.build();
}
© www.soinside.com 2019 - 2024. All rights reserved.