首页 > 编程语言 > 详细

SpringBoot项目中,AOP的使用

时间:2019-01-28 17:06:18      阅读:222      评论:0      收藏:0      [点我收藏+]

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();
        }
    }

 

SpringBoot项目中,AOP的使用

原文:https://www.cnblogs.com/xzmxddx/p/10330825.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!