【问题标题】:Is it possible to hack the spring-security-core plugin @Secured annotation to be able to reference request params given to a controller?是否可以破解 spring-security-core 插件 @Secured 注释以引用给控制器的请求参数?
【发布时间】:2016-07-09 03:16:19
【问题描述】:

我正在使用Grails 2.3.3spring-security-core:2.0-RC4 插件。

我正在尝试通过根据来自接受参数的服务的方法调用的结果来保护控制器操作来保护它。这个参数应该是请求参数里面的东西。

我希望能够做到以下几点:

@Secured("@mySecurityService.myCustomCheck(params.id)")
def myAction(){
    //do some things
}

我设法做到了以下几点:

@Secured("@mySecurityService.myCustomCheck()")

但现在我不知道如何访问发送到控制器的请求参数。

在架构上是否可以在 @Secured 表示法中引用 params 变量?

PS:我知道你会要求我使用spring-security-acl 插件。我的问题是它还添加了一堆我认为我不需要的其他东西。

【问题讨论】:

    标签: grails spring-security


    【解决方案1】:

    在 2.0 中,您可以使用闭包作为注解的检查;文档中有简短的文章和示例:https://grails-plugins.github.io/grails-spring-security-core/v2/guide/newInV2.html

    你可以这样表达你的例子:

    @Secured(closure={
        ctx.mySecurityService.myCustomCheck(
           request.getParameter('id'))
    })
    

    返回true 以允许访问。

    请注意,ApplicationContext 可用作 ctx 变量,请求可用作 request;这是使用 SpEL 时可用的其他变量和方法的补充(有关详细信息,请参阅 Spring Security 文档)。 params 不可用,但您可以使用 request.getParameter 访问值

    【讨论】:

    • 这行得通,但我的做法不同:我设法用@mySecurityService 调用了该服务。我曾尝试使用 request 来查看是否可以通过任何方式获取参数,但如果它不可用,那么我将不得不解析 Web 请求的 URI。
    • 也许我没有很好地解释自己,或者我错过了什么。 request.getParameter('id') 只有在参数类似于http://example.com/myController/myAction?id=1 时才有效?我试过像你展示的那样,但没有奏效。我的 id 类似于 http://example.com/myController/myAction/id
    • 对,那会是个问题。我将添加 params 作为 2.0 final 的可用变量,但现在您可以使用 request['org.codehaus.groovy.grails.WEB_REQUEST'].params 访问它
    • 谢谢。你已经给了我足够的帮助来完成这项工作。
    猜你喜欢
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    • 2015-01-28
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 2016-08-11
    相关资源
    最近更新 更多