写在前面
在web的开发过程中session和cookie一直都是普遍被选择的,对于服务端来说session的使用更由来已久,从传统的单节点点tomcat到集群化的使用。本文简单说明一下使用spring-session&redis的方式。
搭建:
引入jar包,由于使用maven管理,直接在pom中引入:
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
在配置文件中设置对应的redis配置。
注意:在配置的时候要注意使用的database 可以不配置默认使用0就好了,注意要使用密码进行登录。
spring:
redis:
host: 127.0.0.01
port: 6379
password: password
timeout: 10000
database: 0
jedis:
pool:
max-active: 20000
max-wait: -1
max-idle: 10
min-idle: 0
下面是按照个人的习惯设置的一个bean。
@Bean
public DefaultCookieSerializer defaultCookieSerializer() {
DefaultCookieSerializer defaultCookieSerializer = new DefaultCookieSerializer();
defaultCookieSerializer.setCookiePath("/");
defaultCookieSerializer.setUseHttpOnlyCookie(false);
return defaultCookieSerializer;
}
到此配置已经完成了,已经可以使用了。
**
验证
**
1、编写一段简单的代码用来生成session,检验session在使用的时候是否已经生效。
简单代码,请忽略其中的魔法值
// 检查session是否生效,返回sessionid
// 生成session 返回sessionid
@GetMapping("/session")
public Object session() {
HttpSession httpSession = request.getSession(true);
if (httpSession == null ) {
return new Date();
}
httpSession.setAttribute("abc","abc");
System.out.println(httpSession.getId());
return "sessionId为:"+httpSession.getId();
}
// 检查session是否存在,如果存在那么session中的数据是否是开始的时候存入的。
@GetMapping("/checkSession")
public Object checkSession() {
System.out.println(request.getCookies());
HttpSession httpSession = request.getSession(false);
if (httpSession == null ) {
return "no session";
}
System.out.println(httpSession.getId());
return "sessionId为:"+httpSession.getId()+"session中的abc为"+httpSession.getAttribute("abc");
}
验证的时候(使用postman来验证)发现结果是按照预想的实现的,下面简单贴个图来说明:
未进行session生成的时候,没有返回cookie,也获取不到相关的参数。
下面在调用一次生成之后,就可以查询到相关的结果了。
此时检查redis中发现数据也存在了。
**
使用
**
简单说明一下在项目中的说明:
设置session中存储的内容
public void setAttribute(String name, Object value);
设置session的过期时间
public void setMaxInactiveInterval(int interval);
将session设置为无效
public void invalidate();
这几个主要的设置,当然在设置中也有别的其他设置,请使用的时候根据需要自行设置。
**
其他问题
**
1、在使用的过程中,注意
request.getSession(true/false);
里面的true or false 参数,如果不是想自动生成一个新的session一定要设置为false。
在有些情况不使用false,可以通过session.isNew()来判断是否是新生成的session来处理相关的业务逻辑。
2、sessionid问题,上面的验证过程中可以发现,cookie中返回的sessionid 和我们自己通过getId()获取到的id是不同的。
当然我们可以通过调整cookie中的设置
defaultCookieSerializer.setUseBase64Encoding(false);来决定是否对ID进行编码;
我们也可以通过下面的base64来对sessionid进行相互转换。这也引出如果我们不是通过cookie的方式来获得sessionid 也可以进行session的使用和管理。
验证和说明:
@Autowired
private SessionRepository sessionRepositry;
@GetMapping("/checkSession2")
public Object checkSession2() {
System.out.println(request.getCookies());
Cookie[] cookies = request.getCookies();
Cookie sessionIds = cookies[0];
String sessionId = sessionIds.getValue();
System.out.println("cookie中的sessionid为:"+sessionId);
System.out.println("cookie中的sessionid经过base64decode之后为:"+base64Decode(sessionId));
HttpSession httpSession = request.getSession(false);
Session session = sessionRepositry.findById(base64Decode(sessionId));
if (session == null ) {
return "no session";
}
if (httpSession == null ) {
return "no http session";
}
return "httpsessionId为:"+httpSession.getId()+"session中的sessionid为"+session.getId();
}
private static String base64Decode(String base64Value) {
try {
byte[] decodedCookieBytes = Base64.getDecoder().decode(base64Value);
return new String(decodedCookieBytes);
}
catch (Exception e) {
return null;
}
}
调用上面的程序可以发现返回的结果为:
httpsessionId为:b10c601f-f97d-4389-a50f-8d7075379562session中的sessionid为b10c601f-f97d-4389-a50f-8d7075379562
可以看到返回的两个session的id是一致的,也就是说这一次请求通过两种方式来获取session发现获取的是同一个绘画;
这样我们可以通过自定义的参数和自定义的编码或者加密来处理sessionID。
控制台上打印的是:
cookie中的sessionid为:YjEwYzYwMWYtZjk3ZC00Mzg5LWE1MGYtOGQ3MDc1Mzc5NTYy
cookie中的sessionid经过base64decode之后为:b10c601f-f97d-4389-a50f-8d7075379562
由此可见sessionid确实可以通过base64进行编码和解码。在使用的时候要注意这一点。
3、session的刷新时间
使用httpsession的时候,可以通过getsession()来自动刷新最后操作时间;
但是如果使用的是 Session 那么就需要自己来刷新最后操作时间,否则时间不会发生变化:session.setLastAccessedTime();
上面只是从实际的使用出发,做了简单的说明,具体在项目中使用的时候,需要根据具体的情况进行调整,比如什么时候进行session的生成,什么的时候销毁,怎么设置过期时间等等。