1

我想在我的Grails應用程序中創建一個AuthorizationFilter,它將檢查請求參數並確定用戶是否有權處理請求。我希望我可以從我的AuthorizationFilter內引發自定義異常,後來通過的路線Grails的聲明execption處理像處理:Grails是否允許使用過濾器進行聲明式異常處理?

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

...但原來當我嘗試這個Grails的2.2.4(和最新的開發快照)我得到

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

所以...有沒有什麼好的方法來使用聲明式異常處理和過濾器?

+0

快速提示:如果您正在修改UrlMappings,那麼首選一個grails clean然後run-app。我現在再次測試了實現,並且它按預期工作。如果需要,我可以分享項目/代碼。 – dmahapatro

回答

3

我想你可以從Controllers,但Filters處理聲明異常。您可以使用響應,而不是發送錯誤代碼

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 
      } 
     } 
    } 
} 

上面的邏輯將呈現從ErrorController的依據是在問題中提供的UrlMappingstatus403行動響應

確保您從過濾器除去error控制器。

相關問題