我尝试将此代码添加到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()
答案 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域类链接角色和用户。