首先創建一個註解方式的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);
}