Spring Session,springsession

來源:互聯網
上載者:User

Spring Session,springsession
目的

使用Redis儲存管理HttpSession;

添加pom.xml

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

引入的依賴有spring-session、spring-boot-starter-web、spring-boot-starter-redis,pom檔案如下:

<?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來實現的。

import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;@EnableRedisHttpSessionpublic class HttpSessionConfig {    }
操作HttpSession

這裡我們將實現兩個操作,一個是往Session中寫入資料,另一個是查詢資料,如下所示:

@RestControllerpublic 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等註解

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class APP {       public static void main(String[] args) throws Exception {            SpringApplication.run(APP.class, args);        }}
運行程式,測實驗證

1、本地啟動redis服務;

2、開啟redis用戶端,輸入flushall清空緩衝;

3、瀏覽器輸入http://localhost:8080/set,設定Session

redis用戶端輸入keys *命令, 可以發現redis中確實有資料插入:

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

5、清空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/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.