Alo*_*es5 2 spring-security spring-boot
我收到错误 403 Forbidden for POST 端点,其他端点按预期工作。
我有 4 个端点,我需要重现身份验证行为:
GET \users - no authentication
GET \details\1 - needs authentication
GET \users\1 needs authentication
POST \users\1 needs authentication
Run Code Online (Sandbox Code Playgroud)
我的配置类:
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
Maven 依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我怀疑csrf是造成问题的原因。
如果您不使用csrf但仍将默认启用。请参阅跨站点请求伪造 (CSRF),因此请尝试禁用csrf保护。
如果您在安全中启用CSRF,您的发布请求需要更新以包含一些额外信息。它解释了为什么 GET 有效,但 POST 无效。
在您的情况下,尝试像下面那样禁用它,看看它是否能解决问题。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1965 次 |
| 最近记录: |