打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用Shiro的JdbcRealm做权限简单实现
     新接一个项目,着急使用权限功能,就先没写Realm扩展类直接使用JdbcRealm了。已经建好了5个表:用户,角色,权限,和两个中间表,重写了JdbcRealm的三个Query属性:authenticationQuery,userRolesQuery,permissionsQuery。配置时还要把permissionsLookupEnabled属性设置为true,否则permissionsQuery不能执行,将会查不到权限表里的数据。
Java代码  
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.        xmlns:util="http://www.springframework.org/schema/util"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xsi:schemaLocation="  
  6.        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd  
  8.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
  9.   
  10.     <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
  11.         <property name="realm" ref="jdbcRealm"/>  
  12.     </bean>  
  13.   
  14.     <bean id="jdbcRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">  
  15.         <property name="dataSource" ref="dataSource"/>  
  16.         <property name="permissionsLookupEnabled" value="true"/>  
  17.         <property name="authenticationQuery" value="select password from rms_user where login_name = ?" />  
  18.         <property name="userRolesQuery" value="select r.role_code from rms_role r,rms_user u,rms_user_role ur where r.id = ur.role_id and u.id = ur.user_id and u.login_name = ?"/>  
  19.         <property name="permissionsQuery" value="select distinct p.priv_code from rms_privilege p,rms_role r,rms_role_privilege rp where p.id = rp.priv_id and r.id = rp.role_id and r.role_code = ?"/>  
  20.     </bean>  
  21.       
  22.     <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>  
  23.   
  24.     <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
  25.         <property name="securityManager" ref="securityManager"/>  
  26.         <property name="loginUrl" value="/login"/>  
  27.         <property name="successUrl" value="/main"/>  
  28.         <property name="unauthorizedUrl" value="/login"/>  
  29.         <property name="filterChainDefinitions">  
  30.             <value>  
  31.                 /login = authc  
  32.                 /logout = logout  
  33.                 /static/** = anon  
  34.                 /** = authc  
  35.             </value>  
  36.         </property>  
  37.     </bean>  
  38. </beans>  

进入页面后,权限表中priv_code有"user:view"数据,所对应的角色和用户,就能看见Shiro标签里的内容。
Java代码  
  1. <shiro:user>  
  2. <shiro:hasPermission name="user:view">  
  3. <div>用户管理</div>  
  4. </shiro:hasPermission>  
  5. </shiro:user>  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
实战,通过复写shiro的SessionDAO来实现将session保存到redis集群中
CAS和Shiro在spring中集成
Apache shiro集群实现 (六)分布式集群系统下的高可用session解决方案
Java框架搭建
spring4.3
xml、schema、xsd
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服