6

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

现在,我在 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')

RESTful 映射与 ItemRestController 完美配合:每个方法(显示、更新、保存、删除)都使用正确的 HTTP 方法正确映射。额外的方法 (batchDelete) 也有效。

我保护了 API url,这样做:

grails.plugins.springsecurity.controllerAnnotations.staticRules = [
     // ...
     '/something/**': ['IS_AUTHENTICATED_FULLY']
     '/api/**': ['IS_AUTHENTICATED_FULLY']
]

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

http://host/context/something/bla_bla

但如果我调用(在需要时使用适当的有效负载)则不会:

http://host/context/api/item/batchDelete
http://host/context/api/item/1
http://host/context/api/item

我的怀疑是在将其余控制器与资源键映射时,静态规则无法正常工作。

另请注意,UrlMapping.groovy 文件中不存在“某物”网址。

有任何想法吗?

4

1 回答 1

7

我认为你必须使用

grails.plugins.springsecurity.controllerAnnotations.staticRules = [
     '/itemrest/**': ['IS_AUTHENTICATED_FULLY'],
      //this will be redundant after the above rule I guess
     '/api/**': ['IS_AUTHENTICATED_FULLY']
]

没有在 urlMapping 中映射的 URL 必须controller直接在规则中引用。查看文档中的警告controllerAnnotations.staticRules

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

于 2013-05-24T14:59:11.020 回答