【问题标题】:HttpSecurity POST 403 ForbiddenHttpSecurity POST 403 被禁止
【发布时间】:2019-04-01 21:25:29
【问题描述】:

我收到错误 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】:

    我发现this 很有帮助

    http.csrf().disable().cors().and().....
    

    【讨论】:

      【解决方案2】:

      我怀疑是csrf 引起了问题。

      如果您没有使用csrf,但它仍将默认启用。请参阅Cross Site Request Forgery (CSRF),因此请尝试禁用csrf 保护。

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

      在你的情况下,尝试如下禁用它,看看它是否能解决问题。

      @Override
        protected void configure(HttpSecurity http) throws Exception {
          http
            .csrf().disable();
        }
      

      【讨论】:

        猜你喜欢
        • 2020-02-11
        • 1970-01-01
        • 2019-04-10
        • 2014-04-13
        • 2018-11-06
        • 1970-01-01
        • 2019-09-03
        • 2011-10-21
        相关资源
        最近更新 更多