Springboot中自帶依賴
1.創建一個SellerAuthorizeAspect類,打上標簽@Aspect和@Component
@Aspect @Component @Slf4j public class SellerAuthorizeAspect { }
2.設置切點,這個注解的意思是攔截所有controller中Seller*開頭的類的方法但是不攔截SellerUserController中的方法
@Pointcut("execution(public * com.xiong.sell.controller.Seller*.*(..))" +
" && !execution(public * com.xiong.sell.controller.SellerUserController.*(..))")
public void verify() {
}
3.設置攔截后的操作,這里可以直接處理,也可以拋出一個異常來,然后攔截處理
@Before("verify()")
public void doVerify() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
//查詢cookie
Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
if (cookie == null) {
log.warn("【登錄校驗】 cookie中沒有token");
throw new SellerAuthorizeException();
}
//查詢redis
String tokenValue = redisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue()));
if(StringUtils.isEmpty(tokenValue)){
log.warn("【登錄校驗】 redis中沒有token");
throw new SellerAuthorizeException();
}
}
