集群版shiro(redis)搭建实录

  公司的公共服务-通知中心要添加登录验权功能,在考虑到以后的扩展性和集群部署的特性,选择了shiro+redis 的方案,现在就跟随我看看一个适用于集群的shiro是如何搭建的。

shiro配置
  • 导包
  • 修改web.xml
  • 添加shiro-spring.xml 文件
shiro相关代码
  • 自定义Realm
  • RedisCache实现

Start

配置
  1. 导入shiro相关jar包,这里使用maven来管理


  2. 修改原有的web.xml文件,shiro接管Servlet过滤器



    3.工程使用的是spring,这里添加shiro-spirng.xml 完成shiro核心功能的配置和依赖。

  • 在原有application.xml 文件中添加(文件名自定义)
<import resource="/security-context.xml" />
  • 创建shiro-spring.xml 文件(我这里命名为security-context)
  1. 添加凭证匹配器(密码使用MD5加密验证,散列次数=1)
<!-- 凭证匹配器 密码MD5加密验证 -->
    <bean id="credentialsMatcher"
          class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
        <property name="hashAlgorithmName" value="md5"/>
        <property name="hashIterations" value="1"/>
    </bean>
  1. 添加自定义realm(安全数据源),并开启缓存和注入凭证匹配器
    <!--自定义Realm-->
    <bean id="noticeRealm" class="com.lingyun.security.relam.simpleRealm">
        <!--启用缓存,默认关闭-->
        <property name="cachingEnabled" value="true"/>
        <!--启用身份验证缓存,即缓存AuthenticationInfo,默认false-->
        <property name="authenticationCachingEnabled" value="true"/>
        <!-- 配置密码匹配器 -->
        <property name="credentialsMatcher" ref="credentialsMatcher"/>
    </bean>
  1. RedisCache相关类注册,这里使用了Git开源的shiro+redis缓存实现(https://github.com/alexxiyang/shiro-redis),做了本地化改造
    <!--redis池管理器 -->
    <bean id="shardedJedisPoolManager"
          class="com.lingyun.security.rediscache.ShardedJedisPoolManager">
        <property name="expire" value="1800"/><!-- 秒 -->
    </bean>
    <!--Session缓存DAO组件-->
    <bean id="redisSessionDAO" class="com.lingyun.security.rediscache.RedisSessionDAO">
        <property name="redisManager" ref="shardedJedisPoolManager"/>
    </bean>
    <!--shiro(redis)缓存管理-->
    <bean id="redisCacheManager" class="com.lingyun.notice.service.web.security.rediscache.RedisCacheManager">
        <property name="redisManager" ref="shardedJedisPoolManager"/>
    </bean>
  1. 添加SimpleCookie,修改默认sessionID。默认sessionID 为JSESSIONID 会和容器名冲突, 如JETTY, TOMCAT 等,导致session失效
    <!--修改默认sessionID 为notice-center-->
    <bean id="simpleCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
        <constructor-arg name="name" value="notice-center"/>
        <property name="path" value="/"/>
    </bean>
  1. 添加会话管理器,
    <!-- (session)会话管理器 -->
    <bean id="sessionManager" class="com.lingyun.web.security.session.SimpleWebSessionManager">
        <!--session缓存-->
        <property name="sessionDAO" ref="redisSessionDAO"/>
        <!--修改默认Cookie-->
        <property name="sessionIdCookie" ref="simpleCookie"/>
        <!-- session的失效时长,单位毫秒 -->
        <property name="globalSessionTimeout" value="600000"/>
        <!-- 删除失效的session -->
        <property name="deleteInvalidSessions" value="true"/>
        <!--隐藏url中的JSESSIONID -->
        <property name="sessionIdUrlRewritingEnabled" value="false"/>
    </bean>
  1. 配置shiroWeb过滤器,实现url自定义拦截
    <!-- Shiro 的Web过滤器 -->
    <bean id="securityFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <property name="loginUrl" value="/login.html"/>
        <property name="unauthorizedUrl" value="/login.html"/>
        <property name="filterChainDefinitions">
            <value>
                /static/** = anon
                /doLogin = anon
                /error = anon
                /send* = anon
                /** = user
            </value>
        </property>
    </bean>
  1. 开启rememberMe功能
    <!-- rememberMeManager管理器,写cookie,取出cookie生成用户信息 -->
    <bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">
        <property name="cookie" ref="rememberMeCookie"/>
    </bean>
    <!-- 记住我cookie -->
    <bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
        <!-- rememberMe是cookie的名字 -->
        <constructor-arg value="notice-cookie"/>
        <!-- 记住我cookie生效时间30天 -->
        <property name="maxAge" value="2592000"/>
    </bean>
  1. 最后注册安全管理器,注入相关管理器
    <!-- securityManager安全管理器 -->
    <bean id="securityManager" class="com.lingyun.security.SimpleWebSecurityManager">
        <property name="realm" ref="noticeRealm"/>
        <!-- 注入缓存管理器 -->
        <property name="cacheManager" ref="redisCacheManager"/>
        <!-- 注入session管理器 -->
        <property name="sessionManager" ref="sessionManager"/>
        <!-- 记住我 -->
        <property name="rememberMeManager" ref="rememberMeManager"/>
    </bean>

  到这里shiro相关的配置及改造就结束了,接下来是一些和业务绑定的代码和数据库相关

continue

代码编写及改造
  1. 创建用户DTO和Table,这里要注意UserDTO一定要实现序列化接口,redis缓存时会序列化对象



    2.自定义Realm实现,要注意因为使用了凭证匹配器,认证时不用再做密码验证,将数据源中的密码传入即可,另外因实现了redis缓存,要传入UserDTO实例

    /**
     * 认证
     *
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        String username = (String) token.getPrincipal();

        NoticeUserDTO user = noticeUserInfoService.findUserIFByName(username);

        if (user == null || user.getDataState() == 0) {
            throw new UnknownAccountException(); //用户为空
        }

        //如果身份认证验证成功,返回一个AuthenticationInfo实现;
        return new SimpleAuthenticationInfo(user, user.getPassword(), getName());
    }
  这样一个简版的shiro框架就搭建起来了,至于权限验证和其他功能,在以后的工作再慢慢添加。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章