Bak*_*123 2 java spring spring-security basic-authentication spring-boot
我正在尝试使用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");
}
}
Run Code Online (Sandbox Code Playgroud)
请求-POST:
User-Agent: Fiddler
Host: localhost:8080
Content-Length: 200
Content-Type: application/json
Authorization: Basic dXNlcjpwYXNzd29yZA==
Run Code Online (Sandbox Code Playgroud)
要求正文:
{"name" : "name1",
"description" : "desc1"}
Run Code Online (Sandbox Code Playgroud)
可能是CSRF,默认情况下弹簧安全启用。在GET请求中查找X-XSRF-TOKEN标头,然后在POST中使用该标头和值。
在执行此操作之前,请三思而后行,但是您可以通过以下方式禁用csrf:
http.csrf().disable()
Run Code Online (Sandbox Code Playgroud)
https://docs.spring.io/spring-security/site/docs/current/reference/html/web-app-security.html#csrf