【发布时间】:2016-11-01 16:17:09
【问题描述】:
我使用 spring boot 开发了一个应用程序,它运行良好。有一个安静的控制器。我试图为某些页面添加弹簧安全性。 其余控制器的端点是
/api/greetings
我在下面的类中配置了安全设置。
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home","/api/greetings").permitAll()
//.antMatchers("/api/greetings","").permitAll()//can't do this
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
现在,当我尝试从 Rest 客户端(邮递员)访问 Rest 端点时,只能访问 GET 方法,如果我尝试 POST、PUT 或 DELETE,我会收到 403 Forbidden 响应。
{
"timestamp": 1467223888525,
"status": 403,
"error": "Forbidden",
"message": "Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'.",
"path": "/api/greetings/2"
}
我该如何解决这个问题。我是 Spring Security 方面的新手。
【问题讨论】:
-
因为我的是一个非浏览器应用程序,禁用 csrf 不会有什么坏处,它对我有用。我很好奇,如果我不想禁用 csrf 怎么办,或者如果我的应用程序也是 Web 应用程序,我找到了这个链接。虽然没有尝试过,但很快就会尝试。 stackoverflow.com/questions/27182701/…
标签: java rest spring-mvc spring-security spring-boot