ThirdSessionInterceptor.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package interceptor;
  2. import annotation.ApiLogin;
  3. import cn.hutool.core.util.StrUtil;
  4. import cn.hutool.json.JSONUtil;
  5. import com.alibaba.druid.support.json.JSONUtils;
  6. import com.util.ThirdSessionHolder;
  7. import lombok.AllArgsConstructor;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.apache.shiro.authc.AuthenticationException;
  10. import org.jeecg.common.constant.CommonConstant;
  11. import org.jeecg.common.exception.JeecgBoot401Exception;
  12. import org.jeecg.common.exception.JeecgBootException;
  13. import org.jeecg.common.system.util.JwtUtil;
  14. import org.jeecg.common.util.RedisUtil;
  15. import org.jeecg.common.util.TenantContextHolder;
  16. import org.jeecg.modules.wxuser.entity.ThirdSession;
  17. import org.springframework.data.redis.core.RedisTemplate;
  18. import org.springframework.http.MediaType;
  19. import org.springframework.stereotype.Component;
  20. import org.springframework.web.method.HandlerMethod;
  21. import org.springframework.web.servlet.HandlerInterceptor;
  22. import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
  23. import javax.annotation.Resource;
  24. import javax.servlet.http.HttpServletRequest;
  25. import javax.servlet.http.HttpServletResponse;
  26. import java.io.IOException;
  27. import java.io.PrintWriter;
  28. import java.util.concurrent.TimeUnit;
  29. /**
  30. * ThirdSession拦截器,校验每个请求的ThirdSession
  31. * @author
  32. */
  33. @Slf4j
  34. //@AllArgsConstructor
  35. @Component
  36. public class ThirdSessionInterceptor implements HandlerInterceptor {
  37. //public class ThirdSessionInterceptor extends HandlerInterceptorAdapter {
  38. // private final RedisTemplate redisTemplate;
  39. @Resource
  40. private RedisUtil redisUtil;
  41. @Override
  42. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  43. // if (!(handler instanceof HandlerMethod)) {
  44. // return super.preHandle(request, response, handler);
  45. // }
  46. if ((handler instanceof HandlerMethod)) {
  47. HandlerMethod method = (HandlerMethod) handler;
  48. //判断访问的control是否添加ApiLogin注解
  49. ApiLogin apiLogin = method.getMethodAnnotation(ApiLogin.class);
  50. String appIdHeader = request.getHeader("app-id");
  51. //小程序端的所有接口需要登录才能访问,校验thirdSession
  52. return this.judeSession(request, response, apiLogin);
  53. }
  54. return Boolean.TRUE;
  55. }
  56. /**
  57. * 校验session
  58. * @param request
  59. * @param response
  60. * @return
  61. * @throws IOException
  62. */
  63. private boolean judeSession(HttpServletRequest request, HttpServletResponse response, ApiLogin apiLogin) throws IOException {
  64. //获取header中的ThirdSession
  65. String thirdSessionHeader = request.getHeader("third-session");
  66. if (StrUtil.isNotBlank(thirdSessionHeader)) {
  67. //获取缓存中的ThirdSession
  68. String key = CommonConstant.PREFIX_WX_APP_USER_TOKEN + thirdSessionHeader;
  69. Object thirdSessionObj = redisUtil.get(key);
  70. if (thirdSessionObj == null) {//session过期
  71. ThirdSessionHolder.clear();
  72. this.writerPrint(response, "登录超时,请重新登录");
  73. return Boolean.FALSE;
  74. } else {
  75. String thirdSessionStr = String.valueOf(thirdSessionObj);
  76. ThirdSession thirdSession = JSONUtil.toBean(thirdSessionStr, ThirdSession.class);
  77. //判断session是否属于当前tenantId、appId
  78. String tenantIdHeader = request.getHeader("tenant-id");
  79. if (StrUtil.isNotBlank(tenantIdHeader) && !thirdSession.getTenantId().equals(tenantIdHeader)) {
  80. this.writerPrint(response, "登录超时,请重新登录");
  81. return Boolean.FALSE;
  82. }
  83. String appIdHeader = request.getHeader("app-id");
  84. if (StrUtil.isNotBlank(appIdHeader) && StrUtil.isNotBlank(thirdSession.getAppId()) && !thirdSession.getAppId().equals(appIdHeader)) {
  85. this.writerPrint(response, "登录超时,请重新登录");
  86. return Boolean.FALSE;
  87. }
  88. redisUtil.expire(key, JwtUtil.EXPIRE_TIME * 24 * 30 / 1000);//更新session过期时间
  89. TenantContextHolder.setTenantId(thirdSession.getTenantId());//设置租户ID
  90. ThirdSessionHolder.setThirdSession(thirdSession);//设置thirdSession
  91. if (apiLogin != null && apiLogin.mustLogin()) {
  92. //此接口必须登录商城才能访问
  93. return this.judeSessionUserMall(response, thirdSession);
  94. }
  95. return Boolean.TRUE;
  96. }
  97. } else {
  98. this.writerPrint(response, "session不能为空");
  99. return Boolean.FALSE;
  100. }
  101. }
  102. /**
  103. * 校验session是否商城登录
  104. * @param thirdSession
  105. * @return
  106. * @throws IOException
  107. */
  108. private boolean judeSessionUserMall(HttpServletResponse response, ThirdSession thirdSession) throws IOException {
  109. String userId = thirdSession.getUserId();
  110. if(StrUtil.isBlank(userId)){
  111. this.writerPrint(response, "请先登录");
  112. return Boolean.FALSE;
  113. }
  114. return Boolean.TRUE;
  115. }
  116. private void writerPrint(HttpServletResponse response, String msg) throws IOException {
  117. //返回401错误码,触发小程序重新登录
  118. JwtUtil.responseError(response,401,msg);
  119. }
  120. }