2011-05-16 121 views
0
def filters = { 
    loginCheck(controller:'*', action:'*') { 
     before = { 
      if(!session.user && !session.merchants) 
      { 
       redirect(action:'login') 
       return false 
      } 
     }} 

這是我的登錄安全篩選器。下面是限制用戶搜索操作的攔​​截器。但兩者都不起作用。任何人都可以說出錯是什麼嗎?如何限制用戶訪問控制器的特定操作?

def beforeInterceptor = [action:this.&checkUser,Only:'search'] 

    def scaffold = true 
    def checkUser() 
    { 
     if (session.user) 
     { 
      redirect(action:"search") 
     } 
     if(session.merchants) 
     { 
     redirect(action:"toIndex") 
     flash.message="You are not valid user for this action" 
     return false 
     } 
    } 
+0

請不要發佈兩次完全相同的問題。 – FoamyGuy 2012-07-02 00:46:33

+0

使用Spring Security插件,不要試圖扮演你自己的角色。 – cdeszaq 2012-07-02 13:19:39

回答

0

實際上,您的過濾器應該能夠爲每個控制器和每個動作設置。 這裏是從參考Grails的例子:

class SecurityFilters { 
    def filters = { 
     loginCheck(controller:'*', action:'*') { 
      before = { 
       if(!session.user && !actionName.equals('login')) { 
        redirect(action:'login') 
        return false 
       } 
      } 
     } 
    } 
} 

我這一個工作,它爲我工作。 我不確定是在代碼中的session.merchants。 這是什麼?

是否遵循這樣的:

要創建過濾器創建一個類, 在 在grails-app/conf目錄公約過濾器結束。


編輯: 如果你使用Spring Security,你不需要添加過濾器或攔截器。 查看用戶指南:http://burtbeckwith.github.com/grails-spring-security-core/docs/manual/index.html

您可以使用網址映射或註釋對其進行配置。

ex。

grails.plugins.springsecurity.controllerAnnotations.staticRules = [ 
    '/js/admin/**': ['ROLE_ADMIN'], 
    '/someplugin/**': ['ROLE_ADMIN'] 
] 

編輯2:

來獲取登錄的用戶使用:

def authenticateService 

... 
def action{ 
    def user = authenticateService.principal() 
    def username = user?.getUsername() 
... 
+0

商人是我的另一個用戶的應用程序,而不是admin.and我命名我的過濾器結束會議過濾器 – Nandita 2011-05-17 05:46:17

+0

我認爲它更好地使用彈簧安全性,因爲許多人建議我嘗試it.i已經安裝它,並跑了S2快速入門 – Nandita 2011-05-17 05:48:29

+0

有這個命令我應該運行下一個**'grails s2-quickstart org.example SecUser SecRole'** ..但我沒有得到我應該給secUser n secRole的地方n org.example應該是我的應用程序名稱還是什麼? – Nandita 2011-05-17 05:50:48

0

有一個非常好的速記,你可以直接向動作(如果你不使用過濾器):

@Secured(['ROLE_USER']) 
def search = { 

} 

您可以爲每個用戶提供ROLE_USER並只需要該角色。

+0

它可以在沒有SpringSecurity的情況下工作嗎? – 2011-05-17 04:20:55

+1

糟糕,你是對的。絕對依賴Spring安全。 – 2011-05-17 04:52:19

+0

我剛剛安裝了s2 spring secutirity – Nandita 2011-05-17 06:05:34

相關問題