Redirect page based on userlogin type

前端 未结 2 980
长发绾君心
长发绾君心 2021-01-26 05:05

Upon successful user login i want the page to redirect to /person/personCreate and it did work after adding the following code to Config.groovy.

<
相关标签:
2条回答
  • 2021-01-26 05:52

    In the spring security plugin, you'll find a LoginController.groovy.template and an auth.gsp.template.

    You should copy those into your project and then modify the def auth() method to look something like this:

    /**
     * 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]
    }
    
    0 讨论(0)
  • 2021-01-26 05:52

    It seems that you're distinguishing between super humans and regular humans based on an attribute of the User class. As you're using spring security, a much more natural way to make this distinction is to define these as roles (AKA authorities) and assign them to each user as appropriate.

    You can then implement your post-login redirection action like this:

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

    By the way, in your question, you said that personCreate is the action that should handle the redirection logic, and this is also that action that human's should be redirected to. This will create an infinite loop of redirection for human's, which is why I've redirected humans to a different action in the controller above.

    0 讨论(0)
提交回复
热议问题