From 871ba27ca97380a0f693efa6bd5b553fd1991d87 Mon Sep 17 00:00:00 2001 From: ly19960718 <1622779752@qq.com> Date: Thu, 28 Jan 2021 17:31:15 +0800 Subject: [PATCH] 1 --- .../tenlion/config/CheckAuthInterceptor.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main/java/cn/com/tenlion/config/CheckAuthInterceptor.java b/src/main/java/cn/com/tenlion/config/CheckAuthInterceptor.java index ecc18d2..00bc075 100644 --- a/src/main/java/cn/com/tenlion/config/CheckAuthInterceptor.java +++ b/src/main/java/cn/com/tenlion/config/CheckAuthInterceptor.java @@ -6,6 +6,7 @@ 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.alibaba.fastjson.JSONObject; import com.github.pagehelper.util.StringUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -15,6 +16,7 @@ import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.io.PrintWriter; /** * TODO @@ -23,7 +25,7 @@ import java.io.IOException; * @date 2021/1/27 12:20 */ @Component -public class CheckAuth implements HandlerInterceptor { +public class CheckAuthInterceptor implements HandlerInterceptor { @Autowired private ICardUserService cardUserService; @@ -38,19 +40,26 @@ public class CheckAuth implements HandlerInterceptor { 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不合法"); + response.setStatus(response.SC_UNAUTHORIZED); + response.setCharacterEncoding("UTF-8"); + response.setContentType("application/json; charset=utf-8"); + JSONObject res = new JSONObject(); + res.put("status","401"); + res.put("msg","token不合法"); + PrintWriter out = null ; + out = response.getWriter(); + out.write(res.toString()); + out.flush(); + out.close(); return false; } CardUserDTO tokenManage = AccessTokenManager.getInstance().getKey(token); if(tokenManage != null) { AccessTokenManager.getInstance().updateLastActivityTime(token); + response.setStatus(response.SC_OK); return true; }else{ String aesDecoderToken = AesUtil.aesCommonDecoder(token,token); @@ -58,9 +67,10 @@ public class CheckAuth implements HandlerInterceptor { if(cardUserDTO != null){ AccessTokenManager.getInstance().setKey(token,cardUserDTO); AccessTokenManager.getInstance().updateLastActivityTime(token); + response.setStatus(response.SC_OK); + return true; } } - response.setStatus(response.SC_OK); } catch (Exception e) { e.printStackTrace(); }