对一个请求禁用基本身份验证(Spring Security),对所有请求都保留

时间:2020-06-05 16:09:06

标签: java spring spring-boot spring-security

我如何禁用一个请求的基本身份验证,并保留所有请求的身份验证。 我尝试做,但是这些不适用于mi。

@Override
protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
            .authorizeRequests()
                .antMatchers(HttpMethod.POST, "/registration").permitAll()
            .and()
             .authorizeRequests()
        .anyRequest().authenticated().and().httpBasic();
}

基本身份验证仍适用于“ / registration”。

1 个答案:

答案 0 :(得分:2)

我假设/registration是您创建的网页。那应该是

http.csrf()
    .disable()
    .httpBasic()
    .and()
    .authorizeRequests()
    .antMatchers(HttpMethod.GET,"/registration")
    .permitAll()
    .anyRequest()
    .authenticated()

如果HttpMethod.POST是API端点而不是网页,则应使用POST,或者如果出于某些原因,如果您也有/registration的{​​{1}}请求,请删除{{ 1}}在一起,只是将HttpMethod.GET留在/registration