| | |
| | | * 登出 |
| | | * |
| | | * @param req |
| | | * @param res |
| | | * @return |
| | | */ |
| | | @GetMapping(value = "/logout") |
| | | public ResponseMsg<Boolean> logout(HttpServletRequest req) { |
| | | public ResponseMsg<Boolean> logout(HttpServletRequest req, HttpServletResponse res) { |
| | | try { |
| | | String token = WebHelper.getToken(req); |
| | | if (StringHelper.isEmpty(token)) { |
| | | return fail("没有检测到令牌", false); |
| | | } |
| | | |
| | | Boolean flag = tokenService.logout(token, req); |
| | | Boolean flag = tokenService.logout(token, req, res); |
| | | |
| | | return success(flag ? "登出成功" : "登出失败", flag); |
| | | } catch (Exception ex) { |
| | |
| | | */ |
| | | public static void saveToken2Cookie(String token, HttpServletRequest request, HttpServletResponse response) { |
| | | // 先删除 |
| | | deleteCookie(StaticData.TOKEN_COOKIE_KEY, request); |
| | | deleteCookie(StaticData.TOKEN_COOKIE_KEY, request, response); |
| | | |
| | | // 再保存 |
| | | saveCookie(StaticData.TOKEN_COOKIE_KEY, token, response); |
| | |
| | | /** |
| | | * 保存Cookie |
| | | * |
| | | * @param cookieKey |
| | | * @param key |
| | | * @param value |
| | | * @param response |
| | | */ |
| | | public static void saveCookie(String cookieKey, String value, HttpServletResponse response) { |
| | | Cookie cookie = new Cookie(cookieKey, value); |
| | | public static void saveCookie(String key, String value, HttpServletResponse response) { |
| | | Cookie cookie = new Cookie(key, value); |
| | | // 设置cookie失效时间,单位为秒 |
| | | cookie.setMaxAge(4 * 60 * 60); |
| | | cookie.setHttpOnly(false); |
| | | cookie.setPath("/"); |
| | | // cookie.setDomain("") |
| | | //cookie.setDomain("*") |
| | | |
| | | response.setHeader("P3P", "CP=CAO PSA OUR"); |
| | | response.addCookie(cookie); |
| | |
| | | * @param cookieKey |
| | | * @param request |
| | | */ |
| | | public static void deleteCookie(String cookieKey, HttpServletRequest request) { |
| | | public static void deleteCookie(String cookieKey, HttpServletRequest request, HttpServletResponse response) { |
| | | Cookie[] cookies = request.getCookies(); |
| | | if (cookies != null && cookies.length > 0) { |
| | | for (Cookie c : cookies) { |
| | | if (cookieKey.equalsIgnoreCase(c.getName())) { |
| | | c.setMaxAge(0); |
| | | c.setPath("/"); |
| | | response.addCookie(c); |
| | | } |
| | | } |
| | | } |
| | |
| | | * @param req |
| | | * @return |
| | | */ |
| | | public Boolean logout(String token, HttpServletRequest req) { |
| | | public Boolean logout(String token, HttpServletRequest req, HttpServletResponse res) { |
| | | TokenEntity te = getEntityByToken(token); |
| | | if (te == null) { |
| | | return false; |
| | | } |
| | | |
| | | // 清除Cookie |
| | | WebHelper.deleteCookie(StaticData.TOKEN_COOKIE_KEY, req); |
| | | // 清除Cookie WebHelper.saveCookie(StaticData.TOKEN_COOKIE_KEY, "", 60, res) |
| | | WebHelper.deleteCookie(StaticData.TOKEN_COOKIE_KEY, req, res); |
| | | |
| | | // 获取当前用户 |
| | | UsersEntity ue = getCurrentUser(req); |
| | |
| | | */ |
| | | public UsersEntity getCurrentUser(HttpServletRequest req) { |
| | | String token = WebHelper.getToken(req); |
| | | if (StringHelper.isEmpty(token)) { |
| | | if (StringHelper.isNull(token)) { |
| | | return null; |
| | | } |
| | | |
| | |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | document.onkeydown = function (e) { |
| | | var ev = window.event || e; |
| | | var code = ev.keyCode || ev.which || ev.charCode; |
| | | if (code == 13) { |
| | | sysLogin(); |
| | | } |
| | | } |
| | | </script> |
| | | </body> |
| | | </html> |