根据用户登录类型重定向页面

时间:2014-08-12 03:08:41

标签: grails groovy

用户登录成功后,我希望页面重定向到/person/personCreate,并在将以下代码添加到Config.groovy后确实有效。

grails.plugin.springsecurity.successHandler.defaultTargetUrl = "/person/personCreate"

现在,我有两种类型的用户SuperHumanHuman。如果usertype是SuperHuman我想要重定向页面/person/superHumanPage,或者如果用户类型是Human,我希望将页面重定向到/person/personCreate

我怎样才能完成这项工作?

更新

enter image description here

2 个答案:

答案 0 :(得分:0)

在spring安全插件中,您将找到LoginController.groovy.template和auth.gsp.template。

您应该将这些复制到项目中,然后修改def auth()方法,看起来像这样:

/**
 * Show the login page.
 */
def auth = {
    def config = SpringSecurityUtils.securityConfig

    if (springSecurityService.isLoggedIn()) {
        def currentUser = springSecurityService.currentUser
        if (currentUser.userType.human)) {
            redirect controller: 'human'
        }
        else if (currentUser.userType.superHuman) {
            redirect controller: 'superHuman'
        }
        else {
            redirect uri: config.successHandler.defaultTargetUrl
        }

        return
    }

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

答案 1 :(得分:0)

根据User类的属性,您似乎在区分超级人类和普通人类。当您使用Spring安全性时,更自然的方法是将这些区分定义为角色(AKA权限)并将其分配给每个用户。

然后,您可以实现这样的登录后重定向操作:

import grails.plugin.springsecurity.SpringSecurityUtils

class PersonController {

  def personCreate() {
    if (SpringSecurityUtils.ifAllGranted('SUPER_HUMAN')) {
      redirect action: 'superHumanHome'  
    } else {
      redirect action: 'humanHome'
    }
  }

  def superHumanHome() {
    // show the super human's home page
  }

  def humanHome() {
    // show the human's home page
  }
}

顺便提一下,在你的问题中,你说personCreate是应该处理重定向逻辑的动作,这也是人类应该被重定向到的动作。这将为人类创建一个无限循环的重定向,这就是为什么我将人类重定向到上面控制器中的不同动作。