Spring Session

来源:互联网 发布:梦想小镇淘宝充值 编辑:程序博客网 时间:2024/06/06 14:12

目的

使用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/

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小米note3不充电怎么办 网易云海外会员怎么办 百度云支付失败怎么办 信用卡输入密码错误怎么办 相机储存卡被锁怎么办 sd卡检测不到怎么办 手机图标闪退怎么办 电视看爱奇艺版权受限怎么办 王者荣耀重复id怎么办 拼多多资源位下架怎么办 百度云网络异常怎么办 公开课没上好怎么办 ios云备份失败怎么办 网易云登录异常怎么办? 阴阳师网易账号冻结怎么办 梦幻账号被冻结怎么办 手机忘记解锁密码怎么办 胃疼持续一天怎么办 胃痛一天一夜怎么办 孩子爱玩电脑怎么办 ipad不显示画面怎么办 正版win10换电脑怎么办 电脑系统不是正版怎么办 苹果平板进水了怎么办 华为平板进水了怎么办 三星平板进水了怎么办 笔记本cpu运行过高怎么办 微博永久性封号怎么办 电脑占用内存多怎么办 直播签约后悔了怎么办 移动宽带网络不稳定怎么办 股票帐号被锁定怎么办 电脑帐号锁定了怎么办 云校家帐号被锁定怎么办 qq音乐停止运行怎么办 做事效率低怎么办教案 孩子做事效率低怎么办 百度搜不到答案怎么办 学乐云账号忘了怎么办 wps云空间不足怎么办 超星密码忘了怎么办