Spring boot 端点身份验证失败

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

这是我正在使用的安全配置

@Configuration
public class SpringSecurityConfiguration {

    @Bean
    public SecurityFilterChain getFilter(HttpSecurity http) throws Exception{
        http.csrf().disable();
        http.authorizeHttpRequests(
                auth->auth.anyRequest().authenticated()
        );
        http.httpBasic(Customizer.withDefaults());

        return http.build();

    }
}

我有两个端点

  1. loginUser 作为 get 方法 -> 其身份验证正在工作
  2. registerUser 作为 post 方法 -> 其身份验证总是失败。

使用的版本:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

有什么建议说明为什么 registerUser 端点单独失败吗?

实际上,我不需要对注册表端点进行任何身份验证。所以我尝试使用“requestMatchers”禁用该路径的身份验证。即使禁用后,我仍然收到 401 未经授权的错误。 因此,在调试时,我删除了 requestMatchers 设置并尝试进行身份验证,因为它应该进行身份验证,但失败了。另外,我尝试禁用 csrf,但不起作用

java spring-boot spring-security spring-data spring-rest
1个回答
0
投票

使用 (1) 表示所有请求都需要进行身份验证,包括注册用户的端点,在此之前,您必须指定没有任何身份验证的 url (2)。

(1)

http.authorizeHttpRequests(
                auth->auth.anyRequest().authenticated()
        );

(2)

http.authorizeHttpRequests(
                auth->auth.requestMatchers("YOUR_PATH_WITHOUT_AUTHENTICATION")
                                             .permitAll().anyRequest().authenticated()
        );
© www.soinside.com 2019 - 2024. All rights reserved.