首页 > 解决方案 > 控制器中的操作后如何精确重定向页面(grails)

问题描述

所以我有注销功能,我想在注销后返回登录页面。我的控制器名为 LoginController.groovy。但是注销后它只刷新页面而不是直接它。顺便说一句,它是由 spring 安全插件创建的。

/**
 * go to login page when successful logout.
 */
def logout() {
    if(request.logout()) // Logout current user
    redirect(controller: 'Login', action: 'index') // Redirect to the login page
}


/**
 * Default action; redirects to 'defaultTargetUrl' if logged in, /login/auth otherwise.
 */
def index() {
    if (springSecurityService.isLoggedIn()) {

        redirect controller:'Login', action:'homepage'

    }
    else {
        redirect action: 'auth', params: params
    }
}


 /**
 * Show the login page.
 */
def auth() {

    def config = SpringSecurityUtils.securityConfig

    if (springSecurityService.isLoggedIn()) {
        //redirect uri: config.successHandler.defaultTargetUrl
        redirect controller:'Login', action:'homepage'
    }

    String view = 'auth'
    String postUrl = "${request.contextPath}${config.apf.filterProcessesUrl}"
    render view: view, model: [postUrl: postUrl,rememberMeParameter: config.rememberMe.parameter]


}

标签: redirectgrailsviewcontroller

解决方案


但是注销后它只刷新页面而不是直接它。顺便说一句,它是由 spring 安全插件创建的。

你有这个:

def logout() {
    if(request.logout()) 
    redirect(controller: 'Login', action: 'index')
}

这相当于:

def logout() {
    if(request.logout()) {
        redirect(controller: 'Login', action: 'index')
    } else {
        render view: 'logout', model: [:]
    }
}

这意味着重定向只会在.logout()返回true(或评估为 Groovy 真相的东西)时发生。


推荐阅读