@within和@annotation的區別:
1. @within 對象級別
2. @annotation 方法級別
例如:
@Slf4j
@Aspect
@RequiredArgsConstructor
public class SecurityInnerAspect implements Ordered {
private final HttpServletRequest request;
@SneakyThrows
@Around("@within(inner) || @annotation(inner)")
public Object around(ProceedingJoinPoint point, Inner inner) {
// 實際注入的inner實體由表達式后一個注解決定,即是方法上的@Inner注解實體,若方法上無@Inner注解,則獲取類上的
if (inner == null) {
Class<?> clazz = point.getTarget().getClass();
inner = AnnotationUtils.findAnnotation(clazz, Inner.class);
}
String header = request.getHeader(SecurityConstants.FROM);
if (inner.value() && !StrUtil.equals(SecurityConstants.FROM_IN, header)) {
log.warn("訪問接口 {} 沒有權限", point.getSignature().getName());
throw new AccessDeniedException("Access is denied");
}
return point.proceed();
}
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE + 1;
}
}
//這個用於攔截標注在類上面的@RestController注解
@Around("@within(org.springframework.web.bind.annotation.RestController")
// 這個用於攔截標注在方法上面的@RestController注解
@Around("@annotation(org.springframework.web.bind.annotation.RestController")