The basic idea is:
1. When logging in, set session and cookie and a static variable to store the generated sessionId value;
2. Because the session life cycle is over when the browser is closed, we need to use cookie s to access the value of sessionId, and then set the value in the static variable map for later query
3. In the filter, you need to get the value of the specified cookie to get the sessionId, then get the information in the static variable according to the sessionId, and then store it in the session.
The code is as follows:
Login background code:
@Controller @RequestMapping("site") public class SiteIndexController extends PressyunController { @Inject private DnaUserService dnaUserService; public static Map<String, DnaUser> sessionUsermap = new HashMap<>(); @LogOperationRequired(value = "Land") @RequestMapping(value = "/login", method = RequestMethod.POST) public String login(@RequestParam("username") String username, @RequestParam("password") String password,HttpServletRequest request, HttpServletResponse response, Map<String, Object> viewData)throws UnsupportedEncodingException { Map<String, Object> map = new HashMap<String, Object>(); DnaUser dnaUser = dnaUserService.getUserByLogin(username, password); dnaUser.setPassword(null); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); HttpSession session = request.getSession(); request.getSession().setAttribute("dnaUser", dnaUser); session.setMaxInactiveInterval(3600 * 24 * 10);// 10 days Cookie sessionIdCookie = new Cookie("dnasessionId", session.getId());//Set sessionId for page sessionIdCookie.setMaxAge(60 * 60 * 24 * 10); sessionIdCookie.setPath("/"); response.addCookie(sessionIdCookie); sessionUsermap.put(session.getId(), dnaUser);//Add values to static variables return "redirect:index"; } }
Interceptor background code: if the value of sessionid is found and there is a value in the static variable, the user can log in directly and set the value in the session
import javax.annotation.Resource; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; public class LoginInterceptor extends HandlerInterceptorAdapter { @Resource private DnaUserService userService; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return login(request);//This will be done before entering the controller layer } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } private boolean login(HttpServletRequest request) { //Get local cookies String sessionIdStr = ""; Cookie[] cookies = request.getCookies(); if (null != cookies) { for (Cookie cookie : cookies) { if ("dnasessionId".equals(cookie.getName())) { sessionIdStr = cookie.getValue(); } } } DnaUser loginUser = SiteIndexController.sessionUsermap.get(sessionIdStr);//Take value in static variable according to session if (loginUser != null) { HttpSession session = request.getSession(); request.getSession().setAttribute(Constant.SITE_USER, loginUser);//Set the session after taking the value session.setMaxInactiveInterval(3600 * 24 * 10);// 10 days } return true; } }
Homepage jsp code: if there is no value in the session, skip to the login interface
<%@page import="com.ppress.example.po.DnaUser"%> <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <link rel="stylesheet" type="text/css" href="content/site/common/css/component.css"/> <link rel="stylesheet" type="text/css" href="content/site/common/css/index.css?v=201803151600"/> <script src="content/site/common/js/common/modernizr.custom.js"></script> <script src="content/site/common/js/common/jquery.dlmenu.js"></script> <% DnaUser dnaUser = null; String userName = null; if(session.getAttribute("dnaUser") != null){ dnaUser = (DnaUser)session.getAttribute("dnaUser"); userName = dnaUser.getAccount(); } %> <div class="navbar navbar-fixed-top"> <div class="navbar-inner"> <div class="container-fluid"> <a href="/" class="logo"><img src="/content/site/common/images/system/logo.png" width="40" height="40">XX platform</a> <div class="top-menu visible-desktop"> <ul class="pull-right"> <li><a href="javascript:;"> <% if("admin".equals(userName)){ %> ${dnaUser.account}(Administrator) <% }else{ %> ${dnaUser.account}(User) <% } %> </a></li> <li><a href="/site/logout"><i class="icon-off"></i> Sign out</a></li> </ul> </div> </div> </div> </div> <script type="text/javascript"> $(function () { <% if(request.getAttribute("dnaUser")==null){ %> location.href = "/site/login"; <% } %> }); </script>
Logout background operation:
@RequestMapping(value = "/logout") public String logout(HttpServletRequest request, HttpServletResponse response, Map<String, Object> viewData) { request.getSession().invalidate(); String sessionIdStr = ""; Cookie[] cookies = request.getCookies(); if (null != cookies) { for (Cookie cookie : cookies) { if ("dnasessionId".equals(cookie.getName())) { sessionIdStr = cookie.getValue(); sessionUsermap.remove(sessionIdStr);//Remove values from static variables } } } Cookie deleteNewCookie = new Cookie("dnasessionId", null); deleteNewCookie.setMaxAge(0); // Delete the Cookie deleteNewCookie.setPath("/"); response.addCookie(deleteNewCookie); return "redirect:login"; }