SpringBoot項目中,AOP的使用


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM