|
@@ -0,0 +1,107 @@
|
|
|
|
|
+package com.chuanghai.student_portrait.cron;
|
|
|
|
|
+
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.auth0.jwt.interfaces.Claim;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
|
|
|
+import com.chuanghai.student_portrait.annotation.PassToken;
|
|
|
|
|
+import com.chuanghai.student_portrait.response.BaseResponse;
|
|
|
|
|
+import com.chuanghai.student_portrait.response.enums.StatusEnum;
|
|
|
|
|
+import com.chuanghai.student_portrait.utils.JWTUtils;
|
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
+import org.springframework.web.method.HandlerMethod;
|
|
|
|
|
+import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
|
|
+import org.springframework.web.servlet.ModelAndView;
|
|
|
|
|
+
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
+import java.io.PrintWriter;
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+public class JwtAuthenticationInterceptor implements HandlerInterceptor {
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * preHandle:进入到控制器方法之前执行的内容
|
|
|
|
|
+ * boolean:
|
|
|
|
|
+ * true:可以继续执行控制器方法
|
|
|
|
|
+ * false:拦截
|
|
|
|
|
+ * posthandler:执行控制器方法之后执行的内容
|
|
|
|
|
+ * afterCompletion:响应结束之前执行的内容
|
|
|
|
|
+ *///说明
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
|
|
+
|
|
|
|
|
+ //region 检查是否有passtoken注释,有则跳过认证
|
|
|
|
|
+ //PassToken注解用在方法上面
|
|
|
|
|
+ HandlerMethod handlerMethod = (HandlerMethod) handler;
|
|
|
|
|
+ Method method = handlerMethod.getMethod();
|
|
|
|
|
+ if (method.isAnnotationPresent(PassToken.class)) {
|
|
|
|
|
+ PassToken passToken = method.getAnnotation(PassToken.class);
|
|
|
|
|
+ if (passToken.requeired()) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ //region 获取url传递的参数 Query string传递的参数
|
|
|
|
|
+ //获取url参数 getParameter方法里的参数名和定义的参数名一致
|
|
|
|
|
+ String token = request.getHeader("token");
|
|
|
|
|
+ // 验证userToken
|
|
|
|
|
+ if (!StringUtils.hasText(token)) {
|
|
|
|
|
+// throw new Exception(StatusEnum.SYSTEM_TOKEN_ERROR);
|
|
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
|
|
+ response.setContentType("application/json; charset=utf-8");
|
|
|
|
|
+ JSONObject res = new JSONObject();
|
|
|
|
|
+ res.put("code", StatusEnum.SYSTEM_TOKEN_ERROR.getStatus());
|
|
|
|
|
+ res.put("message", StatusEnum.SYSTEM_TOKEN_ERROR.getDesc());
|
|
|
|
|
+ PrintWriter out = null;
|
|
|
|
|
+ out = response.getWriter();
|
|
|
|
|
+ out.write(res.toString());
|
|
|
|
|
+ out.flush();
|
|
|
|
|
+ out.close();
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 解析token
|
|
|
|
|
+ try {
|
|
|
|
|
+ Map<String, Claim> stringClaimMap = JWTUtils.verifyToken(token);
|
|
|
|
|
+ if (ObjectUtils.isNotEmpty(stringClaimMap)) { // 登录
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }catch (Exception e){
|
|
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
|
|
+ response.setContentType("application/json; charset=utf-8");
|
|
|
|
|
+ JSONObject res = new JSONObject();
|
|
|
|
|
+ res.put("code", StatusEnum.SYSTEM_LOGIN_ERROR.getStatus());
|
|
|
|
|
+ res.put("message", StatusEnum.SYSTEM_LOGIN_ERROR.getDesc());
|
|
|
|
|
+ PrintWriter out = null;
|
|
|
|
|
+ out = response.getWriter();
|
|
|
|
|
+ out.write(res.toString());
|
|
|
|
|
+ out.flush();
|
|
|
|
|
+ out.close();
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
|
|
+ response.setContentType("application/json; charset=utf-8");
|
|
|
|
|
+ JSONObject res = new JSONObject();
|
|
|
|
|
+ res.put("code", StatusEnum.SYSTEM_LOGIN_ERROR.getStatus());
|
|
|
|
|
+ res.put("message", StatusEnum.SYSTEM_LOGIN_ERROR.getDesc());
|
|
|
|
|
+ PrintWriter out = null;
|
|
|
|
|
+ out = response.getWriter();
|
|
|
|
|
+ out.write(res.toString());
|
|
|
|
|
+ out.flush();
|
|
|
|
|
+ out.close();
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+}
|