Grails spring-security静态规则的休息资源似乎无法正常工作

luc*_*e84 6 rest grails spring-security url-mapping

我有一个使用Spring Security插件(版本1.2.7.3)的Grails(2.0.4)应用程序和安全注释方法(默认方法,更多这里).

现在,我在UrlMapping.groovy中使用资源键或控制器/操作对来拥有这些URL,如下所示:

"/$controller/$action?/$id?" {
        constraints {
            // apply constraints here
        }
    }

// other rules, all working properly

"/api/item/$id?"(resource: 'itemRest')
'/api/item/batch-delete'(controller: 'itemRest', action: 'batchDelete')
Run Code Online (Sandbox Code Playgroud)

RESTful映射与ItemRestController完美配合:使用正确的HTTP方法正确映射每个方法(显示,更新,保存,删除).额外的方法(batchDelete)也可以.

我保护了API网址,这样做:

grails.plugins.springsecurity.controllerAnnotations.staticRules = [
     // ...
     '/something/**': ['IS_AUTHENTICATED_FULLY']
     '/api/**': ['IS_AUTHENTICATED_FULLY']
]
Run Code Online (Sandbox Code Playgroud)

现在,如果我打电话,我会被重定向到登录页面:

http://host/context/something/bla_bla
Run Code Online (Sandbox Code Playgroud)

但是,如果我打电话(在需要时使用适当的有效载荷):

http://host/context/api/item/batchDelete
http://host/context/api/item/1
http://host/context/api/item
Run Code Online (Sandbox Code Playgroud)

我怀疑是在使用资源键映射其余控制器时,静态规则无法正常工作.

另请注意,UrlMapping.groovy文件中不存在"something"网址.

有任何想法吗?

dma*_*tro 7

我想你必须使用

grails.plugins.springsecurity.controllerAnnotations.staticRules = [
     '/itemrest/**': ['IS_AUTHENTICATED_FULLY'],
      //this will be redundant after the above rule I guess
     '/api/**': ['IS_AUTHENTICATED_FULLY']
]
Run Code Online (Sandbox Code Playgroud)

未在urlMapping中映射的网址必须controller直接在规则中引用.看一看的警告controllerAnnotations.staticRules的文档.

映射在UrlMappings.groovy中映射的控制器的URL时,您需要保护未URL映射的URL.例如,如果您有一个映射到/ foo/bar/$ action的FooBarController,则必须在controllerAnnotations.staticRules中将其注册为/ foobar/**.这与您将用于其他两种方法的映射不同,并且是必需的,因为controllerAnnotations.staticRules条目被视为它们是相应控制器上的注释.