zoukankan      html  css  js  c++  java
  • grails-shiro权限认证

    一.引用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>
    如果有使用请标明来源:http://www.cnblogs.com/duwenlei/
  • 相关阅读:
    ApacheCN JavaScript 译文集 20211122 更新
    ApacheCN jQuery 译文集 20211121 更新
    ApacheCN 大数据译文集(二) 20211206 更新
    我这里一直空荡荡的,还占着博客园的位置,真是不好意思
    2009年最后一天
    学习petshop的朋友。这里有一些讲坐下载
    Repeater控件中如何做编辑和删除功能
    可以让你少奋斗十年的工作经验
    GridView自定义分页,模仿GridView的自动分页功能
    从内容面访问母版页中的控件或属性
  • 原文地址:https://www.cnblogs.com/duwenlei/p/4186181.html
Copyright © 2011-2022 走看看