zhangyayun0 2019-09-08
此文只说明在Spring Boot中如何使用AOP切面,让自己专注业务逻辑代码
@Aspect @Component @Slf4j public class SellerAuthorizeAspect { @Autowired private StringRedisTemplate redisTemplate; @Pointcut("execution(public * com.example.demo.controller.Seller*.*(..))"+ "&& !execution(public * com.example.demo.controller.SellerUserController.*(..))") public void verify(){} @Before("verify()") public void doverify(){ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN); if(cookie == null){ log.warn("【登录校验】Cookie中查不到token"); throw new SellerAuthorizeException(); } String tokenValue = redisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue())); if (StringUtils.isEmpty(tokenValue)){ log.warn("【登录校验】Redis中查不到token"); throw new SellerAuthorizeException(); } } }
在注解@Before 编辑自己的逻辑函数即可
以后会好好看看Spring AOP切面的底层原理,文章如果有错误欢迎指出,一起成长