2016-08-03 13 views
0

我已成功设置我的Grails应用程序来验证用户。如何使用@Secured闭包时获得控制器方法参数?

我映射使用网址参数控制方法的参数,在我UrlMappings.groovy:

"/$source/owner/$ownerId/show"(controller:"myController", action: "show", method: "GET") 

我如何得到我的@Secured关闭$源和值$ OWNERID?

我控制器的方法是这样的:

@Secured(closure = { 
    //null 
    def testSource = params.source 

    //also null 
    def testOwnerId = params.ownerId 

    //null 
    def owner = request.ownerId 

    //null 
    def owner2 = request.getParameter('ownerId') 

    //contains only query string params 
    def grailsParams = request['org.codehaus.groovy.grails.WEB_REQUEST']?.params 

    return true 
}) 
def show(String source, String ownerId) { 
    ... 
} 

我如何获得这些价值?我在做什么错,在这里?

我认为,这一职位将提供一个解决方案,但鉴于那里的答案并没有为我工作:

Is it possible to hack the spring-security-core plugin @Secured annotation to be able to reference request params given to a controller?

我使用下面的Grails和插件版本:

grails 2.5.1 
    compile ":spring-security-core:2.0-RC5" 
    compile ":spring-security-rest:1.5.3", { 
     excludes 'com.fasterxml.jackson.core:jackson-databind:' 
    } 
+0

如果你简单地执行'def owner = request.ownerId',会发生什么?那也是空的? – Gregg

+0

@Gregg谢谢你的回复!但是这也返回null。 – RMorrisey

+0

试试'println params'看看它们是否全部通过。如果是这样,你可以从数组中找到值。 –

回答

1

简介:

使用request.getParameter

详细信息:

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

你会表达自己的例子,因为这:

@Secured(closure={ 
     request.getParameter('ownerId') >=123 //this is example 
}) 

返回true允许访问,false拒绝访问。

+0

request.getParameter()也返回null。 URL参数未映射为请求参数。 – RMorrisey

相关问题