Grails HttpSecurity - 允许POST

时间:2015-09-04 13:31:28

标签: grails spring-security grails-3.0

我的Grails 3.0.3应用程序中有一个相当简单的安全配置:

@Configuration
@EnableWebSecurity
class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers('/admin/**').hasAnyRole('ADMIN')
            .antMatchers('/**').hasAnyRole('USER', 'ADMIN')
            //.antMatchers('/').permitAll()
            .and()
        .formLogin().permitAll()
            .and()
        .logout().permitAll()

    http.headers().frameOptions().disable()

    http.csrf().disable()
}

我还有一些使用@Resource注释

的DomainClasses
@Resource(uri="/myresource",formats=['json'])

当我关闭/ **路径的身份验证时 - 一切正常。但是当我为/ **打开身份验证时,包括/ myresource,它不再接受POST请求。然后返回405方法不允许。 如何在Grails 3中使用HttpSecurity允​​许POST请求?

更新1: 允许经过身份验证的用户使用GET请求

1 个答案:

答案 0 :(得分:1)

我刚遇到同样的问题。有两种方法可以解决它:

  1. 禁用HttpSecurity http.csrf().disable()中的csrf保护 配置方法

  2. 将csrf标记添加到您的JSON中。有关这方面的更多信息,请阅读 this link