1. 程式人生 > >freamwork2實現單點登入,跨系統獲取session資訊

freamwork2實現單點登入,跨系統獲取session資訊

   

redissession.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:remote="http://www.sf-pay.com/schema/remote"
   xmlns:sfpay-mvc="http://www.sf-pay.com/schema/sfpay-mvc"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
   xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd          
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      http://www.sf-pay.com/schema/sfpay-mvc http://www.sf-pay.com/schema/framework2-mvc.xsd
      http://www.sf-pay.com/schema/remote http://www.sf-pay.com/schema/remote/framework2-remote.xsd">


   
    <!--redis sentinel 配置-->
  <bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration" >
  <constructor-arg index="0" value="${master}" />
  <constructor-arg index="1">
  <set>
    <value>${redis1.url}</value>
    <value>${redis2.url}</value>
    <value>${redis3.url}</value>
  </set>
   
  </constructor-arg>
   
  </bean>
   
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:password="${redis.pass}" p:timeout="${redis.timeout}"  >
        <constructor-arg index="0" ref="redisSentinelConfiguration"></constructor-arg>
        <constructor-arg index="1" ref="jedisPoolConfig"></constructor-arg>
        </bean> 
     <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory" />
     
      <bean id="redisSessionDao" class="com.sfpay.framework2.web.session.SpringDataRedisSessionDao" >
    <property name="redisTemplate" ref="redisTemplate"></property>
    </bean>
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> 
        <property name="maxIdle" value="${redis.maxIdle}" /> 
        <property name="maxTotal" value="${redis.maxActive}" /> 
        <property name="minIdle" value="${redis.maxWait}" /> 
        
    </bean>
     
     
<bean id="distributedSessionFilter"
       class="com.sfpay.framework2.web.session.DistributedSessionFilter">
        <property name="distributedSessionManager">
               <bean
                      class="com.sfpay.framework2.web.session.DefaultDistributedSessionManager">
                      <property name="distributedSessionDao">
                             <ref bean="redisSessionDao" />
                      </property>
                      <property name="sessionCookieName" value="SFPAY_JSESSIONID" />
               </bean>
        </property>
</bean>


<sfpay-mvc:config>
        <sfpay-mvc:message-converter bean-name="jacksonHttpMessageConverter" />
        <sfpay-mvc:filter-chain-proxy bean-name="filterChainProxy">
               <list>
                     <bean class="org.springframework.web.filter.CharacterEncodingFilter">
                             <property name="encoding" value="UTF-8" />
                             <property name="forceEncoding" value="true" />
                      </bean> 
                      <ref local="distributedSessionFilter" />
               </list>
        </sfpay-mvc:filter-chain-proxy>
</sfpay-mvc:config>
      
</beans>

pom.xml

<!--設定 Spring 的版本 -->
<properties>
<org.springframework.version>3.0.0.RELEASE</org.springframework.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<framework2.version>1.3.6-sp1</framework2.version>
</properties>

<dependency>
   <groupId>com.sfpay.framework2</groupId>
   <artifactId>framework2-core</artifactId>
   <version>${framework2.version}</version>
</dependency>
 
<dependency>
   <groupId>com.sfpay.framework2</groupId>
   <artifactId>framework2-context</artifactId>
   <version>${framework2.version}</version>
</dependency>
 
<dependency>
    <groupId>com.sfpay.framework2</groupId>
    <artifactId>framework2-security</artifactId>
    <version>${framework2.version}</version>
</dependency>
 
<dependency>
   <groupId>com.sfpay.framework2</groupId>
   <artifactId>framework2-web</artifactId>
   <version>${framework2.version}</version>
</dependency>

web.xml中配置

<filter>
        <filter-name>distributedSessionFilterProxy</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>targetBeanName</param-name>
            <param-value>distributedSessionFilter</param-value>
        </init-param>
    </filter>


    <filter-mapping>
        <filter-name>distributedSessionFilterProxy</filter-name>
        <url-pattern>*</url-pattern>
    </filter-mapping>