Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grails 如何限制用户访问控制器的特定操作?_Grails_Filter_Controller_Action_Interceptor - Fatal编程技术网

Grails 如何限制用户访问控制器的特定操作?

Grails 如何限制用户访问控制器的特定操作?,grails,filter,controller,action,interceptor,Grails,Filter,Controller,Action,Interceptor,这是我的登录安全过滤器。下面是拦截器,用于限制用户的搜索行为。但两者都不起作用。有人能说出错误是什么吗 def filters = { loginCheck(controller:'*', action:'*') { before = { if(!session.user && !session.merchants) { redirect(action:'login')

这是我的登录安全过滤器。下面是拦截器,用于限制用户的搜索行为。但两者都不起作用。有人能说出错误是什么吗

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

实际上,您的过滤器应该工作,因为它是为每个控制器和每个操作设置的。 以下是参考资料中的grails示例:

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
        }   
    }
我和这个一起工作,它对我来说很有用。 我不确定的是您代码中的
会话.merchants
。 这是什么

您是否遵循了以下步骤:

要创建筛选器,请创建一个 以中的约定过滤器结束 grailsapp/conf目录


编辑: 如果使用SpringSecurity,则不需要添加过滤器或拦截器。 查看用户指南:

您可以使用url映射或注释对其进行配置


编辑2:

要获取已登录的用户,请使用:

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

有一个非常好的速记,您可以直接应用于操作(如果您不使用过滤器):


你可以给每个用户角色\u user,只需要这个角色。

merchant是我应用程序的另一个用户,而不是admin。我用end convention Filters命名了我的过滤器。我认为最好使用spring security,因为很多人建议我尝试使用它。我已经安装了它,并运行了s2 QuickStart。下面是这个命令,我应该运行“grails”s2 quickstart org.example SecUser SecRole“…但是我没有得到我应该在SecUser n SecRole place n org中给出的内容。example应该是我的应用程序名还是什么?我已经有一个用户域类,我需要删除它才能拥有插件用户类吗?嗨,Nandita。在'grailss2quickstart org.example'org.example中,应该是您的应用程序名。第二个问题,是的,您可以使用自己的用户类。但我建议使用s2 quickstart命令生成User和Role类,并将您的个人代码添加到此类中。这是最简单的方法。我刚刚安装了s2 spring secutity,接下来我应该运行这个命令“grails s2 quickstart org.example SecUser SecRole”…但是我没有得到应该在SecUser n SecRole place n org中给出的内容。example应该是我的应用程序名还是什么?如果我使用@Secured(['ROLE\u USER'])我正在获取插件生成的登录页面。但我没有获取其流量。当我输入用户域类的用户名和密码时,它没有登录。我收到错误消息,因为没有用户。请不要发布两次完全相同的问题。使用Spring安全插件,不要尝试扮演自己的角色。
grails.plugins.springsecurity.controllerAnnotations.staticRules = [
   '/js/admin/**': ['ROLE_ADMIN'],
   '/someplugin/**': ['ROLE_ADMIN']
]
 def authenticateService

...
def action{
     def user = authenticateService.principal() 
     def username = user?.getUsername()
...
@Secured(['ROLE_USER'])
def search = {

}