我已经为我的 springframework RESTful 服务配置了 Springframework security (4.0)。对于浏览器启动的 GET 访问和 action=GET 的 html 表单,事情进展顺利,但对于 method=requestMethod.POST 的服务失败,要么从浏览器提交带有 action=POST 的 HTML 表单;或者通过远程应用程序使用 RestTemplate。
为了查看问题,我将配置简化为以下内容。有了这个,我可以访问 Method=GET 的所有服务而不会受到凭据挑战(似乎是正确的),但是收到“accessDeniedPage”失败(我从未受到凭据挑战)
所有服务都在“/CouponController/**”下:
@Override
protected void configure (HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/CouponController/**").permitAll()
.and().formLogin().permitAll()
.and().exceptionHandling().accessDeniedPage("/Accesses_Denied_Due_To_Security_failure");
}
如果我使用以下配置,在访问 method=GET 的服务时我会被要求提供凭据,而在访问 Method=POST 的服务时仍然会看到相同的行为:
@Override
protected void configure (HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/CouponController/**").access("hasRole('STAFF')")
.and().formLogin().permitAll()
.and().exceptionHandling().accessDeniedPage("/Accesses_Denied_Due_To_Security_failure");
}
我做错了什么?
最佳答案
我找到了表单提交失败的原因。
默认情况下,spring security 为 XML 和 Java 配置启用 csrf。这导致我的代码无法提交表单,因为我提交的表单不包含 csrf token 。不幸的是,当我使用 Java 配置时,错误消息只是“404 请求的资源不可用”。当我切换到 XML 配置时,错误消息变得有用。
我禁用csrf后,表单提交成功。现在,我需要回过头来弄清楚如何在我的表单中包含一个 csrf token 。
希望这对您有所帮助。
关于java - Springframework 安全性,无法为 POST 方法提交 html 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35710086/