40253dd03b92cd16efc43404494459a15255d267.svn-base 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package org.jeecg.common.util;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.commons.lang3.StringUtils;
  4. import org.apache.shiro.authc.AuthenticationException;
  5. import org.jeecg.common.api.CommonAPI;
  6. import org.jeecg.common.constant.CommonConstant;
  7. import org.jeecg.common.system.util.JwtUtil;
  8. import org.jeecg.common.system.vo.LoginUser;
  9. import javax.servlet.http.HttpServletRequest;
  10. /**
  11. * @Author scott
  12. * @Date 2019/9/23 14:12
  13. * @Description: 编程校验token有效性
  14. */
  15. @Slf4j
  16. public class TokenUtils {
  17. /**
  18. * 获取 request 里传递的 token
  19. *
  20. * @param request
  21. * @return
  22. */
  23. public static String getTokenByRequest(HttpServletRequest request) {
  24. String token = request.getParameter("token");
  25. if (token == null) {
  26. token = request.getHeader("X-Access-Token");
  27. }
  28. return token;
  29. }
  30. /**
  31. * 验证Token
  32. */
  33. public static boolean verifyToken(HttpServletRequest request, CommonAPI commonAPI, RedisUtil redisUtil) {
  34. log.debug(" -- url --" + request.getRequestURL());
  35. String token = getTokenByRequest(request);
  36. if (StringUtils.isBlank(token)) {
  37. throw new AuthenticationException("Token不能为空!");
  38. }
  39. // 解密获得username,用于和数据库进行对比
  40. String username = JwtUtil.getUsername(token);
  41. if (username == null) {
  42. throw new AuthenticationException("Token非法无效!");
  43. }
  44. // 查询用户信息
  45. LoginUser user = commonAPI.getUserByName(username);
  46. if (user == null) {
  47. throw new AuthenticationException("用户不存在!");
  48. }
  49. // 判断用户状态
  50. if (user.getStatus() != 1) {
  51. throw new AuthenticationException("账号已锁定,请联系管理员!");
  52. }
  53. // 校验token是否超时失效 & 或者账号密码是否错误
  54. if (!jwtTokenRefresh(token, username, user.getPassword(), redisUtil)) {
  55. throw new AuthenticationException("Token失效,请重新登录");
  56. }
  57. return true;
  58. }
  59. /**
  60. * 刷新token(保证用户在线操作不掉线)
  61. * @param token
  62. * @param userName
  63. * @param passWord
  64. * @param redisUtil
  65. * @return
  66. */
  67. private static boolean jwtTokenRefresh(String token, String userName, String passWord, RedisUtil redisUtil) {
  68. String cacheToken = String.valueOf(redisUtil.get(CommonConstant.PREFIX_USER_TOKEN + token));
  69. if (oConvertUtils.isNotEmpty(cacheToken)) {
  70. // 校验token有效性
  71. if (!JwtUtil.verify(cacheToken, userName, passWord)) {
  72. String newAuthorization = JwtUtil.sign(userName, passWord);
  73. // 设置Toekn缓存有效时间
  74. redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, newAuthorization);
  75. redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);
  76. }
  77. //update-begin--Author:scott Date:20191005 for:解决每次请求,都重写redis中 token缓存问题
  78. // else {
  79. // redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, cacheToken);
  80. // // 设置超时时间
  81. // redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 1000);
  82. // }
  83. //update-end--Author:scott Date:20191005 for:解决每次请求,都重写redis中 token缓存问题
  84. return true;
  85. }
  86. return false;
  87. }
  88. /**
  89. * 验证Token
  90. */
  91. public static boolean verifyToken(String token, CommonAPI commonAPI, RedisUtil redisUtil) {
  92. if (StringUtils.isBlank(token)) {
  93. throw new AuthenticationException("token不能为空!");
  94. }
  95. // 解密获得username,用于和数据库进行对比
  96. String username = JwtUtil.getUsername(token);
  97. if (username == null) {
  98. throw new AuthenticationException("token非法无效!");
  99. }
  100. // 查询用户信息
  101. LoginUser user = commonAPI.getUserByName(username);
  102. if (user == null) {
  103. throw new AuthenticationException("用户不存在!");
  104. }
  105. // 判断用户状态
  106. if (user.getStatus() != 1) {
  107. throw new AuthenticationException("账号已被锁定,请联系管理员!");
  108. }
  109. // 校验token是否超时失效 & 或者账号密码是否错误
  110. if (!jwtTokenRefresh(token, username, user.getPassword(), redisUtil)) {
  111. throw new AuthenticationException("Token失效,请重新登录!");
  112. }
  113. return true;
  114. }
  115. }