Search code examples
javatomcatgrailsgroovy

Grails: Allow only specific user like in Java Spark


As you can see the question above, I am looking for an authentification process for grails only for allowed users.

I have some experience in Java with Apache Spark, where you can use the function "before" and there you can check, whether the user is allowed to visit a website or not.

But since I am new in the grails world, I think it is a little bit different.

So my question is: What can I use in grails that has the same function like in Java Spark?

What I real want is, that only some specific user can access the website, and the other not allowed users should be welcomed with an "You are not allowed to visit this website."-page. The server, where my website is, works with Tomcat 7.0

I hope you can help me. I would also appreciate a code example, if possible.

Thanks in advance!


Solution

  • You can use Spring Security Core plugin. When you install you would be able to use Spring Secured annotations.

    Then you could check if user has privilege to see page like this:

    class IndexController{
    
        @Secured(closure = {
            authentication.name == 'admin'
        })
        def admin(){
            render("Admin things")
        }
    
    }
    

    If user is not allowed to see page then he would be redirected to "access denied" page. This page is configurable, please have a look at docs.