[feat][M]:新增AOP验证模块

master
Romesum 5 years ago
parent 6bb2dd273e
commit c3662d9e09

@ -0,0 +1,51 @@
package com.hzu.bookingsystem.filter;
import com.hzu.bookingsystem.exception.UserAuthorizeException;
import com.hzu.bookingsystem.utils.CookieUtil;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
/**
* author
*/
@Aspect
@Component
public class UserAuthorizeAspect {
@Autowired
private StringRedisTemplate redisTemplate;
@Pointcut("execution(public * com.hzu.bookingsystem.controller.*.*(..))" +
"&& !execution(public * com.hzu.bookingsystem.controller.UserController.login(..))")
public void verify() {}
@Before("verify()")
public void doVerify() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
//查询cookie
Cookie cookie = CookieUtil.get(request, "token");
if (cookie == null) {
System.out.println("【登录校验】Cookie中查不到token");
throw new UserAuthorizeException("未登录");
}
//去redis里查询
String tokenValue = redisTemplate.opsForValue().get(cookie.getValue());
if (StringUtils.isEmpty(tokenValue)) {
System.out.println("登陆已过期");
throw new UserAuthorizeException();
}
}
}
Loading…
Cancel
Save