【问题标题】:Spring security - Basic authSpring 安全 - 基本身份验证
【发布时间】:2015-08-08 17:59:42
【问题描述】:

我正在尝试使用 POST 请求插入数据,但出现 403 错误。当我使用 GET 时,基本身份验证有效。对于测试,我使用 Fiddler。

有什么问题?

安全配置:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/**").hasRole("USER").and()
                .httpBasic();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth)
            throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user")
                    .password("password")
                    .roles("USER");
    }
}

请求 - POST:

User-Agent: Fiddler
Host: localhost:8080
Content-Length: 200
Content-Type: application/json
Authorization: Basic dXNlcjpwYXNzd29yZA==

请求正文:

{"name" : "name1",
"description" : "desc1"}

【问题讨论】:

    标签: java spring spring-security spring-boot basic-authentication


    【解决方案1】:

    可能是 CSRF,spring security 默认启用的。在 GET 请求中查找 X-XSRF-TOKEN 标头,并在 POST 中使用该标头和值。

    在执行此操作之前请三思,但您可以使用

    禁用 csrf
    http.csrf().disable()
    

    https://docs.spring.io/spring-security/site/docs/current/reference/html/web-app-security.html#csrf

    【讨论】:

      【解决方案2】:

      试试这个:

      @Configuration
      @EnableWebSecurity
      public class HelloWebSecurityConfiguration
         extends WebSecurityConfigurerAdapter {
      
        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) {
          auth
            .inMemoryAuthentication()
              .withUser("user").password("password").roles("USER");
        }
      }
      

      来源:http://spring.io/blog/2013/07/03/spring-security-java-config-preview-web-security/

      【讨论】:

      • 不太清楚:您是否尝试使用表单和 POST 登录/插入数据等,它不能与 SpringSecurity 一起使用,但 GET 可以?
      猜你喜欢
      • 2018-09-07
      • 1970-01-01
      • 2016-12-25
      • 2011-05-04
      • 1970-01-01
      • 2013-01-20
      • 2015-09-06
      • 2017-03-21
      • 2019-01-27
      相关资源
      最近更新 更多