1. 程式人生 > >springMvc整合shiro xml配置

springMvc整合shiro xml配置

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

    <!-- 使用spring元件掃描@service  -->
    <context:component-scan base-package="com.swxc"/>

    <!-- 引入外部資料來源配置資訊 -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:${env.active}/webconfig.properties</value>
                <value>classpath:${env.active}/redis.properties</value>
            </list>
        </property>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
    </bean>

    <!-- 自定義域realm -->
    <!--<bean id="accountRealm" class="com.swxc.core.cfg.shiro.realm.AccountRealm"></bean>-->
    <!--<bean id="dynamicPasswordAccountRealm" class="com.swxc.core.cfg.shiro.realm.DynamicPasswordAccountRealm"></bean>-->

    <bean id="oAuth2Realm" class="com.swxc.core.cfg.shiro.realm.OAuth2Realm">
        <property name="authenticationCachingEnabled" value="false"/>
        <property name="authorizationCachingEnabled" value="false"/>
    </bean>

    <bean id="shiroCacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"/>

    <bean id="javaUuidSessionIdGenerator" class="org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator"></bean>

    <bean id="simpleCookie" class=" org.apache.shiro.web.servlet.SimpleCookie">
        <constructor-arg name="name" value="uid"/>
        <property name="httpOnly" value="false"/>
        <property name="maxAge" value="-1"/>
        <property name="domain" value="${shiro.uid.cookie.domain}"/>
        <property name="path" value="${shiro.uid.cookie.path}"/>
    </bean>
    <bean id="cacheSessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">
        <property name="cacheManager" ref="shiroCacheManager"/>
        <property name="sessionIdGenerator" ref="javaUuidSessionIdGenerator"/>
    </bean>

    <!-- 會話驗證排程器 -->
    <bean id="sessionValidationScheduler"
          class="org.apache.shiro.session.mgt.ExecutorServiceSessionValidationScheduler">
        <property name="sessionManager" ref="sessionManager"/>
        <property name="interval" value="300000"/>
    </bean>
    <!-- 會話管理器 -->
    <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">

        <property name="globalSessionTimeout" value="1800000"/>
        <!--<property name="sessionValidationInterval" value="1800000"/>-->
        <!--<property name="sessionValidationSchedulerEnabled" value="true"/>-->
        <!--<property name="sessionValidationScheduler" ref="sessionValidationScheduler"/>-->
        <!--<property name="deleteInvalidSessions" value="true"/>-->
        <property name="sessionIdCookieEnabled" value="true"/>
        <property name="sessionIdCookie" ref="simpleCookie"/>
        <property name="cacheManager" ref="shiroCacheManager"/>
        <property name="sessionDAO" ref="cacheSessionDAO"/>
        <property name="sessionFactory" ref="MkSessionFactory"/>

    </bean>

    <bean id="MkSessionFactory" class="com.swxc.core.cfg.shiro.MkSessionFactory"></bean>

    <!-- 安全管理器-->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="cacheManager" ref="shiroCacheManager"/>
        <property name="authenticator" ref="authenticator"/>
        <property name="sessionManager" ref="sessionManager"/>
        <property name="realms">
            <list>
                <ref bean="oAuth2Realm"/>
            </list>
        </property>
    </bean>

    <!--<bean id="accountRealm" class="com.swxc.core.cfg.shiro.realm.AccountRealm">-->
    <!--</bean>-->

    <!--<bean id="dynamicPasswordAccountRealm" class="com.swxc.core.cfg.shiro.realm.DynamicPasswordAccountRealm"></bean>-->

    <bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
        <property name="authenticationStrategy">
            <bean class="org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy"></bean>
        </property>
    </bean>

    <bean id="securitySetSecurityManager" class="com.swxc.core.cfg.shiro.SecuritySetSecurityManager">
        <property name="securityManager" ref="securityManager"/>
    </bean>

    <bean id="oauth2Authc" class="com.swxc.core.cfg.shiro.filter.OAuth2AuthenticationFilter">
        <property name="resourceId" value="health"/>
    </bean>

    <bean id="miOAuth2Authc" class="com.swxc.core.cfg.shiro.filter.MiOAuth2AuthenticationFilter">
        <property name="resourceId" value="property_customer_app"/>
    </bean>

    <bean id="oAuth2Logout" class="com.swxc.core.cfg.shiro.filter.OAuth2LogoutFilter">
        <property name="loginURL" value="${shiro.login.url}"/>
    </bean>

    <bean id="oauth2CallbackFilter" class="com.swxc.core.cfg.shiro.filter.OAuth2CallbackFilter">
        <property name="clientId" value="${oauth2.clientid}"/>
        <property name="clientSecret" value="${oauth2.client.secret}"/>
        <property name="accessTokenUrl" value="${oauth2.access.token.url}"/>
        <property name="redirectUrl" value="${oauth2.redirect.url}"/>
        <property name="accessTokenCookie" ref="accessTokenCookie"/>
    </bean>

    <bean id="accessTokenContext" class="com.swxc.core.cfg.shiro.filter.AccessTokenContextFilter">
    </bean>

    <bean id="auditorAware" class="com.swxc.core.cfg.shiro.MkAuditorAware"></bean>
    <bean id="customerAppOauth2Authc" class="com.swxc.core.cfg.shiro.filter.OAuth2AuthenticationFilter">
        <property name="resourceId" value="property_customer_app"/>
    </bean>

    <bean id="accessTokenCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
        <property name="name" value="access_token"/>
        <property name="httpOnly" value="false"/>
        <property name="maxAge" value="-1"/>
        <property name="domain" value="${shiro.uid.cookie.domain}"/>
        <property name="path" value="${shiro.uid.cookie.path}"/>
    </bean>
    <!-- shiro filter -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!-- 安全管理器必須的 -->
        <property name="securityManager" ref="securityManager"/>
        <!-- 身份認證失敗   認證提交的地址 -->
        <property name="loginUrl" value="${shiro.login.url}"/>
        <!-- 許可權認證失敗    沒有許可權認證提交的地址 -->
        <property name="unauthorizedUrl" value="/login.jsp"/>
        <property name="filterChainDefinitions">
            <value>
                <!-- 對靜態資源設定匿名訪問 -->
                /resources/**=anon
                /server/resources/**=anon
                /favicon.png=anon
                /welcome=anon
                /authenticate.htm=anon
                /mi/**=accessTokenContext,miOAuth2Authc
                /mc/**=accessTokenContext,customerAppOauth2Authc
                /oauth2/callback.htm=oauth2Callback
                /logout.htm=accessTokenContext,logout
                /** = accessTokenContext,oauth2Authc
            </value>
        </property>
        <property name="filters">
            <until:map>
                <entry key="accessTokenContext" value-ref="accessTokenContext"/>
                <entry key="customerAppOauth2Authc" value-ref="customerAppOauth2Authc"/>
                <entry key="oauth2Callback" value-ref="oauth2CallbackFilter"/>
                <entry key="oauth2Authc" value-ref="oauth2Authc"/>
                <entry key="miOAuth2Authc" value-ref="miOAuth2Authc"/>
                <entry key="logout" value-ref="oAuth2Logout"/>
            </until:map>
        </property>
    </bean>
    <!-- Shiro生命週期處理器 -->
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean>
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"/>
    </bean>


</beans>