From 2f640b1038fa331954f78ed1f4317212cf5bb34d Mon Sep 17 00:00:00 2001 From: chenlu <1320612696@qq.com> Date: 星期五, 07 六月 2024 16:56:06 +0800 Subject: [PATCH] Merge branch 'master' of http://10.153.19.25:10101/r/ERP_override --- north-glass-erp/src/main/java/com/example/erp/common/interceptor/JwtInterceptor.java | 65 ++++++++++++++++++++++++++++++++ 1 files changed, 65 insertions(+), 0 deletions(-) diff --git a/north-glass-erp/src/main/java/com/example/erp/common/interceptor/JwtInterceptor.java b/north-glass-erp/src/main/java/com/example/erp/common/interceptor/JwtInterceptor.java new file mode 100644 index 0000000..60ed717 --- /dev/null +++ b/north-glass-erp/src/main/java/com/example/erp/common/interceptor/JwtInterceptor.java @@ -0,0 +1,65 @@ +package com.example.erp.common.interceptor; + +import cn.hutool.core.util.StrUtil; +import com.auth0.jwt.JWT; +import com.auth0.jwt.JWTVerifier; +import com.auth0.jwt.algorithms.Algorithm; +import com.auth0.jwt.exceptions.JWTVerificationException; +import com.example.erp.common.CacheUtil; +import com.example.erp.common.Constants; +import com.example.erp.controller.dto.UserDTO; +import com.example.erp.exception.ServiceException; +import com.example.erp.service.userInfo.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.method.HandlerMethod; +import org.springframework.web.servlet.HandlerInterceptor; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@Component +public class JwtInterceptor implements HandlerInterceptor { + + @Autowired + private UserService userService; + @Autowired + private CacheUtil cacheUtil; + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + //return HandlerInterceptor.super.preHandle(request, response, handler); + /*String token=request.getHeader("token"); + if(!(handler instanceof HandlerMethod)){ + return true; + } + + if(StrUtil.isBlank(token)){ + throw new ServiceException(Constants.Code_401,"鏃爐oken,閲嶆柊鐧婚檰"); + } + String userId; + try{ + userId = JWT.decode(token).getAudience().get(0); + }catch (Exception e){ + throw new ServiceException(Constants.Code_500,"token鏍煎紡閿欒"); + } + + UserDTO getUserDTO = cacheUtil.getCacheData(userId); + + if(getUserDTO != null && !getUserDTO.getToken().equals(token)){ + throw new ServiceException(Constants.Code_600,"鐢ㄦ埛鍦ㄥ叾浠栦綅缃櫥闄�"); + } + + + String password=userService.getUserByID(userId).toLowerCase(); + //鐢ㄦ埛瀵嗙爜鍔犵楠岃瘉 + JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(password)).build(); + try { + jwtVerifier.verify(token); + } catch (JWTVerificationException e) { + throw new ServiceException(Constants.Code_401,"token楠岃瘉澶辫触,璇烽噸鏂扮櫥闄�"); + }*/ + return true; + } + +} -- Gitblit v1.8.0