Spring security - Basic auth

时间:2015-05-26 21:18:42

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

我正在尝试使用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"}

2 个答案:

答案 0 :(得分:4)

它可能是CSRF,弹出安全性默认启用。在GET请求中查找X-XSRF-TOKEN标头,并在POST中使用该标头和值。

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

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

http://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html

答案 1 :(得分:1)

试试这个:

@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/