集羣版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框架就搭建起來了,至於權限驗證和其他功能,在以後的工作再慢慢添加。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章