当前位置: 移动技术网 > IT编程>开发语言>Java > 详解Spring-Boot集成Spring session并存入redis

详解Spring-Boot集成Spring session并存入redis

2019年07月22日  | 移动技术网IT编程  | 我要评论

spring session 提供了一套用于管理用户 session 信息的api和实现。

spring session为企业级java应用的session管理带来了革新,使得以下的功能更加容易实现:

  1. 编写可水平扩展的原生云应用。
  2. 将session所保存的状态卸载到特定的外部session存储中,如redis或apache geode中,它们能够以独立于应用服务器的方式提供高质量的集群。
  3. 当用户使用websocket发送请求的时候,能够保持httpsession处于活跃状态。
  4. 在非web请求的处理代码中,能够访问session数据,比如在jms消息的处理代码中。
  5. 支持每个浏览器上使用多个session,从而能够很容易地构建更加丰富的终端用户体验。
  6. 控制session id如何在客户端和服务器之间进行交换,这样的话就能很容易地编写restful api,因为它可以从http 头信息中获取session id,而不必再依赖于cookie。

spring-boot集成spring session并存入redis

添加maven依赖

redis的相关依赖可以看之前的内容,这里需要增加如下依赖。

<dependency> 
   <groupid>org.springframework.session</groupid> 
   <artifactid>spring-session</artifactid> 
</dependency> 

java代码实现

增加httpsessionconfig。

package com.core.config; 
 
import org.springframework.context.annotation.bean; 
import org.springframework.session.data.redis.config.annotation.web.http.enableredishttpsession; 
import org.springframework.session.web.http.headerhttpsessionstrategy; 
import org.springframework.session.web.http.httpsessionstrategy; 
 
@enableredishttpsession(maxinactiveintervalinseconds = 100, redisnamespace = "xxxx") 
public class httpsessionconfig { 
 
  @bean 
  public httpsessionstrategy httpsessionstrategy() { 
    return new headerhttpsessionstrategy(); 
  } 
} 

其中注解 enableredishttpsession 创建了一个名为springsessionrepositoryfilter的spring bean,该bean实现了filter接口。该filter负责通过 spring session 替换httpsession从哪里返回。这里spring session是通过 redis 返回。

类中的方法 httpsessionstrategy(),用来定义spring session的 httpsession 集成使用http的头来取代使用 cookie 传送当前session信息。如果使用下面的代码,则是使用cookie来传送 session 信息。

@bean 
public httpsessionstrategy httpsessionstrategy() { 
  return new cookiehttpsessionstrategy(); 
} 

使用http的头,会看到如下信息

-- response -- 
200  
x-auth-token: 4792331e-44c2-4285-a9d1-ebabf0e72251 
content-type: text/html;charset=utf-8 
content-length: 75 
date: mon, 09 jan 2017 10:14:00 gmt 
 
8e107efb-bf1e-4a55-b896-c97f629c8e40 : 4792331e-44c2-4285-a9d1-ebabf0e72251 

使用cookie,会看到如下信息

-- response -- 
200  
set-cookie: session=4792331e-44c2-4285-a9d1-ebabf0e72251;path=/;httponly 
content-type: text/html;charset=utf-8 
content-length: 75 
date: mon, 09 jan 2017 10:47:37 gmt 

测试

在controller中增加如下代码

@getmapping("/") 
public string uid(httpservletrequest request) { 
  httpsession session = request.getsession(); 
  uuid uid = (uuid) session.getattribute("uid"); 
  if (uid == null) { 
    uid = uuid.randomuuid(); 
  } 
  session.setattribute("uid", uid); 
  return uid.tostring() + " : " + session.getid(); 
} 

启动服务,在chrome浏览器输入 http://127.0.0.1:8080/,得到sessionid

fbfae849-1d49-4301-b963-573048e763e7 

在redis中可以看到如下信息

1) "spring:session:xxxx:sessions:fbfae849-1d49-4301-b963-573048e763e7" 

2) "spring:session:xxxx:expirations:1483958700000" 

3) "spring:session:xxxx:sessions:expires:fbfae849-1d49-4301-b963-573048e763e7" 

打开火狐的httprequester,使用chrome获取的sessionid点击get,可以看到如下输出

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网