Linux中session共享

有的項目的所有功能都要進行登錄驗證,
驗證條件是判斷 session 中是否有 user 對象,
如果有就認爲登錄過,如果沒有就進行登錄

session(會話):
    服務器上的一塊存儲空間,和客戶端一一對應。
    
    一一對應靠的是 sessionId 和 cookie
        sessionId 是每個 session 的唯一標識

cookie:
    服務器發送給瀏覽器保存的數據,
    瀏覽器會自動存儲服務器發送給他的所有 cookie,默認保存在網站根路徑目錄下
    瀏覽器向服務器發請求的時候會攜帶該網站保存的所有 cookie

使用 redis 實現 session 共享:
redis安裝:https://blog.csdn.net/ilovehua521/article/details/84403735

    1. 項目增加依賴:
        spring-session-data-redis 
            通過 spring 實現 session 在 redis 共享的框架
        jedis
            使用 java 操縱 redis 的框架
        
    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>/*</url-pattern>
            </filter-mapping>

    3. 配置 spring-dao.xml
        1. 配置 redis 連接池信息
            <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"></bean>
            
        2. 配置 redis 密碼    
            <bean id="redisPassword" class="org.springframework.data.redis.connection.RedisPassword">
                <constructor-arg name="thePassword" value="123456"></constructor-arg>
            </bean>
            
        3. 配置 redis 連接信息
            <bean id="redisStandaloneConfiguration" class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">
                <property name="hostName" value="192.168.7.230"/>
                <property name="port" value="6379"/>
                <property name="password" ref="redisPassword"/>
            </bean>    
            
        4. 配置 redis 連接工廠
            <bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
                <constructor-arg name="standaloneConfig" ref="redisStandaloneConfiguration"></constructor-arg>
            </bean>
        
        5. 配置 session-redis 過濾器
            <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>

    4. 存入 session 的數據必須支持序列化
        序列化:對象 --> 二進制
        反序列化:二進制 --> 對象
        
        java 中的 model 類如果需要支持序列化操作
            只需要實現 Serializable 接口即可
            
            這個接口沒有任何方法,起到一個標識作用
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章