18 changed files with 146 additions and 144 deletions
@ -1,54 +1,54 @@ |
|||
package com.bnyer.img.handler; |
|||
|
|||
import com.bnyer.common.core.annotation.TokenCheck; |
|||
import com.bnyer.common.core.exception.ServiceException; |
|||
import com.bnyer.common.redis.service.RedisService; |
|||
import com.bnyer.img.constants.RedisKeyConstant; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
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; |
|||
|
|||
/** |
|||
* @author chengkun |
|||
* @date 2022/4/22 15:04 |
|||
*/ |
|||
@Component |
|||
public class TokenCheckHandler implements HandlerInterceptor { |
|||
|
|||
String tokenKey = RedisKeyConstant.TIKTOK_USER_LOGIN_KEY; |
|||
|
|||
@Autowired |
|||
private RedisService redisService; |
|||
|
|||
@Override |
|||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { |
|||
|
|||
// 判断访问的方法上是否有注解
|
|||
HandlerMethod handlerMethod = (HandlerMethod) handler; |
|||
boolean tokenCheck = handlerMethod.hasMethodAnnotation(TokenCheck.class); |
|||
|
|||
// 尝试从请求头从获取token值
|
|||
String token = request.getHeader("token"); |
|||
if(StringUtils.isEmpty(token)){ |
|||
if(tokenCheck){ |
|||
throw new ServiceException("token为空,无权访问!"); |
|||
} |
|||
} |
|||
String[] split = token.split("#"); |
|||
//从token中获取openId
|
|||
String openId = split[1]; |
|||
//组装redisKey
|
|||
String redisKey = tokenKey+openId; |
|||
//判断redis中是否存在该key,存在则放行,不存在则过期
|
|||
if(redisService.hasKey(redisKey)){ |
|||
return true; |
|||
}else{ |
|||
throw new ServiceException("token失效,请重新授权!"); |
|||
} |
|||
} |
|||
} |
|||
//package com.bnyer.img.handler;
|
|||
//
|
|||
//import com.bnyer.common.core.annotation.TokenCheck;
|
|||
//import com.bnyer.common.core.exception.ServiceException;
|
|||
//import com.bnyer.common.redis.service.RedisService;
|
|||
//import com.bnyer.img.constants.RedisKeyConstant;
|
|||
//import org.apache.commons.lang3.StringUtils;
|
|||
//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;
|
|||
//
|
|||
///**
|
|||
// * @author chengkun
|
|||
// * @date 2022/4/22 15:04
|
|||
// */
|
|||
//@Component
|
|||
//public class TokenCheckHandler implements HandlerInterceptor {
|
|||
//
|
|||
// String tokenKey = RedisKeyConstant.TIKTOK_USER_LOGIN_KEY;
|
|||
//
|
|||
// @Autowired
|
|||
// private RedisService redisService;
|
|||
//
|
|||
// @Override
|
|||
// public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|||
//
|
|||
// // 判断访问的方法上是否有注解
|
|||
// HandlerMethod handlerMethod = (HandlerMethod) handler;
|
|||
// boolean tokenCheck = handlerMethod.hasMethodAnnotation(TokenCheck.class);
|
|||
//
|
|||
// // 尝试从请求头从获取token值
|
|||
// String token = request.getHeader("token");
|
|||
// if(StringUtils.isEmpty(token)){
|
|||
// if(tokenCheck){
|
|||
// throw new ServiceException("token为空,无权访问!");
|
|||
// }
|
|||
// }
|
|||
// String[] split = token.split("#");
|
|||
// //从token中获取openId
|
|||
// String openId = split[1];
|
|||
// //组装redisKey
|
|||
// String redisKey = tokenKey+openId;
|
|||
// //判断redis中是否存在该key,存在则放行,不存在则过期
|
|||
// if(redisService.hasKey(redisKey)){
|
|||
// return true;
|
|||
// }else{
|
|||
// throw new ServiceException("token失效,请重新授权!");
|
|||
// }
|
|||
// }
|
|||
//}
|
|||
|
|||
Loading…
Reference in new issue