1

I'd like to create an AuthorizationFilter in my Grails app that will inspect request parameters and determine whether a user is authorized to proceed with a request. I was hoping I could throw a custom exception from within my AuthorizationFilter and later handle it with Grails declarative execption handling via a route like:

"403"(controller: 'error', action: 'status403', exception:AuthException)

... but turns out when I try this in grails 2.2.4 (and the latest dev snapshot) I get

java.lang.IllegalArgumentException: Method name must not be null
    at grails.plugin.cache.web.ProxyAwareMixedGrailsControllerHelper.retrieveAction(ProxyAwareMixedGrailsControllerHelper.java:41)

So... is there any good way to use declarative exception handling together with filters?

4

1 に答える 1

3

Controllersbutからの宣言的例外を処理できると思いますFilters。代わりに response を使用してエラー コードを送信できます

class AuthorizationFilters {
    def filters = {
        auth(controller: 'error', invert: true) {
            before = {
                if(1){ //If auth fails
                    response.sendError(403)
                    //render(status: 403) //or
                    //redirect(controller: 'error', action: 'status403') //or
                }
                return false
            }
        }
    }
}

上記のロジックは、質問で提供されたにstatus403基づいて、ErrorController のアクションからの応答をレンダリングします。UrlMapping

errorコントローラーをフィルターから除外していることを確認してください。

于 2013-08-29T14:56:32.300 に答える