Spring Session - 使用Redis存儲HttpSession例子

目的

使用Redis存儲管理HttpSession;html

添加pom.xml

該工程基於Spring Boot,同時咱們將使用Spring IO Platform來維護依賴版本號;html5

引入的依賴有spring-session、spring-boot-starter-web、spring-boot-starter-redis,pom文件以下:java

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>Athens-SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.4.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- Additional lines to be added here... -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.4.3.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

配置Spring Session

配置比較簡單,主要是添加@EnableRedisHttpSession註解便可,該註解會建立一個名字叫springSessionRepositoryFilter的Spring Bean,其實就是一個Filter,這個Filter負責用Spring Session來替換原先的默認HttpSession實現,在這個例子中,Spring Session是用Redis來實現的。git

import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@EnableRedisHttpSession
public class HttpSessionConfig {
    
}

操做HttpSession

這裏咱們將實現兩個操做,一個是往Session中寫入數據,另外一個是查詢數據,以下所示:github

@RestController
public class Example {

    @RequestMapping("/set")
    String set(HttpServletRequest req) {
        req.getSession().setAttribute("testKey", "testValue");
        return "設置session:testKey=testValue";
    }

    @RequestMapping("/query")
    String query(HttpServletRequest req) {
        Object value = req.getSession().getAttribute("testKey");
        return "查詢Session:\"testKey\"=" + value;
    }

}

編寫main方法

編寫main方法,使用@SpringBootApplication註解標註,若是查看該註解源碼的話,會發現至關於添加了@SpringBootConfiguration @EnableAutoConfiguration @ComponentScan等註解web

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class APP {
       public static void main(String[] args) throws Exception {
            SpringApplication.run(APP.class, args);
        }
}

運行程序,測試驗證

一、本地啓動redis服務;redis

二、打開redis客戶端,輸入flushall清空緩存;spring

三、瀏覽器輸入http://localhost:8080/set,設置Sessionapache

redis客戶端輸入keys *命令, 能夠發現redis中確實有數據插入:瀏覽器

四、瀏覽器輸入http://localhost:8080/query,查詢Session

五、清空redis緩存,瀏覽器輸入http://localhost:8080/query,再次查詢Session

發現HttpSession中已經無數據。

 

最後,若是查看瀏覽器的cookie的話,會發現有一個name爲「SESSION」的cookie,其值爲redis中spring session key的一部分。

 

另外,還能夠在redis客戶端輸入HGETALL來查看spring session具體的值,以下:

結論:以上測試結果所有符合預期,HttpSession的實現成功被Spring Session替換,操做HttpSession等同於操做redis中的數據。

工程源碼參考

https://github.com/peterchenhdu/spring-session-example

參考資料

http://docs.spring.io/spring-session/docs/1.3.1.BUILD-SNAPSHOT/reference/html5/

相關文章
相關標籤/搜索