假设你的 BuildConfig 中有这样一行:
compile ":spring-security-core:2.0-RC4"
在您的 BootStrap 中还有一些类似的代码:
def roleAdmin = new Role(authority:LSSRole.ROLE_ADMIN.toString()).save(failOnError: true)
def roleFirm = new Role(authority:LSSRole.ROLE_FIRM.toString()).save(failOnError: true)
def roleLaw = new Role(authority:LSSRole.ROLE_LAWYER.toString()).save(failOnError: true)
def roleFin = new Role(authority:LSSRole.ROLE_FINANCE.toString()).save(failOnError: true)
使用此代码创建的示例管理员用户:
UserRole.create admin, roleAdmin, true
还有一些像这样在 Config 中的代码:
'/dbconsole/**': [LSSRole.ROLE_ADMIN.toString()],
'/secure/**': [LSSRole.ROLE_ADMIN.toString()],
'/payment/**': [LSSRole.ROLE_FIRM.toString()],
'/filing/**': [LSSRole.ROLE_FIRM.toString()],
'/finance/**': [LSSRole.ROLE_FINANCE.toString()],
'/lawyer/**': [LSSRole.ROLE_LAWYER.toString()],
其中 LSSRole 是一个枚举,一些代码如下:
"/" {
controller = "dispatch"
action = "index"
}
在成功登录后将用户转移到的 UrlMappings 中,您可以构建这样的调度程序,以根据用户的角色将用户分派到不同的登录页面:
class DispatchController {
def index() {
def controller = 'login'
def action = 'auth'
if (SpringSecurityUtils.ifAllGranted(LSSRole.ROLE_ADMIN.toString())) {
controller = 'secure'
action = 'index'
} else if (SpringSecurityUtils.ifAllGranted(LSSRole.ROLE_FINANCE.toString())) {
controller = 'finance'
action = 'index'
} else if (SpringSecurityUtils.ifAllGranted(LSSRole.ROLE_FIRM.toString())) {
controller = 'filing'
action = 'summary'
} else if (SpringSecurityUtils.ifAllGranted(LSSRole.ROLE_LAWYER.toString())) {
controller = 'lawyer'
action = 'index'
} else {
flash.message = 'Where do you think you\'re going? Nno no no'
SecurityContextHolder.clearContext()
}
redirect controller:controller, action:action
}
希望这会有所帮助。