您的位置:首页 > 运维架构 > Apache

Apache shiro 登录验证授权管理

2016-05-27 16:40 706 查看
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.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/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/aop/spring-aop.xsd"
default-lazy-init="false">

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer" ref="jackson2JsonRedisSerializer" />
<property name="hashKeySerializer" ref="jackson2JsonRedisSerializer" />
<property name="valueSerializer" ref="jackson2JsonRedisSerializer" />
<property name="hashValueSerializer" ref="jackson2JsonRedisSerializer" />
</bean>

<bean id="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer" />

<bean id="jackson2JsonRedisSerializer"  class="org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer"  >
<constructor-arg type="java.lang.Class" value="java.lang.Object" index="0"/>
</bean>

<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.ip}" />
<property name="port" value="${redis.port}" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxActive}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
</bean>

<bean id="redisManager" class="org.crazycake.shiro.RedisManager">
<property name="host" value="${redis.ip}" />
<property name="port" value="${redis.port}" />
<property name="expire" value="7200" />
<property name="timeout" value="10000" />
</bean>

<bean id="redisCacheManager" class="org.crazycake.shiro.RedisCacheManager">
<property name="redisManager" ref="redisManager" />
</bean>

<bean id="redisSessionDAO" class="org.crazycake.shiro.RedisSessionDAO">
<property name="redisManager" ref="redisManager" />
</bean>

<!-- 权限管理器 -->
<!-- Shiro默认会使用Servlet容器的Session,可通过sessionMode属性来指定使用Shiro原生Session -->
<!-- 即<property name="sessionMode" value="native"/>,详细说明见官方文档 -->
<!-- 这里主要是设置自定义的单Realm应用,若有多个Realm,可使用'realms'属性代替 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 数据库认证的实现 com.winwho.common.shiro.ShiroDbRealm -->
<property name="realm" ref="shiroDbRealm" />
<!-- session 管理器 -->
<!--<property name="sessionManager" ref="sessionManager" />-->
<!-- 缓存管理器 -->
<!--<property name="cacheManager" ref="shiroCacheManager" />-->

<!-- session 管理器 -->
<property name="sessionManager" ref="sessionManager" />
<!-- 缓存管理器 -->
<property name="cacheManager" ref="redisCacheManager" />
</bean>

<!-- session管理器 -->
<bean id="sessionManager"
class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<!-- 超时时间 -->
<property name="globalSessionTimeout" value="7200000" />
<!-- session存储的实现 -->
<!--<property name="sessionDAO" ref="shiroSessionDao" /> -->

<!-- session存储的实现 -->
<property name="sessionDAO" ref="redisSessionDAO" />
<!-- 定时检查失效的session -->
<property name="sessionValidationSchedulerEnabled" value="true" />
</bean>

<bean id="kickoutSessionControlFilter" class="cn.com.che.util.shiro.KickoutSessionControlFilter">
<property name="cacheManager" ref="redisCacheManager"/>
<property name="sessionManager" ref="sessionManager"/>
<property name="kickoutAfter" value="false"/>
<property name="maxSession" value="1"/>
<property name="kickoutUrl" value="/app/login/unlogin-kickout"/>
</bean>

<!-- session存储的实现 -->
<!--<bean id="shiroSessionDao" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO" /> -->

<!-- 单机session
<bean id="shiroCacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" />
-->
<!-- session 集群 -->
<!--
<bean id="shiroCacheManager" class="com.winwho.common.shiro.cache.ShiroRedisCacheManager">
<property name="cached" ref="redisCached" />
</bean>
-->

<!-- shiro的主过滤器,beanId和web.xml中配置的filter name需要保持一致 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 安全管理器 -->
<property name="securityManager" ref="securityManager" />
<!-- 默认的登陆访问url -->
<property name="loginUrl" value="/app/login/unlogin" />
<!-- 没有权限跳转的url -->
<property name="unauthorizedUrl" value="/app/login/unauth" />
<property name="filters">
<map>
<entry key="kickout" value-ref="kickoutSessionControlFilter"/>
</map>
</property>

<!-- 访问地址的过滤规则,从上至下的优先级,如果有匹配的规则,就会返回,不会再进行匹配 -->
<property name="filterChainDefinitions">
<value>

/wx/auction/** = user<!--拦截的url-->

/** = anon <!--必须-->
</value>
</property>
</bean>
<!--/app/appinit/config = roles[admin]-->
<!-- Support Shiro Annotation -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"  depends-on="lifecycleBeanPostProcessor"/>

<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>

<!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: