每天学点Shiro-Session管理
1. 在之前创立的TestService中添加关于Session的打印方法
public void testSessionPrint(){ Session session = SecurityUtils.getSubject().getSession() ; System.out.println("=====================session================="); System.out.println("httpsession:" + session.getAttribute("httpsession")); System.out.println("session host : " + session.getHost()); System.out.println("session lastAccessTime : " + session.getLastAccessTime()); System.out.println("session startTimestamp : " + session.getStartTimestamp()); }
2. 在Controller类中进行testSessionPrint方法的调用,并且在HttpSession中添加一条测试的 key-value
@RequestMapping("/{page}.do") public String getIndexPage(@PathVariable("page") String page, HttpSession session){ try { session.setAttribute("httpsession","testhttpsession"); testService.testTimePrint(); testService.testSessionPrint(); } catch (Exception e) { System.out.println(e.getMessage()); } return page ; }
3. 打印结果如下:
=====================session================= session host : 127.0.0.1 httpsession:testhttpsession session lastAccessTime : Sun Oct 01 14:08:53 CST 2017 session startTimestamp : Sun Oct 01 14:08:50 CST 2017
4. 可以看出基于shiro的session可以获取到host,访问时间等信息,而且能获取到主动放入HttpSession的信息