shiro xml標准配置


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

    <description>Shiro安全配置</description>

    <!--安全管理器-->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!--設置自定義Realm-->
        <property name="realm" ref="shiroDbRealm"/>
        <!--將緩存管理器,交給安全管理器-->
        <property name="cacheManager" ref="shiroEhcacheManager"/>
        <!-- 注入session管理器 -->
        <property name="sessionManager" ref="sessionManager" />
        <!-- 記住密碼管理 -->
<!--         <property name="rememberMeManager" ref="rememberMeManager"/> -->
    </bean>

    <!-- 項目自定義的Realm -->
    <bean id="shiroDbRealm" class="com.agood.bejavagod.shiro.ShiroDbRealm"/>

    <!-- 記住密碼Cookie -->
    <bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie">  
        <constructor-arg value="rememberMe"/>
        <property name="httpOnly" value="true"/>
        <!-- 7天,采用spring el計算方便修改[細節決定成敗]! -->
        <property name="maxAge" value="#{7 * 24 * 60 * 60}"/>
<!--         <property name="domain" value=".bejavagod.com"/> -->
    </bean>

    <!-- rememberMe管理器,cipherKey生成見{@code Base64Test.java} -->
    <bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">
        <property name="cipherKey" value="#{T(org.apache.shiro.codec.Base64).decode('5aaC5qKm5oqA5pyvAAAAAA==')}"/>
        <property name="cookie" ref="rememberMeCookie"/>  
    </bean>

    <!-- Shiro Filter -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!-- 安全管理器 -->
        <property name="securityManager" ref="securityManager"/>
        <!-- 默認的登陸訪問url -->
        <property name="loginUrl" value="/login.action"/>
        <!-- 登陸成功后跳轉的url -->
        <property name="successUrl" value="/index.action"/>
        <!-- 沒有權限跳轉的url -->
        <property name="unauthorizedUrl" value="/unauth.action"/>
        
<!--         自定義filter配置 -->
        <property name="filters">
            <map>
                <entry key="authc">
                    <bean class="com.agood.bejavagod.controller.filter.CustomFormAuthenticationFilter"></bean>
                </entry>
            </map>
        </property>
        
        <property name="filterChainDefinitions">
            <value>
                <!-- 
                    anon  不需要認證
                    authc 需要認證
                    user  驗證通過或RememberMe登錄的都可以
                -->
<!--                 /commons/** = anon -->
                /static/** = anon
<!--                 /webhooks = anon -->
                /login.action = anon
                
                /page/404.action = anon
                /page/500.action = anon
                
<!--                 /dataDict/saveOrUpdateDataDict.action = perms["shiro:save"] -->
                
                /** = authc
            </value>
        </property>
    </bean>

    <!-- 用戶授權信息Cache, 采用EhCache -->
    <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <property name="cacheManagerConfigFile" value="classpath:shiro/ehcache-shiro.xml"/>
    </bean>

    <!-- 在方法中 注入  securityManager ,進行代理控制 -->
    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/>
        <property name="arguments" ref="securityManager"/>
    </bean>

    <!-- 保證實現了Shiro內部lifecycle函數的bean執行 -->
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

    <!-- AOP式方法級權限檢查  -->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>

    <!-- 啟用shrio授權注解攔截方式 -->
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"/>
    </bean>
    
    <!-- 會話管理器 -->
<!--     <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"> -->
        <!-- session的失效時長,單位毫秒 1小時: 3600000 -->
<!--         <property name="globalSessionTimeout" value="3600000"/> -->
        <!-- 刪除失效的session -->
<!--         <property name="deleteInvalidSessions" value="true"/> -->
<!--     </bean> -->
    
    
    <!-- 會話管理器 start -->
    <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
        <!-- session的失效時長,單位毫秒 1小時: 3600000 -->
        <!-- 設置全局會話超時時間,默認30分鍾,即如果30分鍾內沒有訪問會話將過期 1800000 -->
        <property name="globalSessionTimeout" value="1800000"/>
        <!-- 刪除失效的session -->
        <property name="deleteInvalidSessions" value="true"/>
        <!-- 是否開啟會話驗證器,默認是開啟的 -->
        <property name="sessionValidationSchedulerEnabled" value="true"/>
        <!-- 
            Shiro提供了會話驗證調度器,用於定期的驗證會話是否已過期,如果過期將停止會話;
            出於性能考慮,一般情況下都是獲取會話時來驗證會話是否過期並停止會話的;
            但是如在web環境中,如果用戶不主動退出是不知道會話是否過期的,因此需要定期的檢測會話是否過期,
            Shiro提供了會話驗證調度器SessionValidationScheduler來做這件事情。
         -->
        <property name="sessionValidationScheduler" ref="sessionValidationScheduler"/> 
        <!-- Shiro提供SessionDAO用於會話的CRUD -->
        <property name="sessionDAO" ref="sessionDAO"/>
        <!-- 
            是否啟用/禁用Session Id Cookie,默認是啟用的;
            如果禁用后將不會設置Session Id Cookie,即默認使用了Servlet容器的JSESSIONID,
            且通過URL重寫(URL中的“;JSESSIONID=id”部分)保存Session Id。 
        -->
        <property name="sessionIdCookieEnabled" value="true"/>
        <property name="sessionIdCookie" ref="sessionIdCookie"/>
    </bean>
    <!-- 會話驗證調度器 -->
    <bean id="sessionValidationScheduler" class="org.apache.shiro.session.mgt.ExecutorServiceSessionValidationScheduler">
        <!-- 設置調度時間間隔,單位毫秒,默認就是1小時 -->
        <property name="interval" value="1800000"/>
        <!-- 設置會話驗證調度器進行會話驗證時的會話管理器 -->
        <property name="sessionManager" ref="sessionManager"/>
    </bean>
<!--     <bean id="sessionValidationScheduler" class="org.apache.shiro.session.mgt.quartz.QuartzSessionValidationScheduler"> -->
<!--         <property name="sessionValidationInterval" value="1800000"/> -->
<!--         <property name="sessionManager" ref="sessionManager"/> -->
<!--     </bean> -->
    <!-- 會話DAO -->
    <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">
        <!-- 設置Session緩存名字,默認就是shiro-activeSessionCache,要和ehcache.xml中的那么對應 -->
        <property name="activeSessionsCacheName" value="shiro-activeSessionCache"/>
<!--         <property name="activeSessionsCacheName" value="shiroCache"/> -->
        <property name="sessionIdGenerator" ref="sessionIdGenerator"/>
    </bean>
    <!-- 會話ID生成器,用於生成會話ID,默認就是JavaUuidSessionIdGenerator,使用java.util.UUID生成-->
    <bean id="sessionIdGenerator" class="org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator"/>
     <!-- 會話Cookie模板,sessionManager創建會話Cookie的模板 -->
    <bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
        <!-- 設置Cookie名字,默認為JSESSIONID -->
<!--         <constructor-arg value="bjg_sid"/> -->
        <!-- 不修改使用默認的話,那么404的時候session就會過期 -->
        <property name="name" value="bjg_sid"/>
        <!-- 
            如果設置為true,則客戶端不會暴露給客戶端腳本代碼,使用HttpOnly cookie有助於減少某些類型的跨站點腳本攻擊;
            此特性需要實現了Servlet 2.5 MR6及以上版本的規范的Servlet容器支持
         -->
        <property name="httpOnly" value="true"/>
        <!-- 設置Cookie的過期時間,秒為單位,默認-1表示關閉瀏覽器時過期Cookie -->
        <property name="maxAge" value="-1"/>
        <!-- 設置Cookie的域名,默認空,即當前訪問的域名 -->
<!--         <property name="domain" value=".bejavagod.com"/> -->
    </bean>
    <!-- 會話管理器 end -->

    
    <!-- 自定義form認證過慮器 -->
    <!-- 基於Form表單的身份驗證過濾器,不配置將也會注冊此過慮器,表單中的用戶賬號、密碼及loginurl將采用默認值,建議配置 -->
<!--         <bean id="formAuthenticationFilter" class="com.agood.bejavagod.controller.filter.CustomFormAuthenticationFilter"> -->
            <!-- 表單中賬號的input名稱 -->
<!--             <property name="usernameParam" value="username" /> -->
            <!-- 表單中密碼的input名稱 -->
<!--             <property name="passwordParam" value="password" /> -->
            <!-- 記住我input的名稱 -->
<!--             <property name="rememberMeParam" value="rememberMe"/> -->
<!--      </bean> -->
     
</beans>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM