How do I define redirects in Grails properly?

366 Views Asked by At

In my grails app I use interceptors for checking if a user is logged in or not. If not, the user should be redirected to the login page (/account/index). This does not work for some reason. (Additional info: I didn't define namespaces for my controllers.)

The relevant code snippets from my application:

class UrlMappings {

static mappings = {
    "/$controller/$action?/$id?(.$format)?"{
        constraints {
            // apply constraints here
        }
    }

    "/"(controller: "account", action:"index")
    "500"(view:'/error')
    "404"(view:'/notFound')
  }
}

In application.yml I have set:

grails:
    profile: web
    cors:
        enabled: true
    codegen:
        defaultPackage: myapp
    gorm:
        reactor:
            # Whether to translate GORM events into Reactor events
            # Disabled by default for performance reasons
            events: false
spring:
    jmx:
        unique-names: true
    main:
        banner-mode: "off"
    groovy:
        template:
            check-template-location: false
    devtools:
        restart:
            additional-exclude:
                - '*.gsp'
                - '**/*.gsp'
                - '*.gson'
                - '**/*.gson'
                - 'logback.groovy'
                - '*.properties'
                - 'grails-app/views/**'
                - 'grails-app/i18n/**'
                - 'grails-app/conf/**'
management:
    endpoints:
        enabled-by-default: false

but setting the cors to enabled or removing these lines didn't make any difference.

The redirect in an interceptor (I've got one interceptor for each controller, I think that's the way to go), looks like this:

class ZoneInterceptor {

    boolean before() {
        Boolean allowed = session?.user != null
        if (!allowed) {
            redirect(controller: "account", action: "index")
            return false
        }
        return allowed
    }

    boolean after() { true }

    void afterView() {
        // no-op
    }
}

This code gives me a funky message: the browser shows a popup "Open xdg-open? A website wants to open this application" (using the latest chromium and brave browser, both react in the same way, firefox just doesn't do anything. That's why I think the problem is definitely on my side).

How can I solve that? I just want to have a proper redirect, not any xdg-open popup.

Thanks for all your help in advance!

0

There are 0 best solutions below