Shiro进行权限控制

来源:互联网 发布:js 按钮显示 隐藏 div 编辑:程序博客网 时间:2024/05/17 04:32

列举Shiro的几种控制权限的方法


一、使用shiro的方法注解方式权限控制

第一步:在spring配置文件中开启shiro注解支持

        

 <!-- 开启shiro框架注解支持 -->          <bean id="defaultAdvisorAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">                       <!-- 必须使用cglib方式为Action对象创建代理对象-->                      <property name="proxyTargetClass" value="true"/>         </bean>        <!-- 配置shiro框架提供的切面类,用于创建代理对象 -->       <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"/>


第二步:在Action的方法上使用shiro注解

@RequiresPermissions("staff-delete")//执行这个方法,需要当前用户具有staff-delete这个权限public String deleteBatch(){staffService.deleteBatch(ids);return LIST;}


二、使用shiro提供的页面标签方式进行权限控制

第一步:在jsp页面中引入shiro的标签库

<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>


第二步:使用shiro的标签控制页面元素展示

<shiro:hasPermission name="staff-delete">{id : 'button-delete',text : '删除',iconCls : 'icon-cancel',handler : doDelete},</shiro:hasPermission>


三、URL拦截权限控制

         <!--注入URL拦截规则 --><property name="filterChainDefinitions"><value>/css/** = anon/js/** = anon/images/** = anon/validatecode.jsp* = anon/login.jsp = anon/userAction_login.action = anon/page_base_staff.action = perms["staff-list"]/* = authc</value></property>

编写Realm中的授权方法

      //授权方法protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();//获取当前登录用户对象User user = (User) SecurityUtils.getSubject().getPrincipal();// 根据当前登录用户查询数据库,获取实际对应的权限List<Function> list = null;if(user.getUsername().equals("admin")){DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Function.class);//超级管理员内置用户,查询所有权限数据list = functionDao.findByCriteria(detachedCriteria);}else{list = functionDao.findFunctionListByUserId(user.getId());}for (Function function : list) {info.addStringPermission(function.getCode());}return info;}

使用ehcache缓存权限数据

第一步:pom.xml文件中引入ehcache的依赖

<!-- 引入ehcache的依赖 --><dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache-core</artifactId><version>2.6.6</version></dependency>

第二步:在项目中提供ehcache的配置文件

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">    <diskStore path="java.io.tmpdir"/>    <defaultCache            maxElementsInMemory="10000"            eternal="false"            timeToIdleSeconds="120"            timeToLiveSeconds="120"            overflowToDisk="true"            maxElementsOnDisk="10000000"            diskPersistent="false"            diskExpiryThreadIntervalSeconds="120"            memoryStoreEvictionPolicy="LRU"            /></ehcache>

第三步:在spring配置文件中配置缓存管理器对象,并注入给安全管理器对象

<!-- 注册安全管理器对象 --><bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><!-- 注入自定义realm --><property name="realm" ref="bosRealm"/><!-- 注入缓存管理器 --><property name="cacheManager" ref="cacheManager"/></bean><!-- 注册缓存管理器 --><bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"><!-- 注入ehcache的配置文件 --><property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/></bean>


铸剑团队签名:

【总监】十二春秋之,3483099@qq.com;

【Master】戈稻不苍,han169@126.com;

【Java开发】雨鸶,343691194@qq.com;思齐骏惠,qiangzhang1227@163.com;小王子,545106057@qq.com;巡山小钻风,840260821@qq.com;

【VS开发】豆点,2268800211@qq.com;

【系统测试】土镜问道,847071279@qq.com;尘子与自由,695187655@qq.com;

【大数据】沙漠绿洲,caozhipan@126.com;张三省,570417591@qq.com;

【网络】夜孤星,11297761@qq.com;

【系统运营】三石头,261453882@qq.com;平凡怪咖,591169003@qq.com;

【容灾备份】秋天的雨,18568921@qq.com;

【安全】保密,你懂的。

原创作者:小王子

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。