springSecurityService.getCurrentUser()返回错误没有这样的属性:类的id:java.lang.String

时间:2014-06-12 13:21:06

标签: grails spring-security

我尝试将此代码添加到AdminLoginController以检查当前用户的角色。

def checkRole() {

        User user = springSecurityService.getCurrentUser()

        if(User.getRoleByUser(user.username).contains(Constants.ROLE_ADMIN)){
            redirect uri:'/admin'
        }else if(User.getRoleByUser(user.username).contains(Constants.ROLE_TWO)) {
            redirect uri:'/two'
        }else if(User.getRoleByUSer(user.username).contains(Constants.ROLE_THREE)){
            redirect uri: '/three'
        }else{

        }   
    }

当我尝试运行该应用程序时,它显示以下错误:

 groovy.lang.MissingPropertyException: No such property: id for class: java.lang.String

错误在User user = springSecurityService.getCurrentUser()

1 个答案:

答案 0 :(得分:1)

if (springSecurityService.isLoggedIn()) {

        User user = securityService.getCurrentUser()  //Note:- i used securityService not springSecurityService 

        List<Role> currentUserRoles = UserRole.findByUser(user).collect { it.role } as List

        if(currentUserRoles.contains(Role.findByName(Constants.ROLE_ONE))) {

            redirect(controller:'admin', action:'index')

        } else if(currentUserRoles.contains(Role.findByName(Constants.ROLE_TWO))) {

            redirect(controller:'two', action:'action')

        } else if(currentUserRoles.contains(Role.findByName(Constants.ROLE_THREE))) {

            redirect(controller:'three', action:'action')
        }

    } else {

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

注意: - userRole域类链接角色和用户。