HttpSecurity POST 403 Forbidden

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

我收到错误403 Forbidden for POST端点,其他端点正在按预期工作。

我有4个端点,我需要重现身份验证行为:

GET \users - no authentication
GET \details\1 - needs authentication
GET \users\1 needs authentication
POST \users\1 needs authentication

我的配置类:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(AuthenticationManagerBuilder auth)
        throws Exception {
    auth.inMemoryAuthentication()
            .passwordEncoder(org.springframework.security
                .crypto.password.NoOpPasswordEncoder.getInstance())
            .withUser("user").password("pwd")
            .roles("USER").and().withUser("admin").password("pwd")
            .roles("USER", "ADMIN");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers( "/users").permitAll()
            .anyRequest().authenticated()
            .and()
            .httpBasic();
     }
}

Maven依赖:

 <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-security</artifactId>
  </dependency>
spring-boot spring-security
1个回答
0
投票

我怀疑csrf造成了这个问题。

如果你没有使用csrf但仍默认启用它。请参阅Cross Site Request Forgery (CSRF)所以尝试禁用csrf保护。

如果您在安全性中启用CSRF,则需要更新您的帖子请求以包含一些额外信息。它解释了为什么GET可以工作,但POST没有。

在您的情况下,尝试禁用它,如下所示,看看它是否解决了问题。

@Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      .csrf().disable();
  }
© www.soinside.com 2019 - 2024. All rights reserved.