package com.hai.common.security; import com.fasterxml.jackson.core.JsonParseException; import com.hai.common.exception.ErrorCode; import com.hai.common.exception.ErrorHelp; import com.hai.common.exception.SysCode; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Component public class UserCenter { private static Logger log = LoggerFactory.getLogger(UserCenter.class); private static final String COOKIE_FIELD = "_ida"; private static final int EXPIRE = 43200;//过期时间为12小时 /** * 判断用户是否登录,并且不能单个用户同时登录 * @param request * @return boolean * @throws Exception */ public static boolean isLogin(HttpServletRequest request){ String token = request.getHeader("Authorization"); if(StringUtils.isBlank(token)){ Cookie cookie = CookieUtil.getCookieByName(request, COOKIE_FIELD); if(cookie != null && StringUtils.isNotBlank(cookie.getValue())){ token = cookie.getValue(); } } if(StringUtils.isBlank(token)){ return false; } try { // String value = AESEncodeUtil.aesDecrypt(token); if (!StringUtils.isEmpty(token)) { SessionObject cacheStr = LoginCache.getData(token); if (cacheStr != null) { LoginCache.setData(token, cacheStr, EXPIRE);//刷新缓存 return true; } } }catch (Exception e){ e.printStackTrace(); } return false; } /** * * @Title: isTokenLogin * @Description: 支持token形式的校验登录 * @author: 机器猫 * @param: @param token * @param: @return * @param: @throws Exception * @return: boolean * @throws */ public static boolean isTokenLogin(String token){ try { // String value = AESEncodeUtil.aesDecrypt(token); // log.error("------------------------------" + value); if(!StringUtils.isEmpty(token)){ SessionObject cacheStr = LoginCache.getData(token); if(cacheStr != null){ log.error("======================" + cacheStr); LoginCache.setData(token, cacheStr, EXPIRE);//刷新缓存 return true; } } return false; } catch (Exception e) { log.error("isTokenLogin failed",e); return false; } } public static String read(HttpServletRequest request) throws Exception{ Cookie cookie = CookieUtil.getCookieByName(request, COOKIE_FIELD); if(cookie == null){ return null; } return cookie.getValue(); } public static SessionObject getSessionObject(HttpServletRequest request) throws Exception { String token = request.getHeader("Authorization"); if (StringUtils.isBlank(token)) { if (StringUtils.isNotBlank(read(request))) { token = read(request); } } if (LoginCache.getData(token) == null) { throw ErrorHelp.genException(SysCode.System, ErrorCode.USE_VISIT_ILLEGAL, ""); } return LoginCache.getData(token); } /** * @param request * @param response * @param response * @param seObj * @throws Exception */ public static void save(HttpServletRequest request, HttpServletResponse response, SessionObject seObj) throws Exception{ CookieUtil.saveCookie(request, response, COOKIE_FIELD, seObj.getUniqueCode(), EXPIRE); LoginCache.setData(seObj.getUniqueCode(), seObj, EXPIRE); } /** * 刷新cookie信息 * * @param request * @param response * @throws Exception */ public static void refreshCookie(HttpServletRequest request, HttpServletResponse response) throws Exception{ CookieUtil.refreshCookie(request, response, COOKIE_FIELD, EXPIRE); } /** * @param request * @param response * @throws Exception */ public static void remove(HttpServletRequest request, HttpServletResponse response) throws Exception{ String token = request.getHeader("Authorization"); LoginCache.clear(AESEncodeUtil.aesDecrypt(token)); CookieUtil.delCookie(response, COOKIE_FIELD); } }