2012-05-16 42 views
0
package hompage 

import logindetails.* 
import login.User 
import grails.plugins.springsecurity.Secured; 

class RedirectController 
{ 
    def springSecurityService 

    def index() { } 

    /** 
    * This method will redirect the user if it has the role "ROLE_ADMIN" 
    */ 
    @Secured(['ROLE_ADMIN']) 
    def adminhomepage = 
    { 
     render(view: "adminhome") 
    } 

    /** 
    * This method will redirect the user if it has the role "ROLE_USER" 
    */ 
    @Secured(['ROLE_USER']) 
    def userhomepage= 
    { 
     render(view: "userhome") 
    } 
} 

這是我的代碼,我想重定向到adminhome當且僅當角色是ROLE_ADMIN和USERHOME如果該角色ROLE_USER。Grails的春季安全插件@Secured標註不解決

請幫我執行這段代碼。

我使用NetBeans IDE,並有進口進口grails.plugins.springsecurity.Secured

unable to resolve class grails.plugins.springsecurity.Secured @ line 5, column 1. 

即使(通常表明Java代碼就像紅色感嘆號)警告進口是目前我沒有得到它是什麼問題?

+0

入住這裏http://stackoverflow.com/questions/19877725/grails-spring-core-security-plugin-unable-to-resolving-classes – akaashanky

回答

2

檢查你的classpath,做一個grails clean,並確保這個插件存在於BuildConfig.groovy中。

0

我在Netbeans中也得到警告;它不會影響我的任何事情。一切仍然編譯並運行良好。

@Secured註釋不會做任何形式的重定向 - 它只是確保在您發送到那裏時允許您執行該操作。

我想確保我明白 - 無論他們在'adminHomeController/index'被重定向到'adminHomeController/adminhomepage.gsp'還是他們得到'adminHomeController/index.gsp'向上?如果他們被重定向,那麼您提供的代碼中沒有任何內容會導致這種情況。你有任何URL映射會這樣做嗎?

或者是真正的問題,如果有人沒有ROLE_USER他們仍然被允許去'adminHomeController/adminhomepage'?如果這是發生了什麼事請確保您在您的配置文件如下:

grails.plugins.springsecurity.securityConfigType = "Annotation" 

然後重新啓動應用程序並嘗試訪問adminhomepage作爲不具有ROLE_USER用戶。如果他們仍然可以到達那裏,確保他們確實沒有ROLE_USER。

如果這是在登錄時重定向某人,有更多更全面的方法來處理這個問題,比如創建一個自定義的AuthSuccessHandler,但這有點複雜。要做到重定向這樣我會做這樣的事情:

import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils.* 

    def index() { 
     if(ifAnyGranted("ROLE_ADMIN")) { 
      redirect(action: 'adminhomepage') 
     } else if(ifAnyGranted("ROLE_USER")) { 
      redirect(action: 'userhomepage') 
     } 
    } 

    def adminhomepage() { 
     //admin stuff 
    } 

    def userhomepage() { 
     //user stuff 
    } 
+0

基本上我只是想,如果登錄用戶擁有角色作爲ROLE_ADMIN比只有def adminhomepage()應該得到執行,如果用戶是普通用戶然後def userhomepage()得到執行。 – user1397872

+0

那麼你沒有真正回答我問過的任何問題,並且你在原始文章中沒有提到有關ROLE_ADMIN或def userhomepage()的任何信息。看看我的編輯,如果這不起作用請詳細解釋發生了什麼。 – Kelly

+0

基本上我使用彈簧安全核心插件爲用戶管理部分。我正在按照春季安全代碼的教程,這裏是鏈接。 blog.springsource.org/2010/08/11.....在這個鏈接中,你會發現PostController()類,它說,一旦在任何檢查角色的方法中定義了註解,並執行該方法, t.I編輯了我的原始帖子,請參閱並提出解決方案 – user1397872

0

我跟着你的意見列出的相同教程,並擊中了同樣的問題。正如本文所述,「錯誤」並沒有阻止我的代碼從命令行構建。但作爲一名Grails初學者,在遊戲中如此早期看到這款小紅X最基本的應用可能令人沮喪。

由於應用程序編譯並從命令行運行,這肯定是IDE的問題。我檢查了我的插件,Spring Security沒有列出。右鍵單擊插件 - >打開Grails插件管理器並通過STS以這種方式安裝。它會卸載並重新安裝相同的插件。我正在運行較舊版本的STS,因此希望這可以通過更新版本來解決。

希望這有助於任何人在這個較舊的線程上絆倒。

4

包已經從grails.plugins重命名爲grails。插件

import grails.plugin.springsecurity.annotation.Secured 

請使用此。 編號:source

+1

鏈接已損壞。 –

+0

修復了斷開的鏈接,謝謝! – Balaji