1

I have a web application that contains a REST api that is interacted with by the client application that lives at /. When a user session times out and the client js application makes a request to the REST api, that request will trigger a login event. Once the user logs in the user is then taken to the REST api endpoint which just displays a JSON response. I would like to hard wire the login page to always redirect to /.

Edit 1: I'm using spring-security-core plugin with the openid plugin as well and grails 2.0.4.

Edit 2: So I managed to get a solution working, however its a bit crude and I would like to know if there is a more elegant solution out there.

I created a simple filter in grails-app/conf/LoginRedirectFilters.groovy:

class RedirectLoginFilters {
    def filters = {
        redirectAfterLogin (uri: '/api/*') {
            before = {
                if (session["LOGGING_IN"])
                    redirect uri: "/"
            }
            after = {
                if (session["LOGGING_IN"])
                    session["LOGGING_IN"] = false
            }
        }
    }
}

And in my auth function inside of OpenIdController.groovy I added the session flag LOGGING_IN:

def auth = {                                                                                     

    def config = SpringSecurityUtils.securityConfig                                              

    if (springSecurityService.isLoggedIn()) {                                                    
        redirect uri: config.successHandler.defaultTargetUrl                                     
        return                                                                                   
    }                                                                                            

    session["LOGGING_IN"] = true
    .
    .
    .

This is working properly by only checking if the LOGGING_IN flag is true when an api endpoint is called, and it also kills the flag after one request so it won't interfere with subsequent client api calls. I realize this is pretty convoluted, so if there is a better solution please let me know, thanks!

4

2 回答 2

0

为什么不能在过滤器中也使用相同的“springSecurityService.isLoggedIn()”?我很确定这也有效。

于 2012-08-15T05:14:30.063 回答
0

我想我完全误解了你想要实现的目标,但是如果你总是想在成功登录后重定向到“/”,只需在你的中设置这些属性Config.groovy

grails.plugins.springsecurity.successHandler.alwaysUseDefault = true
grails.plugins.springsecurity.successHandler.defaultTargetUrl = "/"

如果您想要对此采取不同的行为,则必须充实您的问题。

于 2012-08-20T09:18:33.363 回答