Grails 基于用户登录类型重定向页面

Grails 基于用户登录类型重定向页面,grails,groovy,Grails,Groovy,在用户成功登录后,我希望页面重定向到/person/personCreate,在将以下代码添加到Config.groovy后,它确实起了作用 现在,我有两种类型的用户:超人和人。如果用户类型是SuperHuman,我希望页面重定向到/person/superHumanPage,或者如果用户类型是Human,我希望页面重定向到/person/personCreate 我怎样才能做到这一点 更新 在spring安全插件中,您将找到LoginController.groovy.template和aut

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

现在,我有两种类型的用户:超人和人。如果用户类型是SuperHuman,我希望页面重定向到/person/superHumanPage,或者如果用户类型是Human,我希望页面重定向到/person/personCreate

我怎样才能做到这一点

更新


在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]
}
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
  }
}

看起来您是在根据用户类的属性区分超人和普通人。当您使用SpringSecurity时,一种更自然的区分方式是将这些角色定义为权限,并根据需要将它们分配给每个用户

然后,您可以执行登录后重定向操作,如下所示:

/**
 * 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]
}
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是应该处理重定向逻辑的操作,这也是人类应该重定向到的操作。这将为用户创建一个无限的重定向循环,这就是为什么我在上面的控制器中将用户重定向到不同的操作。

WH\n您所说的InstaceOf是什么意思?我在用户模型中有一个名为userType的字段,其中一些用户是人类,一些是超人。如何完成此操作?currentUser返回域对象,以便您可以检查所需的任何字段。在我们的应用程序中,我们有不同的子类型的用户,所以我的示例使用instanceOf来区分,而不是使用类型字段。我已经编辑了,只是检查了用户类型。我在复制LoginController.groovy时更新了我的帖子。我收到了上面的错误,请参见上面的更新帖子。我实际上是从你使用的插件版本2.0-RC4复制粘贴的代码?如果没有,您需要从您正在使用的任何版本复制该版本。您提供的链接是针对可能未发布的最新版本的。我使用的是2.0-RC2,但找不到LoginController.groovy。我甚至在谷歌上搜索了源代码,但它不可用。从技术上讲,这需要两个重定向,这不是什么大问题。通常,如果您想控制更多关于登录过程的信息,那么重写LoginController是一种内置的方法,并且工作得很好。我同意你的观点,人类/超人的东西更多的是一种角色区别,而不是一种类型。我使用的是spring安全核心:2.0-RC2。SpringSecurityUtils的重要意义是什么?我已将其添加到我的帖子中