集群版shiro(redis)搭建實錄

??公司的公共服務(wù)-通知中心要添加登錄驗權(quán)功能,在考慮到以后的擴(kuò)展性和集群部署的特性,選擇了shiro+redis 的方案,現(xiàn)在就跟隨我看看一個適用于集群的shiro是如何搭建的。

shiro配置
  • 導(dǎo)包
  • 修改web.xml
  • 添加shiro-spring.xml 文件
shiro相關(guān)代碼
  • 自定義Realm
  • RedisCache實現(xiàn)

Start

配置
  1. 導(dǎo)入shiro相關(guān)jar包,這里使用maven來管理


    image.png
  2. 修改原有的web.xml文件,shiro接管Servlet過濾器


    image.png

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

  • 在原有application.xml 文件中添加(文件名自定義)
<import resource="/security-context.xml" />
  • 創(chuàng)建shiro-spring.xml 文件(我這里命名為security-context)
  1. 添加憑證匹配器(密碼使用MD5加密驗證,散列次數(shù)=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(安全數(shù)據(jù)源),并開啟緩存和注入憑證匹配器
    <!--自定義Realm-->
    <bean id="noticeRealm" class="com.lingyun.security.relam.simpleRealm">
        <!--啟用緩存,默認(rèn)關(guān)閉-->
        <property name="cachingEnabled" value="true"/>
        <!--啟用身份驗證緩存,即緩存AuthenticationInfo,默認(rèn)false-->
        <property name="authenticationCachingEnabled" value="true"/>
        <!-- 配置密碼匹配器 -->
        <property name="credentialsMatcher" ref="credentialsMatcher"/>
    </bean>
  1. RedisCache相關(guān)類注冊,這里使用了Git開源的shiro+redis緩存實現(xiàn)(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,修改默認(rèn)sessionID。默認(rèn)sessionID 為JSESSIONID 會和容器名沖突, 如JETTY, TOMCAT 等,導(dǎo)致session失效
    <!--修改默認(rèn)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"/>
        <!--修改默認(rèn)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過濾器,實現(xiàn)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. 最后注冊安全管理器,注入相關(guān)管理器
    <!-- 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相關(guān)的配置及改造就結(jié)束了,接下來是一些和業(yè)務(wù)綁定的代碼和數(shù)據(jù)庫相關(guān)

continue

代碼編寫及改造
  1. 創(chuàng)建用戶DTO和Table,這里要注意UserDTO一定要實現(xiàn)序列化接口,redis緩存時會序列化對象


    image.png

    2.自定義Realm實現(xiàn),要注意因為使用了憑證匹配器,認(rèn)證時不用再做密碼驗證,將數(shù)據(jù)源中的密碼傳入即可,另外因?qū)崿F(xiàn)了redis緩存,要傳入UserDTO實例

    /**
     * 認(rèn)證
     *
     * @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(); //用戶為空
        }

        //如果身份認(rèn)證驗證成功,返回一個AuthenticationInfo實現(xiàn);
        return new SimpleAuthenticationInfo(user, user.getPassword(), getName());
    }
??這樣一個簡版的shiro框架就搭建起來了,至于權(quán)限驗證和其他功能,在以后的工作再慢慢添加。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容