简体   繁体   中英

Grails security filter all action but except one

I'm gonna create a security filter for my project. I check if !session.user then redirect to action error . Here is my current code:

all(controller: 'accounting|installation|installer|sales|service|serviceOrder|document', action: '*') {
            before = {
                if (!session.user) {
                    redirect(controller: 'installation', action: 'errors')
                    return false
                }
            }
            after = { Map model ->

            }
            afterView = { Exception e ->

            }
        }

However the point is that session.user being created in controller 'installation' and action 'index' . So how can I filter without index action ? Any suggestions will be appreciated. Thanks.

You can use invert:true eg

def filters = {
    allExceptIndex(controller:"installation",action:"index",invert:true) {
        before = {
        }
        after = { Map model ->
        }
        afterView = { Exception e ->
        }
    }
}

For further reference see Blog

Try this

all(controller: 'accounting|installation|installer|sales|service|serviceOrder|document', action: '*') {
        before = {
         if (!(controllerName == 'installation' && actionName == 'index')) {
            if (!session.user) {
                redirect(controller: 'installation', action: 'errors')
                return false
            }
          }
        }
        after = { Map model ->

        }
        afterView = { Exception e ->

        }
    }

Hope I have understood your question ,Since you want to exclude action index then ,try this ..

all(controller: 'accounting|installation|installer|sales|service|serviceOrder|document', action: '*',actionExclude:'index'){....

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM