Spring Session, springsession

Source: Internet
Author: User

Spring Session, springsession
Purpose

Use Redis storage to manage HttpSession;

Add pom. xml

This project is based on Spring Boot, and we will use Spring IO Platform to maintain the dependency version number;

The introduced dependencies include spring-session, spring-boot-starter-web, and spring-boot-starter-redis. The pom file is as follows:

<?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>
Configure Spring Session

The configuration is simple. You can add the @ EnableRedisHttpSession annotation. This annotation creates a Spring Bean named springSessionRepositoryFilter, which is actually a Filter, this Filter is used to replace the original default HttpSession implementation with Spring Session. In this example, Spring Session is implemented using Redis.

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

Here we will implement two operations: one is to write data to the Session, and the other is to query data, as shown below:

@ RestControllerpublic class Example {@ RequestMapping ("/set") String set (HttpServletRequest req) {req. getSession (). setAttribute ("testKey", "testValue"); return "sets session: testKey = testValue";} @ RequestMapping ("/query") String query (HttpServletRequest req) {Object value = req. getSession (). getAttribute ("testKey"); return "query Session: \" testKey \ "=" + value ;}}
Compile the main method

Compile the main method and use the @ SpringBootApplication annotation. If you view the source code of the annotation, you will find that the annotation such as @ SpringBootConfiguration @ EnableAutoConfiguration @ ComponentScan is added.

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);        }}
Run the program and test the verification.

1. Start the redis Service locally;

2. Open the redis client and enter flushall to clear the cache;

3. Enter http: // localhost: 8080/set in the browser and set the Session

Run the keys * command on the redis client to check that data is inserted in redis:

4. Enter http: // localhost: 8080/query in the browser to query the Session

5. Clear the redis cache, enter http: // localhost: 8080/query in the browser, and query the Session again.

No data is found in HttpSession.

 

Finally, if you view the browser's cookie, you will find that there is a cookie named "SESSION", and its value is part of the spring session key in redis.

 

In addition, you can enter hgetall on the redis client to view the specific value of spring session, as shown below:

Conclusion: All the above test results are as expected. The successful implementation of HttpSession is replaced by Spring Session, and the operation of HttpSession is equivalent to the operation of data in redis.

Project source code reference

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

References

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.