2016-08-03 14 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

相關問題