我正在第一次使用Grails应用程序,现在我想保护一些页面只能被管理员查看,并给其他用户一些权限。grails中的访问控制,角色和权限
我为grails使用Apache Shiro插件。
在引导我的示例代码如下所示
class BootStrap {
def init = { servletContext ->
def adminRole
if(ShiroRole.findByName("Admin".isEmpty())){
adminRole = new ShiroRole(name: "Administrator")
adminRole.addToPermissions("*:*")
adminRole.addToPermissions("admin")
adminRole.save()
// '用户' 目前已拥有管理员权限 }
if (ShiroUser.findAllByUsername("user").isEmpty()) {
def user = new ShiroUser(username: "user", passwordHash: new Sha256Hash("pass").toHex())
user.addToPermissions("*:*")
user.addToRoles(adminRole)
user.save()
}
if (ShiroUser.findAllByUsername("Guest").isEmpty()) {
def user = new ShiroUser(username: "Guest", passwordHash: new Sha256Hash("pass").toHex())
user.addToPermissions("inventory:*")
user.save()
}
}
def destroy = {
}
}
我ShiroSecurityFilters看起来像
class ShiroSecurityFilters {
def filters = {
all(uri: "/**") {
before = {
// Ignore direct views (e.g. the default main index page).
if (!controllerName) return true
// Access control by convention.
accessControl()
}
}
}
}
我想给“客人”访问库存只有脚手架。然而,在我的应用程序中,一旦用户“访客”登录到其能够访问其他控制器,但我不希望发生这种情况。我感谢您的帮助。
如果有更好的使用Shiro角色,访问控制和/或权限,请让我知道它。
谢谢