码迷,mamicode.com
首页 > 其他好文 > 详细

grails-shiro权限认证

时间:2014-12-26 11:08:08      阅读:345      评论:0      收藏:0      [点我收藏+]

标签:

一.引用shiro插件

//在BuildConfig的plugins下面添加
compile ":shiro:1.2.1"

二.引用新插件后要进行编译

//grails命令
compile

三.生成脚手架文件

//grials命令 , 要注意的是后面的那个点,否则生成好的文件会混乱
shiro-quick-start --prefix=com.security.

四.配置Bootstrap.groovy

class BootStrap {

    def shiroSecurityService

    def init = { servletContext ->
        // Create the admin role
        def adminRole = Role.findByName(‘ROLE_ADMIN‘) ?:
                new Role(name: ‘ROLE_ADMIN‘).save(flush: true, failOnError: true)

        // Create the user role
        def userRole = Role.findByName(‘ROLE_USER‘) ?:
                new Role(name: ‘ROLE_USER‘).save(flush: true, failOnError: true)

        // Create an admin user
        def adminUser = User.findByUsername(‘admin‘) ?:
                new User(username: "admin",
                passwordHash: shiroSecurityService.encodePassword(‘password‘))
                .save(flush: true, failOnError: true)

        // Add roles to the admin user
        assert adminUser.addToRoles(adminRole)
        .addToRoles(userRole)
        .save(flush: true, failOnError: true)

        // Create an standard user
        def standardUser = User.findByUsername(‘joe‘) ?:
                new User(username: "joe",
                passwordHash: shiroSecurityService.encodePassword(‘password‘))
                .save(flush: true, failOnError: true)

        // Add role to the standard user
        assert standardUser.addToRoles(userRole)
        .save(flush: true, failOnError: true)

    }
    def destroy = {
    }
}

五.增加一个Controller

package com.security

class HomeController {

    def index() {
        render ("此页面不需要登陆")
    }
    def secured() {
        render ("此页面需要用户或者管理员登陆")
    }
    def admin() {
        render ("此页面需要管理员登陆")
    }
}

六.修改com.security.SecurityFilters.groovy

package com.security

/**
 * Generated by the Shiro plugin. This filters class protects all URLs
 * via access control by convention.
 */
class SecurityFilters {
    def filters = {
        //1.role_admin
        home(controller: "home",action: "admin"){
            before = {
                accessControl{
                    role("ROLE_ADMIN");
                }
            }
        }
        //2.role_user
        home_securied(controller: "home",action: "secured"){
            before = {
                accessControl{
                    role("ROLE_USER");
                }
            }
        }
    }
}

这里使用的是shiroPlugin提供的accessControl,role方法会划横线,这里是不影响程序运行的,

使用 role( …… ),验证访问对象是否具有相应的角色;

使用 permission( …… ),验证访问对象是否具有相应的 Permission。

这里没有使用shiro的Tag但是也做一点称述

下是经常使用到的 Tag:

  • principal,输出当前用户的标识
  • hasRole,判断当前用户是否属于给定的角色,参数:name
  • hasPermission, 判断当前用户是否具有指定的权限,参数:type,action 或者 permission
  • isLoggedIn,判断当前用户是否已经登录
  • hasAnyRole,判断当前用户是否属于给定的某个角色,参数:in

使用方式

<shiro:hasPermission permission="home:index,admin"> 
   <span class="button"> 
 <g:actionSubmit class="edit" value="Edit" /> 
 </span> 
   <span class="button"> 
 <g:actionSubmit class="delete" 
 onclick="return confirm(‘Are you sure?‘);" 
 value="Delete" /> 
 </span> 
 </shiro:hasPermission>

 

grails-shiro权限认证

标签:

原文地址:http://www.cnblogs.com/duwenlei/p/4186181.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!