Today, record the springSession of learning and the pits encountered
1.SpringSession depends on importing pom files
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
The following three blogs are more clear about SpringSession. You can refer to
blog 1
blog 2
blog 3
In addition
springboot configuration filter (existing class)
I did n’t understand it before, I thought I had to configure the filter to wrap the HttpSession, at least Spring did it in xml, so I found a lot of information, and finally understood that it was easy to get a comment in the original springboot (crying) , But learning is still good.
springboot configuration filter
pit! !
The most important pit is here. After I added the springSession dependency, the project couldn't run.
It turned out that there was a conflict between springSession and the redis-cli package. The version is solved.
Coding!
In the configuration file:
# Session store type.
spring.session.store-type=redis
# Redis server host.
spring.redis.host=localhost
# Login password of the redis server.
#spring.redis.password=
# Redis server port.
spring.redis.port=6379
#springSessionTimeout
server.session.timeout=36000
I wo n’t talk about it here. The blogs above are all introduced. I did n’t use the redis password here, so I annotated this step.
The following uses SpringSession to log in, log out, and obtain user information
@RestController
@RequestMapping("/user/springSession")
public class UserSpringSessionController {
@Resource
private UserServiceImpl userService;
@RequestMapping(value = "/login.do",method = RequestMethod.POST)
public ServerResponse login(@RequestParam String username, @RequestParam String password, HttpSession session , HttpServletResponse httpServletResponse){
ServerResponse response=userService.login(username,password);
if (response.isSuccess()){
session.setAttribute(Const.CURRENT_USER,response.getData());
//sessionId= 3D5D2DBC9DF714996B58FA5297920E36
// CookieUtil.writeLoginToken(httpServletResponse,session.getId());
// RedisPoolUtil.setEx(session.getId(), JsonUtil.object2String(response.getData()), Const.ReidsCacheExTime.REDIS_SESSION_EXTIME);
}
return response;
}
@RequestMapping(value = "/logout.do")
public ServerResponse logout(HttpServletRequest request, HttpServletResponse response, HttpSession session ){
session.removeAttribute(Const.CURRENT_USER);
// String loginToken=CookieUtil.readLoginToken(request);
// CookieUtil.delLoginToken(response,request);
// RedisPoolUtil.del(loginToken);
return ServerResponse.success(null);}
@RequestMapping(value = "/get_user_info.do")
public ServerResponse getUserInfo(HttpSession session){
User user=(User) session.getAttribute(Const.CURRENT_USER);
if (user==null){
return ServerResponse.error("用户未登录");
}
return ServerResponse.success(user);
}
}
The operation is quite simple, that is, directly operating the session, adding and deleting user information in the session.
It will automatically add values in redis.
A total of three keys are added. When it expires, two keys will expire first, and one is left, but that one is useless. The two expired times are the set expiretime