2014-10-28 15 views
0

我使用Grailsspring-security-core:2.0-RC4如何在@Secured表達式中使用服務方法?

我試圖做一些自定義的安全檢查,我@Secured表達式中,像這樣:

import grails.plugin.springsecurity.annotation.Secured; 

class AdminController { 
    def myCustomSecService; 

    @Secured("@myCustomSecService.customCheck()") 
    def index() { 
     render(view:"index") 
    } 
} 

這似乎並沒有在所有的工作。 我看過類似的examples@PreAuthorize,但(顯然)需要spring-security-acl插件,我想避免使用(除非我真的必須)。

奇怪的是,spring-security-core插件包含一個jar,其中@PreAuthorize@PostAuthorize註釋都是。

我該如何去做這件事?

+0

一種選擇是用你自己的實現來替換默認的'AuthenticationProvider' bean。看看這個博客文章和附帶的示例應用程序的詳細信息:http://burtbeckwith.com/blog/?p=1090 – 2014-10-28 11:48:14

+0

我會看看,併發布一些結果,如果它的工作!謝謝:) – dosaki 2014-10-28 11:50:33

+0

我看到我可以如何使用該示例,但它似乎會隱藏檢查的類型。如果我能夠直接訪問服務,只需查看'@ Secured'註釋,就可以知道該控制器的安全性。 – dosaki 2014-10-28 13:02:31

回答

0

經過大量的挖掘,我想我已經找到了解決方案。

我看到了一個地方,如果我不得不在使用Spring時重寫類和方法,那麼我可能做錯了什麼。

所有我需要做的就是添加@Component我的服務

這裏是我來到我落得這樣做:

我的控制器:

import grails.plugin.springsecurity.annotation.Secured; 

class AdminController { 
    def myCustomSecService; 

    @Secured("@myCustomSecService.customCheck()") 
    def index() { 
     render(view:"index") 
    } 
} 

,我的服務:

import org.springframework.stereotype.Component; 

@Component("myCustomSecService") 
class MyCustomSecService { 
    def customCheck() { 
     /*do checks here*/ 
     return true; 
    } 
} 
相關問題