访问控制,角色和权限在Grails中 [英] access control, role and permission in grails

查看:145
本文介绍了访问控制,角色和权限在Grails中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我第一次使用grails应用程序,现在我想保护一些页面只能由管理员查看,并给予其他用户一些权限。 p>我为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()

//'user'现在拥有所有管理员权限
}

 <$ c $ ()用户名,密码哈希:新的Sha256Hash(pass)。toHex())$ b(用户名:user $ b 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 = {
}

}



My ShiroSecurityFilters看起来像

  class ShiroSecurityFilters {
def filters = {
all(uri:/ **){
before = {
//忽略直接视图(例如,默认的主索引页面)。
if(!controllerName)返回true

//按照惯例访问控制。
accessControl()

}
}
}



}



我只想授予访客访问广告资源脚手架的权限。然而,在我的应用程序中,一旦用户访客登录到其能够访问其他控制器,但我不希望发生这种情况。感谢您的帮助。



如果使用Shiro角色,访问控制和/或权限更好,请让我知道。



谢谢

解决方案

好的。让我们来看看......

在开始处有一个错字:

 Admin.isEmpty()

永远都是假的......我想你没有定义假的角色...



你正在寻找管理员,但创建管理员...


$

  adminRole.save(flush:true,failOnError:true)
code>

而不是 adminRole.save()。这将确保对象真正被保存。



Administrator 已经具有所有权限(*:*)和admin不是典型的shiro权限,所以您可以放下这行...( adminRole.addToPermissions(admin)



如果你做了一个

  user.addToRoles(adminRole)

您不需要添加*:*权限。 我已经创建了一个测试项目,安装了shiro,做了一个 create-auth-controller ,a create-wildcard-realm create-filters ShiroSecurity



通过在Config.groovy中将以下两行添加到log4j配置中,为BootStrap和Shiro-Realm激活日志记录:

  debug'grails.app.conf.BootStrap'
debug'grails.app.realm'

这是我的BootStrap.groovy :(有趣的部分)

  def init = {servletContext  - > 
def adminRole
$ b $ if(ShiroRole.findByName(Administrator)== null){
adminRole =新ShiroRole(名称:Administrator)
adminRole。 addToPermissions(*:*)
adminRole.save(flush:true,failOnError:true)
log.debug adminRole.dump()
}
println ShiroUser.findAllByUsername user)。dump()
log.debug=* 80
if(ShiroUser.findAllByUsername(user)。isEmpty()){
def user = new ShiroUser username:user,passwordHash:new Sha256Hash(pass)。toHex())
user.addToRoles(adminRole)
user.save(flush:true,failOnError:true)
log.debug user.dump()
}

if(ShiroUser.findAllByUsername(Guest)。isEmpty()){
def user = new ShiroUser(username: Guest,passwordHash:新的Sha256Hash(pass).toHex())
user.addToPermissions(inventory:*)
user.save(flush:true,failOnError:true)
log.debug user.dum p()
}

}

和我的ShiroSecurityFilters。 groovy:

  def filters = {
all(controller:'*',action:'*'){
之前= {
//忽略直接视图(例如,默认的主索引页面)。
if(!controllerName)返回true

//按照惯例访问控制。
accessControl()

}
}
}

,它的工作原理......



正如您所看到的,我的SecurityFilters基于控制器和操作...只是我的偏好... p>

但是我想你的问题只是基于错误的引导。当你使用shiro时,记录功能非常有用...

I am working on a grails application for the first time and I now want to protect some pages to be viewed only by admins, and give some permissions to other users.

I am using Apache Shiro plugin for grails.

My sample code in the bootstrap looks like this

class BootStrap {

def init = { servletContext ->
    def adminRole

    if(ShiroRole.findByName("Admin".isEmpty())){
        adminRole = new ShiroRole(name: "Administrator")
        adminRole.addToPermissions("*:*")
        adminRole.addToPermissions("admin")

        adminRole.save()

// 'user' now has all administrator rights }

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

}

My ShiroSecurityFilters looks like

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()

        }
    }
}

}

I wanted to give to "Guest" access to inventory scaffold only. However in my application once the user "Guest" logged in its able to access other controllers butI don't want that to happen. I appreciate your help.

If there is an better of using Shiro role, access control and/or permissions, please let me know about it.

Thank you

解决方案

OK. let's see...

there is a typo right at the start:

"Admin".isEmpty()

will always be false... and I guess you have no role "false" defined...

And you are looking for "Admin" but create "Administrator"...

Do a

adminRole.save(flush:true, failOnError:true)

instead of adminRole.save(). This will make sure that the object is really saved.

The role Administrator already has all permissions ("*:*") and "admin" is not a typical shiro permission, so you can drop this line... (adminRole.addToPermissions("admin"))

If you do a

user.addToRoles(adminRole)

you don't need to add the "*:*"permission. The role is already enough.

I've now created a test project, installed shiro, did a create-auth-controller, a create-wildcard-realm and a create-filters ShiroSecurity.

Activate logging for BootStrap and Shiro-Realm by adding following two lines to the log4j config in Config.groovy:

debug   'grails.app.conf.BootStrap'
debug   'grails.app.realm'

Here is my BootStrap.groovy: (the interesting part)

def init = { servletContext ->
    def adminRole

    if(ShiroRole.findByName("Administrator")==null){
        adminRole = new ShiroRole(name: "Administrator")
        adminRole.addToPermissions("*:*")
        adminRole.save(flush:true, failOnError:true)
        log.debug adminRole.dump()
    }
    println ShiroUser.findAllByUsername("user").dump()
    log.debug "="*80
    if (ShiroUser.findAllByUsername("user").isEmpty()) {
        def user = new ShiroUser(username: "user", passwordHash: new Sha256Hash("pass").toHex())
        user.addToRoles(adminRole)
        user.save(flush:true, failOnError:true)
        log.debug user.dump()
    }

    if (ShiroUser.findAllByUsername("Guest").isEmpty()) {
        def user = new ShiroUser(username: "Guest", passwordHash: new Sha256Hash("pass").toHex())
        user.addToPermissions("inventory:*")
        user.save(flush:true, failOnError:true)
        log.debug user.dump()
    }

}

and my ShiroSecurityFilters.groovy:

def filters = {
    all(controller:'*', action:'*') {
        before = {
        // Ignore direct views (e.g. the default main index page).
        if (!controllerName) return true

        // Access control by convention.
        accessControl()

        }
    }
}

and it works...

As you can see, my SecurityFilters are based on controller and action... just my preference...

But I guess your problem was only based on the wrong bootstrap. Logging is very useful when you work with shiro...

这篇关于访问控制,角色和权限在Grails中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆