Spring &引用;请求方法';邮政';“不支持”;在放置安全注释之后
我有两个控制器: (a) (b) 在我将安全性注释放入控制器“b”之前,一切都很好。 现在,当我转到控制器“a”页面并填写表单时,单击导致控制器“b”的按钮,我得到“HTTP状态405-请求方法“POST”不受支持”。 为什么会发生这种情况以及如何解决Spring &引用;请求方法';邮政';“不支持”;在放置安全注释之后,spring,spring-mvc,spring-security,annotations,Spring,Spring Mvc,Spring Security,Annotations,我有两个控制器: (a) (b) 在我将安全性注释放入控制器“b”之前,一切都很好。 现在,当我转到控制器“a”页面并填写表单时,单击导致控制器“b”的按钮,我得到“HTTP状态405-请求方法“POST”不受支持”。 为什么会发生这种情况以及如何解决 UPD:我帮助添加了登录控制器RequestMethod.POST如果没有spring安全配置,很难进行调试,但我猜当您转到/portal/form.html时,spring会使用HTTP POST将您重定向到登录页面,而您的登录页面控制器处理程
UPD:我帮助添加了登录控制器RequestMethod.POST如果没有spring安全配置,很难进行调试,但我猜当您转到/portal/form.html时,spring会使用HTTP POST将您重定向到登录页面,而您的登录页面控制器处理程序只映射到HTTP GET。尝试将登录页面处理程序映射到POST方法。在将我的应用程序对Spring Security的使用从基于XML的配置迁移到基于JavaConfig的配置时,我也碰巧遇到了您描述的症状 在我的特殊情况下,
HTTP状态405-请求方法“POST”不受支持的消息原因是Spring Security在使用基于JavaConfig的配置时默认激活CSRF保护
要消除这一原因,可以:
- 要么更新应用程序以遵循Spring Security的CSRF保护机制
- 或者停用Spring Security的CSRF保护机制
要停用CSRF保护机制,请在websecurityconfigure
中执行以下操作:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable() //FIXME
.authorizeRequests() ....
}
// ...
}
非常感谢你。在过去的一天里,我一直在为同一个问题绞尽脑汁。
@Override
@RequestMapping(value = "/portal/form.html", method = RequestMethod.POST)
@Transactional
@PreAuthorize("#message.id!=null ? hasPermission(#message, 'WRITE') : hasRole('ROLE_ADMIN')")
public String form(@Valid final Message message, final Model model) {
if (message.getId() == null) {
someService.save(message);
AclManager.create(message);
} else {
someService.update(message);
AclManager.update(message);
}
return "redirect:result.html";
}
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable() //FIXME
.authorizeRequests() ....
}
// ...
}