Search code examples
cssgrailsgrails-plugin

Grails filter disables style/css


I made a security filter, following the book "Beginning Grails, Groovy and Griffon".

Looks like the functionality works ok, but, for some reason, the only page that the filter allows to open without a logged in user, is lacking any style. Just textfields, labels and buttons. Also the simple capthca plugin works and the captcha image is loaded ok.

Here's the page screenshot:

enter image description here

package collab.todo

class SecurityFilters {

def filters = {
    securityFilters(controller:'*', action:'*') {
        before = {
            if(!session.user && 
                (!controllerName.equals("user") && !actionName.equals("login") 
                    && !actionName.equals("captcha")
                     &&!actionName.equals("register")) ){
                redirect(controller: "user", action: "login")
            }
        }
        after = { Map model ->

        }
        afterView = { Exception e ->

        }
    }
}

}


Solution

  • The reason why your page isn't displaying any assets (styles or images) is because in recent versions of Grails (2.3+) assets are served up by the asset pipeline plugin from the /assets/** URL.

    Previous versions of Grails (for which your book was likely written) didn't use this plugin and as a result those assets weren't filterable.

    To fix this issue you can use the exclusion properties of the filter (e.g. uriExclude). You can read more about it in the excellent Grails documentation.

    Here is an example of your filter with uriExclude:

    package collab.todo
    
    class SecurityFilters {
    
    def filters = {
        securityFilters(controller:'*', action:'*', uriExclude: '/assets/**') {
            before = {
                if(!session.user && 
                    (!controllerName.equals("user") && !actionName.equals("login") 
                        && !actionName.equals("captcha")
                         &&!actionName.equals("register")) ){
                    redirect(controller: "user", action: "login")
                }
            }
            after = { Map model ->
    
            }
            afterView = { Exception e ->
    
            }
        }
    }
    }
    

    This change will exclude applying your filter to anything under the /assets/ URI, and your styles/images should now appear.