标签:
SHIRO在UMS的使用
一:引用LIB包
二:建立表关系
1,创建用户,角色,权限及用户角色表,角色权限表;
2,在权限表中加一字段(如:do,;例:notice:list_view);
3,在对应的Controller的方法上加上“ @RequiresPermissions(value = { "notice:list_view" })”;
三:web.Xml的配置
<!-- shiro的filter-->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilt erProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- shiro的filter-mapping-->
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
四:Spring的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login" />
<!-- 用户访问未对其授权的资源时,所显示的连接 -->
<property name="unauthorizedUrl" value="/error/unauthorized" />
<property name="filterChainDefinitions">
<value>
/system/**=authc
</value>
</property>
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean>
<bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.Md5CredentialsMatcher"></bean>
<!-- 缓存管理 -->
<bean id="shiroCacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"></bean>
<bean id="jdbcRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">
<property name="permissionsLookupEnabled" value="true"></property>
<property name="dataSource" ref="ums_dataSource"></property>
<property name="authenticationQuery"
value="select password from t_user where username = ?" />
<property name="userRolesQuery"
value="select b.`name` from t_user_role a left join t_role b on a.role_id=b.id left join t_user c on a.user_id = c.id where username= ?" />
<property name="permissionsQuery"
value="select c.activity from t_role_permission a left join t_role b on a.role_id = b.id left join t_permission c on a.permission_id =c.id where b.name=?" />
</bean>
<!-- Shiro安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="jdbcRealm"></property>
<property name="cacheManager" ref="shiroCacheManager"></property>
</bean>
</beans>
五:退出登录时清空缓存(防止授权不刷新)
在用户退出方法中添加以下代码:
Subject subject = SecurityUtils.getSubject();
subject.logout();
标签:
原文地址:http://www.cnblogs.com/ai211234/p/5620754.html