javaEE shiro框架,权限控制。ehcache缓存用户权限数据

shiro框架的Jar包下载:https://pan.baidu.com/s/1GCeHV_5T4uP2rGZfUEwl3A  密码:bgr0

导入shiro框架的jar包和ehcache的jar包。

src/ehcache.xml(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>

src/applicationContext.xml(Spring配置文件,注册ehcache缓存管理器并注入shiro安全管理器):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xmlns:soap="http://cxf.apache.org/bindings/soap"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop.xsd
						http://www.springframework.org/schema/tx 
						http://www.springframework.org/schema/tx/spring-tx.xsd
						http://cxf.apache.org/bindings/soap 
						http://cxf.apache.org/schemas/configuration/soap.xsd
						http://cxf.apache.org/jaxws 
						http://cxf.apache.org/schemas/jaxws.xsd
						">
		
		
	<!-- 配置shiro框架的过滤器工厂对象。"shiroFilter"要和web.xml中配置的过滤器名保持相同 -->
	<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
		<!-- 注入安全管理器对象 -->
		<property name="securityManager" ref="securityManager"/>
		<!-- 注入访问相关页面的URL -->
		<property name="loginUrl" value="/login.jsp"/>
		<property name="successUrl" value="/index.jsp"/>
		<property name="unauthorizedUrl" value="/unauthorized.jsp"/>  <!-- 权限不足的错误提示页 -->
		<!--注入URL拦截规则 -->
		<property name="filterChainDefinitions">
			<value>
				/css/** = anon   <!-- anon是过滤器的别名(简称)。 两个*表示递归所有层子目录 -->
				/js/** = anon    <!-- 过滤器有次序之分,依次匹配过滤器 -->
				/images/** = anon
				/validatecode.jsp* = anon
				/login.jsp = anon
				/userAction_login.action = anon
				/page_base_staff.action = perms["staff-list"]  <!-- 必须先认证(登录)后,才会进行授权(权限分配)。"staff-list"是自定义的权限名 -->
				/* = authc    <!-- authc表示是否已认证(已登录) -->
			</value>
		</property>
	</bean>
	
	<!-- 注册shiro安全管理器对象 -->
	<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
		<property name="realm" ref="bosRealm"/>
		<!-- 注入ehcache缓存管理器 -->
		<property name="cacheManager" ref="cacheManager"/>
	</bean>
	
	<!-- 注册ehcache缓存管理器 -->
	<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
		<!-- 注入ehcache的配置文件 -->
		<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
	</bean>
	
	<!-- 注册realm -->
	<bean id="bosRealm" class="com.xxx.bos.realm.BOSRealm">
	</bean>
	
	<!-- 开启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"/>
</beans>

猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/82733930