springboot AOP 註解用法

首先創建一個註解方式的AOP

@Target( ElementType.METHOD )
@Retention( RetentionPolicy.RUNTIME )
@Documented
public @interface ShopRootAOP {
}

攔截處理代碼

@Slf4j
@Aspect
@Component
public class ManagerAOP {

    @Autowired
    SecurityUserHolder securityUserHolder;

    @Autowired
    private MerchantManagerService merchantManagerService;

    /**
     * 系統權限判斷
     * @param joinPoint
     * @param rootAOP
     * @throws MerchantException
     */
    

    /**
     * 門店權限判斷
     * @param joinPoint
     * @param shopRootAOP
     * @throws MerchantException
     */
    @Before( value = "@annotation(shopRootAOP)")
    public void shopRootAOP(JoinPoint joinPoint, ShopRootAOP shopRootAOP) throws MerchantException {
        log.info("shopRootAOP 開始權限校驗");
        
        boolean result = merchantManagerService.judgeManagerPower(managerPowerJudgeDTO);
        if (!result) {
            log.error("shopRootAOP 權限不足");
            throw new MerchantException(ResponseStatus.MERCHANT_MANAGER_POWER_ERROR);
        }
    }
}

代碼註解攔截

    @ShopRootAOP
    @PostMapping("/get")
    public ResponseEntity get(@Valid @RequestBody MerchantGetDTO merchantGetDTO) {
        log.info("get start");
        RequestUtil.copyRequestDTO(merchantGetDTO);
        MerchantVO merchant = merchantInfoService.getMerchant(merchantGetDTO.getMerchantId());
        return ResponseResultUtil.success(merchant);
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章