package cn.com.tenlion.config; import cn.com.tenlion.accesstokenmanager.AccessTokenManager; import cn.com.tenlion.pojo.dtos.carduser.CardUserDTO; import cn.com.tenlion.pojo.vos.carduser.CardUserVO; import cn.com.tenlion.service.carduser.ICardUserService; import cn.com.tenlion.util.AesUtil; import com.github.pagehelper.util.StringUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; 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.IOException; /** * TODO * @version 1.0 * @author LY * @date 2021/1/27 12:20 */ @Component public class CheckAuth implements HandlerInterceptor { @Autowired private ICardUserService cardUserService; /** * 在请求处理之前进行调用(Controller方法调用之前) */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { try { String path = request.getServletPath(); if(path.contains("release")){ return true; } String token = request.getHeader("token"); if(token == null || StringUtil.isEmpty(token)){ // response.setStatus(response.SC_METHOD_NOT_ALLOWED); // response.addHeader("msg","token不合法"); response.sendError(response.SC_METHOD_NOT_ALLOWED,"token不合法"); return false; } CardUserDTO tokenManage = AccessTokenManager.getInstance().getKey(token); if(tokenManage != null) { AccessTokenManager.getInstance().updateLastActivityTime(token); return true; }else{ String aesDecoderToken = AesUtil.aesCommonDecoder(token,token); CardUserDTO cardUserDTO = cardUserService.getCardUserById(aesDecoderToken); if(cardUserDTO != null){ AccessTokenManager.getInstance().setKey(token,cardUserDTO); AccessTokenManager.getInstance().updateLastActivityTime(token); } } response.setStatus(response.SC_OK); } catch (Exception e) { e.printStackTrace(); } return false; } /** * 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后) */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) { System.out.println("执行了TestInterceptor的postHandle方法"); } /** * 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作) */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { System.out.println("执行了TestInterceptor的afterCompletion方法"); } }