|  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.fasterxml.jackson.databind.ObjectMapper; | 
|---|
|  |  |  | import com.matrix.component.redis.RedisClient; | 
|---|
|  |  |  | import com.matrix.component.redis.RedisUserLoginUtils; | 
|---|
|  |  |  | import com.matrix.core.constance.MatrixConstance; | 
|---|
|  |  |  | import com.matrix.core.pojo.AjaxResult; | 
|---|
|  |  |  | import com.matrix.core.tools.LogUtil; | 
|---|
|  |  |  | import com.matrix.core.tools.RSAUtils; | 
|---|
|  |  |  | import com.matrix.core.tools.StringUtils; | 
|---|
|  |  |  | import com.matrix.system.common.bean.SysUsers; | 
|---|
|  |  |  | import com.matrix.system.common.dao.SysUsersDao; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Value; | 
|---|
|  |  |  | import org.springframework.stereotype.Component; | 
|---|
|  |  |  | 
|---|
|  |  |  | private RedisClient redisClient; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Value("${login_private_key}") | 
|---|
|  |  |  | private String privateKey; | 
|---|
|  |  |  | private  String privateKey; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private final String TOKEN_HEADER = "Authorization"; | 
|---|
|  |  |  | private final String TOKEN_START_WITH = "Bearer "; | 
|---|
|  |  |  | @Value("${evn}") | 
|---|
|  |  |  | private String evn; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SysUsersDao sysUsersDao; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private static final String TOKEN_HEADER = "Authorization"; | 
|---|
|  |  |  | private static final String TOKEN_START_WITH = "Bearer "; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | 
|---|
|  |  |  | String token = resolveToken(request); | 
|---|
|  |  |  | if ("dev".equals(evn)) { | 
|---|
|  |  |  | SysUsers sysUsers = sysUsersDao.selectById(1022L); | 
|---|
|  |  |  | request.getSession().setAttribute(MatrixConstance.LOGIN_KEY, sysUsers); | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String token = resolveToken(request,privateKey); | 
|---|
|  |  |  | AjaxResult ajaxResult = new AjaxResult(); | 
|---|
|  |  |  | ajaxResult.setStatus(AjaxResult.STATUS_LOGIN_INVALID); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String userStr = redisClient.getCachedValue(token); | 
|---|
|  |  |  | redisClient.resetExpire(token); | 
|---|
|  |  |  | //        redisClient.resetExpire(token); | 
|---|
|  |  |  | if (StringUtils.isBlank(userStr)) { | 
|---|
|  |  |  | ajaxResult.setInfo("login time out"); | 
|---|
|  |  |  | response.getWriter().write(new ObjectMapper().writeValueAsString(ajaxResult)); | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param request | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private String resolveToken(HttpServletRequest request) { | 
|---|
|  |  |  | public static String resolveToken(HttpServletRequest request,String privateKey) { | 
|---|
|  |  |  | String headToken = request.getHeader(TOKEN_HEADER); | 
|---|
|  |  |  | StringBuffer sb = request.getRequestURL(); | 
|---|
|  |  |  | String sb = request.getRequestURI(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (StringUtils.isNotBlank(headToken) && headToken.startsWith(TOKEN_START_WITH)) { | 
|---|
|  |  |  | // 去掉令牌前缀 | 
|---|
|  |  |  | String rsaToken = headToken.replace(TOKEN_START_WITH, ""); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | String decryptTokten = new String(RSAUtils.decryptByPrivateKey(rsaToken, privateKey)); | 
|---|
|  |  |  | String decryptToken = new String(RSAUtils.decryptByPrivateKey(rsaToken, privateKey)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String[] s = decryptTokten.split("_"); | 
|---|
|  |  |  | String[] s = decryptToken.split("_"); | 
|---|
|  |  |  | if (s == null || s.length != 3) { | 
|---|
|  |  |  | return ""; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!sb.toString().equals(s[2])) { | 
|---|
|  |  |  | LogUtil.info("请求路径:{} -- {}", sb, s[2]); | 
|---|
|  |  |  | if (!sb.equals(s[2])) { | 
|---|
|  |  |  | return ""; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|