spring session實現同域下單點登錄


Session會話管理

  在Web項目開發中,Session會話管理是一個很重要的部分,用於存儲與記錄用戶的狀態或相關的數據;通常情況下session交由容器(tomcat)來負責存儲和管理,但是如果項目部署在多台tomcat中,則session管理存在很大的問題;1、多台tomcat之間無法共享session,比如用戶在tomcat A服務器上已經登錄了,但當負載均衡跳轉到tomcat B時,由於tomcat B服務器並沒有用戶的登錄信息,session就失效了,用戶就退出了登錄;2、一旦tomcat容器關閉或重啟也會導致session會話失效;因此如果項目部署在多台tomcat中,就需要解決session共享的問題;

配置文件

1 pom.xml <dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
    <version>1.3.1.RELEASE</version>
</dependency> 2 web.xml   <filter>
        <filter-name>springSessionRepositoryFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSessionRepositoryFilter</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>

  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext.xml
        </param-value>
    </context-param> 3 applicationContext.xml <context:annotation-config/>
    <!-- 初始化一切spring-session准備,且把springSessionFilter放入IOC  -->
    <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <property name="maxInactiveIntervalInSeconds" value="300"/>
    </bean>
  <!-- 配置cookie信息-->
    <bean class="org.springframework.session.web.http.DefaultCookieSerializer" id="defaultCookieSerializer">
        <property name="cookieName" value="SESSION_NAME"/>
        <property name="domainName" value="wangjun.com"/>
        <property name="useHttpOnlyCookie" value="true"/>
        <property name="cookiePath" value="/"/>
        <property name="cookieMaxAge" value="31536000"/>
    </bean>
  <!-- 配置redis連接池信息-->  
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="20"/>
    </bean>
  <!--配置redis連接信息-->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="127.0.0.1"/>
        <property name="port" value="6379"/>
        <property name="poolConfig" ref="jedisPoolConfig"/>
    </bean>

 

代碼測試

public class SessionServlet extends HttpServlet {
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sesssionID = request.getSession().getId();
        //部署兩份,把這個地方8081改成8080就行了,只是為了區分
        response.getWriter().write("8081 Server SessionID"+sesssionID);
    }
}

 


免責聲明!

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



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