@RequestMapping("/loginByEmail")
@GlobalInterceptor(checkParams = true)
public ResponseVO loginByEmail(HttpSession httpSession,
HttpServletRequest request,
@VerifyParam(required = true, param = "email") String email,
@VerifyParam(required = true, param = "password") String password,
@VerifyParam(required = true, param = "checkCode") String checkCode) {
try {
// if (!checkCode.equalsIgnoreCase((String) httpSession.getAttribute(CHECK_CODE_KEY))) {
// throw new BusinessException("wrong code");
// }
String token = loginService.loginByEmail(email, password, getIpAddress(request));
httpSession.setAttribute(Constants.SESSION_KEY, token);
return getSuccessResponseVO(token);
} finally {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
redisTemplate.opsForValue().set("2","2");
redisTemplate.expire("2", 1, TimeUnit.DAYS);
System.out.println(RedisPrefixConstants.SPRING_SESSION_SESSIONS + httpSession.getId());
httpSession.removeAttribute(CHECK_CODE_KEY);
}
}
我尝试测试是否可以在redis中存储数据并更改它的ttl并且我成功了,但是当我更改spring会话在redis中存储的数据时,我无法更改它。密钥肯定是正确的,因为我已经比较过。我不知道为什么我不能改变它。
好吧,我得到了答案。 这里是: Spring会话中数据的TTL不能通过redisTemplate.expire()方法修改,因为spring会话中的TTL是由其自身的maxInactiveInterval控制的。所以如果你想改变他的时间,就需要直接改变session过期时间来控制他的TTL。
您可以使用:
httpSession.setMaxInactiveInterval(300000); // 3000000 in seconds