【问题标题】:Grails spring-security static rules for rest resource seems not working properly休息资源的 Grails spring-security 静态规则似乎无法正常工作
【发布时间】:2013-05-20 03:50:18
【问题描述】:

我有一个 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 文件中不存在“某事”网址。

有什么想法吗?

【问题讨论】:

    标签: rest grails spring-security url-mapping


    【解决方案1】:

    我认为你必须使用

    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 下的warning

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

    【讨论】:

      猜你喜欢
      • 2015-02-05
      • 2013-05-06
      • 2023-04-08
      • 1970-01-01
      • 2018-10-29
      • 2017-04-29
      • 2015-03-18
      • 2015-08-21
      • 2017-09-04
      相关资源
      最近更新 更多