Search code examples
javaspringspring-bootgrailsgroovy

Is there a way to prevent request from reaching the action in grails using filters


I am trying to prevent the request from reaching the grails action ("/$controller/$action?/$id") when token is not valid so that it returns a JSON response back to the client without invoking the action

i tried adding return statement to the code but still the request invokes the action. I am using java 7 and an older version of grails (grails 2.4.3)

        class ShiroSecurityFilters {
        def filters = {
            vueJsFilter(uri: '/api/**'){
                def userObject = null
                def response_data = null
                before = {
                    def authorization_header = request.getHeader("Authorization")
                    if (authorization_header){
                        def authorization = authorization_header.split(' ')[1]
                        userObject = Collaborateur.findByAccessToken(authorization)
                        if (userObject){
                            println "Welcome Back, $userObject"
                        }else{
                            response_data = [
                                title: "filter token error"
                            ]
                            response.status = 401
                            render(response_data as JSON)
                        }
                    }
                }
            }
        }
    }

I expect the filter to stop the request from executing the action or invoking it, and instead send a response back to the client and not send request to next resource in this case the action, i did some research online but i didn't find anything, any help would be appreciated


Solution

  • You have to return false from a before filter to prevent the action from being invoked. In your source code above, the filter doesn't return anything.