Spring+Shiro搭建基于Redis的分布式权限系统(有实例)

摘要: 简单介绍使用Spring+Shiro搭建基于Redis的分布式权限系统。

这篇主要介绍Shiro如何与redis结合搭建分布式权限系统,至于如何使用和配置Shiro就不多说了。完整实例下载地址:https://git.oschina.net/zhmlvft/spring_shiro_redis

要实现分布式,主要需要解决2个大问题。

第一个解决shiro  session共享的问题。这个可以通过自定义sessionDao实现。继承 CachingSessionDao。

复制代码
public class RedisSessionDao extends CachingSessionDAO {
    private final static Logger log = LoggerFactory.getLogger(RedisSessionDao.class);
</span><span style="color: #0000ff;">private</span> RedisTemplate&lt;String, Object&gt;<span style="color: #000000;"> redisTemplate;
</span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span> defaultExpireTime = <span style="color: #800080;">3600</span><span style="color: #000000;">;

</span><span style="color: #0000ff;">private</span> CacheManager cm=<span style="color: #0000ff;">null</span><span style="color: #000000;">;

</span><span style="color: #0000ff;">public</span> RedisSessionDao(RedisTemplate&lt;String, Object&gt; redisTemplate,<span style="color: #0000ff;">int</span><span style="color: #000000;"> defaultExpireTime) {
    </span><span style="color: #0000ff;">this</span>.redisTemplate =<span style="color: #000000;"> redisTemplate;
    </span><span style="color: #0000ff;">this</span>.defaultExpireTime =<span style="color: #000000;"> defaultExpireTime;
}

@Override
</span><span style="color: #0000ff;">protected</span><span style="color: #000000;"> Serializable doCreate(Session session) {
    Serializable sessionId </span>=<span style="color: #000000;"> generateSessionId(session);
    cm </span>=<span style="color: #000000;"> CacheManager.create();
    </span><span style="color: #0000ff;">if</span>(cm==<span style="color: #0000ff;">null</span><span style="color: #000000;">){
        cm </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> CacheManager(getCacheManagerConfigFileInputStream());
    }
    Ehcache ehCache </span>= cm.getCache(<span style="color: #800000;">"</span><span style="color: #800000;">sessioncache</span><span style="color: #800000;">"</span><span style="color: #000000;">);
    assignSessionId(session, sessionId);
    redisTemplate.opsForValue().</span><span style="color: #0000ff;">set</span><span style="color: #000000;">(sessionId.toString(), session);
    redisTemplate.expire(sessionId.toString(), </span><span style="color: #0000ff;">this</span><span style="color: #000000;">.defaultExpireTime, TimeUnit.SECONDS);
    ehCache.put(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Element(sessionId.toString(),session));
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> sessionId;
}

@Override
</span><span style="color: #0000ff;">protected</span><span style="color: #000000;"> Session doReadSession(Serializable sessionId) {
    </span><span style="color: #008000;">//</span><span style="color: #008000;">此方法不会执行,不用管</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">null</span><span style="color: #000000;">;
}

@Override
</span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> doUpdate(Session session) {
    </span><span style="color: #008000;">//</span><span style="color: #008000;">该方法交给父类去执行</span>

}

@Override
</span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> doDelete(Session session) {

    Serializable sessionId </span>=<span style="color: #000000;"> session.getId();
    cm </span>=<span style="color: #000000;"> CacheManager.create();
    </span><span style="color: #0000ff;">if</span>(cm==<span style="color: #0000ff;">null</span><span style="color: #000000;">){
        cm </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> CacheManager(getCacheManagerConfigFileInputStream());
    }
    Ehcache ehCache </span>= cm.getCache(<span style="color: #800000;">"</span><span style="color: #800000;">sessioncache</span><span style="color: #800000;">"</span><span style="color: #000000;">);
    redisTemplate.delete(sessionId.toString());
    ehCache.remove(sessionId.toString());
}

</span><span style="color: #0000ff;">protected</span><span style="color: #000000;"> InputStream getCacheManagerConfigFileInputStream() {
    String configFile </span>= <span style="color: #800000;">"</span><span style="color: #800000;">classpath:ehcache.xml</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> ResourceUtils.getInputStreamForPath(configFile);
    } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
        </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> ConfigurationException(<span style="color: #800000;">"</span><span style="color: #800000;">Unable to obtain input stream for cacheManagerConfigFile [</span><span style="color: #800000;">"</span> +<span style="color: #000000;">
                configFile </span>+ <span style="color: #800000;">"</span><span style="color: #800000;">]</span><span style="color: #800000;">"</span><span style="color: #000000;">, e);
    }
}</span></pre>
复制代码

 

第二个解决shiro cache的问题,这个可以通过自定义CacheManager实现,shiro默认的cache是基于ehcache的,自定义Redis实现的过程可以仿照shiro-ehcache.jar源码来实现。

附上spring中的完整配置如下:

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/task
          http://www.springframework.org/schema/task/spring-task-4.1.xsd
          http://www.springframework.org/schema/jee
          http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
          http://www.springframework.org/schema/cache
          http://www.springframework.org/schema/cache/spring-cache.xsd">
        <context:annotation-config/>
        <context:component-scan base-package="com.zhm.ssr"/>
        <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="ignoreUnresolvablePlaceholders" value="true" />
            <property name="locations">
                <list>
                    <value>classpath:jdbc.properties</value>
                    <value>classpath:redis.properties</value>
                </list>
            </property>
        </bean>
        <aop:aspectj-autoproxy />
       <!-- spring 线程池配置  -->
        <task:annotation-driven executor="executorWithCallerRunsPolicy"/>
        <task:executor id="executorWithCallerRunsPolicy" pool-size="2-5"  queue-capacity="50"  rejection-policy="CALLER_RUNS"/>
    &lt;!-- redis缓存 --&gt;
    &lt;bean id=<span style="color: #800000;">"</span><span style="color: #800000;">cacheManager</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">org.springframework.data.redis.cache.RedisCacheManager</span><span style="color: #800000;">"</span> c:template-<span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">redisTemplate</span><span style="color: #800000;">"</span>&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">defaultExpiration</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">31536000</span><span style="color: #800000;">"</span>&gt;&lt;/property&gt;
    &lt;/bean&gt;
    &lt;!--  支持注解缓存 --&gt;
    &lt;cache:annotation-driven cache-manager=<span style="color: #800000;">"</span><span style="color: #800000;">cacheManager</span><span style="color: #800000;">"</span>/&gt;

    &lt;bean <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator</span><span style="color: #800000;">"</span>/&gt;

    &lt;!-- 文件上传大小限制 --&gt;
    &lt;bean id=<span style="color: #800000;">"</span><span style="color: #800000;">multipartResolver</span><span style="color: #800000;">"</span>     <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">org.springframework.web.multipart.commons.CommonsMultipartResolver</span><span style="color: #800000;">"</span>&gt;  
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">maxUploadSize</span><span style="color: #800000;">"</span>&gt;
            &lt;value&gt;<span style="color: #800080;">4097152000</span>&lt;/value&gt;  
          &lt;/property&gt;
    &lt;/bean&gt;

    &lt;!--  ========shiro 配置开始 ======== --&gt;
    &lt;bean id=<span style="color: #800000;">"</span><span style="color: #800000;">myRealm</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">com.zhm.ssr.shiro.CustomRealm</span><span style="color: #800000;">"</span>&gt;
        &lt;!-- 配置AuthorizationInfo信息不用放redis缓存,直接放session即可 --&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">cachingEnabled</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">false</span><span style="color: #800000;">"</span>/&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">authenticationCachingEnabled</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">false</span><span style="color: #800000;">"</span> /&gt;
    &lt;/bean&gt;
    &lt;!-- session 保存到cookie,关闭浏览器下次可以直接登录认证,当maxAge为-1不会写cookie。--&gt;
    &lt;bean id=<span style="color: #800000;">"</span><span style="color: #800000;">sessionIdCookie</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">org.apache.shiro.web.servlet.SimpleCookie</span><span style="color: #800000;">"</span>&gt;
        &lt;constructor-arg value=<span style="color: #800000;">"</span><span style="color: #800000;">sid</span><span style="color: #800000;">"</span>/&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">httpOnly</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span>/&gt;
        &lt;!-- 浏览器关闭session失效,不计入cookie --&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">maxAge</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">-1</span><span style="color: #800000;">"</span>/&gt;
    &lt;/bean&gt;
    &lt;!--<span style="color: #000000;">  记住我功能,当关闭浏览器下次再访问的时候不需要登录也能查看。只对filterChainDefinitions设置为user级别的链接有效,
         类似于淘宝看商品、添加购物车,不需要验证是否登录。但是提交订单就必须登录。
    </span>--&gt;
    &lt;bean id=<span style="color: #800000;">"</span><span style="color: #800000;">rememberMeCookie</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">org.apache.shiro.web.servlet.SimpleCookie</span><span style="color: #800000;">"</span>&gt;
        &lt;constructor-arg value=<span style="color: #800000;">"</span><span style="color: #800000;">rememberMe</span><span style="color: #800000;">"</span>/&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">httpOnly</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span>/&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">maxAge</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">2592000</span><span style="color: #800000;">"</span>/&gt;&lt;!-- 30天 --&gt;
    &lt;/bean&gt;
    &lt;bean id=<span style="color: #800000;">"</span><span style="color: #800000;">rememberMeManager</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">org.apache.shiro.web.mgt.CookieRememberMeManager</span><span style="color: #800000;">"</span>&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">cookie</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">rememberMeCookie</span><span style="color: #800000;">"</span>/&gt;
        &lt;!-- aes key。shiro默认的key是不安全的,可以使用工程utils包的GenerateAESKey生成一个自定义的key--&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">cipherKey</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">#{T(org.apache.shiro.codec.Base64).decode('XgGkgqGqYrix9lI6vxcrRw==')}</span><span style="color: #800000;">"</span>/&gt;
    &lt;/bean&gt;
    &lt;bean id=<span style="color: #800000;">"</span><span style="color: #800000;">sessionManager</span><span style="color: #800000;">"</span>
          <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">org.apache.shiro.web.session.mgt.DefaultWebSessionManager</span><span style="color: #800000;">"</span>&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">sessionDAO</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">sessionDAO</span><span style="color: #800000;">"</span>/&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">sessionIdCookieEnabled</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span>/&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">sessionIdCookie</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">sessionIdCookie</span><span style="color: #800000;">"</span>/&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">deleteInvalidSessions</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span> /&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">sessionValidationSchedulerEnabled</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span> /&gt;
    &lt;/bean&gt;
    &lt;bean id=<span style="color: #800000;">"</span><span style="color: #800000;">securityManager</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">org.apache.shiro.web.mgt.DefaultWebSecurityManager</span><span style="color: #800000;">"</span>&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">realm</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">myRealm</span><span style="color: #800000;">"</span>/&gt;
        &lt;!-- shiro使用redis缓存 --&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">cacheManager</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">redisCacheManager</span><span style="color: #800000;">"</span> /&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">sessionManager</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">sessionManager</span><span style="color: #800000;">"</span> /&gt;
        &lt;!-- 客户端勾选记住 --&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">rememberMeManager</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">rememberMeManager</span><span style="color: #800000;">"</span>/&gt;
    &lt;/bean&gt;
    &lt;bean id=<span style="color: #800000;">"</span><span style="color: #800000;">redisCacheManager</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">com.zhm.ssr.shiro.RedisCacheManager</span><span style="color: #800000;">"</span> &gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">redisManager</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">redisManager</span><span style="color: #800000;">"</span> /&gt;
    &lt;/bean&gt;
    &lt;bean id=<span style="color: #800000;">"</span><span style="color: #800000;">redisManager</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">com.zhm.ssr.shiro.RedisManager</span><span style="color: #800000;">"</span>&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">expire</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">${redis.expireTime}</span><span style="color: #800000;">"</span> /&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">host</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">${redis.host}</span><span style="color: #800000;">"</span> /&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">password</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">${redis.pass}</span><span style="color: #800000;">"</span> /&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">port</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">${redis.port}</span><span style="color: #800000;">"</span> /&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">timeout</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">${redis.maxWait}</span><span style="color: #800000;">"</span> /&gt;
    &lt;/bean&gt;
    &lt;bean id=<span style="color: #800000;">"</span><span style="color: #800000;">shiroFilter</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">org.apache.shiro.spring.web.ShiroFilterFactoryBean</span><span style="color: #800000;">"</span>&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">securityManager</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">securityManager</span><span style="color: #800000;">"</span>/&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">loginUrl</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">/login.html</span><span style="color: #800000;">"</span>/&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">unauthorizedUrl</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">/nolimit.html</span><span style="color: #800000;">"</span>/&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">filters</span><span style="color: #800000;">"</span>&gt;
            &lt;map&gt;
                &lt;entry key=<span style="color: #800000;">"</span><span style="color: #800000;">mainFilter</span><span style="color: #800000;">"</span>&gt;
                    &lt;bean <span style="color: #0000ff;">class</span>=<span style="color: #800000;">"</span><span style="color: #800000;">com.zhm.ssr.filters.CustomAuthorizationFilter</span><span style="color: #800000;">"</span> /&gt;
                &lt;/entry&gt;
            &lt;/map&gt;
        &lt;/property&gt;
        &lt;property name=<span style="color: #800000;">"</span><span style="color: #800000;">filterChainDefinitions</span><span style="color: #800000;">"</span>&gt;
            &lt;!--<span style="color: #000000;"> 登录页面的所有请求,包括资源文件全部设定为匿名
                 修改用户信息功能需要验证登录,其他都使用user过滤器。即rememberMe功能。
            </span>--&gt;
            &lt;value&gt;
                /login**=<span style="color: #000000;">anon
                </span>/user/doLogin**=<span style="color: #000000;">anon
                </span>/lib/login.js=<span style="color: #000000;">anon
                </span>/css/theme.css=<span style="color: #000000;">anon
                </span>/favicon.ico=<span style="color: #000000;">anon
                </span>/lib/font-awesome/css/font-awesome.css=<span style="color: #000000;">anon
                </span>/js/html5.js=<span style="color: #000000;">anon
                </span>/user/edit<span style="color: #008000;">/*</span><span style="color: #008000;">*=authc,perms[admin:manage]
                /**=mainFilter,user
            &lt;/value&gt;
        &lt;/property&gt;
    &lt;/bean&gt;
    &lt;!-- 自定义shiro的sessionDao,把session写入redis --&gt;
    &lt;bean id="sessionDAO" class="com.zhm.ssr.shiro.RedisSessionDao"&gt;
        &lt;constructor-arg ref="redisTemplate" /&gt;
        &lt;constructor-arg value="${redis.expireTime}" /&gt;
    &lt;/bean&gt;
    &lt;bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/&gt;

    &lt;!-- @shiro注解抛出异常之后跳转的页面。--&gt;
    &lt;bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"&gt;
        &lt;property name="exceptionMappings"&gt;
            &lt;props&gt;
                &lt;prop key="org.apache.shiro.authz.UnauthorizedException"&gt;
                    redirect:/nolimit.html
                &lt;/prop&gt;
                &lt;prop key="org.apache.shiro.authz.UnauthenticatedException"&gt;
                    redirect:/login.html
                &lt;/prop&gt;
            &lt;/props&gt;
        &lt;/property&gt;
    &lt;/bean&gt;
    &lt;!--  ========shiro 配置结束 ======== --&gt;

    &lt;!--  spring-data-redis配置,主要用作redis缓存 begin --&gt;
    &lt;bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:timeout="5000"/&gt;
    &lt;bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" /&gt;
    &lt;bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /&gt;
    &lt;bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"
          p:connection-factory-ref="connectionFactory"&gt;
        &lt;property name="KeySerializer" ref="stringRedisSerializer" /&gt;
        &lt;property name="ValueSerializer" ref="stringRedisSerializer" /&gt;
        &lt;property name="hashKeySerializer" ref="stringRedisSerializer" /&gt;
        &lt;property name="hashValueSerializer" ref="stringRedisSerializer" /&gt;
    &lt;/bean&gt;
    &lt;bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
          p:connection-factory-ref="connectionFactory"&gt;
        &lt;property name="KeySerializer" ref="stringRedisSerializer" /&gt;
        &lt;property name="ValueSerializer" ref="jdkSerializationRedisSerializer" /&gt;
        &lt;property name="hashKeySerializer" ref="stringRedisSerializer" /&gt;
        &lt;property name="hashValueSerializer" ref="jdkSerializationRedisSerializer" /&gt;
        &lt;property name="enableTransactionSupport" value="true" /&gt;
    &lt;/bean&gt;
    &lt;!--  spring-data-redis配置,主要用作redis缓存 end --&gt;

    &lt;bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"&gt;
        &lt;property name="driverClassName" value="${jdbch2.driverClassName}" /&gt;
        &lt;property name="url" value="${jdbch2.url}" /&gt;
        &lt;property name="username" value="${jdbch2.username}" /&gt;
        &lt;property name="password" value="${jdbch2.password}" /&gt;
        &lt;property name="validationQuery" value="select 1 " /&gt;
    &lt;/bean&gt;
    &lt;bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"&gt;
        &lt;property name="dataSource"&gt;&lt;ref bean="dataSource"/&gt;&lt;/property&gt;
    &lt;/bean&gt;
    &lt;bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
        &lt;property name="dataSource" ref="dataSource"/&gt;
    &lt;/bean&gt;
    &lt;tx:annotation-driven transaction-manager="txManager"/&gt;

</beans>

复制代码

原文地址:https://blog.csdn.net/chenyao1994/article/details/89395040;

posted @ 2019-12-17 14:50  星朝  阅读(699)  评论(0编辑  收藏  举报