1.session

shiro提供的session不依赖web容器,可以直接使用,如果是在web环境下,session中的数据和httpsession中的数据是通的。Shiro中的session可以出现在任何地方,例如service、dao等,不需要从controller中传递session参数,用户保存在session中的数据可以在HTTP session中获取,保存在httpsession中的数据也可以从session中获取。

2.session常用方法

在这里插入图片描述

api说明
startTimestampsession的创建时间
stopTimestampsession的失效时间
lastAccessTimesession的最近一次访问时间,初始值是startTimestamp
timeoutsession的有效时长,默认30分钟
attributessession的属性容器
touch刷新
stop销毁会话,Subject.logout()时会自动调用stop方法来销毁会话

3.实现登录成功后保存登录信息到session中

String userName = (String) subject.getPrincipal();
System.out.println("登录账户:"+userName);
Session session = subject.getSession();
session.setAttribute("userName", userName);

4.用户注销

// 注销用户
@RequestMapping("/logout")
public void exitUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
Subject subject = SecurityUtils.getSubject();
      //退出
      subject.logout();
	response.getWriter().write("<script>window.parent.frames.location.href='" +   	   request.getContextPath() + "/login.jsp';</script>");
}
Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐