From 0d8b934f4908dcdf76730a57eb5b40e164810158 Mon Sep 17 00:00:00 2001 From: yangjie <1179743470@qq.com> Date: Wed, 9 Jun 2021 11:37:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9,token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yipin/liuwanr/filter/AuthInterceptor.java | 38 ++++++++----------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/yipin/liuwanr/filter/AuthInterceptor.java b/src/main/java/com/yipin/liuwanr/filter/AuthInterceptor.java index 2c31ddf..5ff829c 100644 --- a/src/main/java/com/yipin/liuwanr/filter/AuthInterceptor.java +++ b/src/main/java/com/yipin/liuwanr/filter/AuthInterceptor.java @@ -1,8 +1,6 @@ package com.yipin.liuwanr.filter; -import com.yipin.liuwanr.entity.UserIdEnum; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.util.StringUtils; @@ -13,7 +11,6 @@ import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Objects; -import java.util.concurrent.TimeUnit; /** * @description @@ -31,28 +28,23 @@ public class AuthInterceptor implements HandlerInterceptor { response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8"); - //userid - String userId = request.getParameter("userId"); - if (UserIdEnum.getUserId(Integer.parseInt(userId))!=0){ - return true; - }else { + String token = request.getHeader("token"); - String token = request.getHeader("token"); - ValueOperations ops = stringRedisTemplate.opsForValue(); - Object loginStatus = ops.get(token); - if (StringUtils.isEmpty(token)) { - response.getWriter().print("0");//用户未登录,请登录后操作! - return false; - } - if( Objects.isNull(loginStatus)){ - response.getWriter().print("1");//token错误 - return false; + if (StringUtils.isEmpty(token)) { + response.getWriter().print("0");//用户未登录,请登录后操作! + return false; + } + ValueOperations ops = stringRedisTemplate.opsForValue(); + Object loginStatus = ops.get(token); + if( Objects.isNull(loginStatus)){ + response.getWriter().print("1");//token错误 + return false; + }else { + if (request.getRequestURI().contains("logins")){ + String userId = request.getParameter("userId"); + return userId.equals(loginStatus); }else { - if (!StringUtils.isEmpty(userId)){ - return userId.equals(loginStatus); - }else { - return true; - } + return true; } } }