0
這是一個spring啓動應用程序,我用aop來捕捉http頭部,body.I可以爲@annotation做一些事情,當捕獲http頭部,body時。但是,當我轉移數據到我的自定義參數,我不能get.but另一個春季啓動應用程序可以轉讓它。我不知道爲什麼。spring aop不能將值傳遞給自定義參數
這是定製@Aspect:
@Component
@Aspect
public class ApiAspect {
@Resource
private HttpServletRequest request;
@Around("@annotation(com.jvbaoji.api.common.ApiAuth)")
public Object aroundAdvice(ProceedingJoinPoint point) throws Throwable {
Long userId = Optional.ofNullable(request.getHeader("user_id")).map(Long::valueOf).orElse(null);
final MethodSignature signature = (MethodSignature) point.getSignature();
final Method method = signature.getMethod();
final String methodName = method.getName();
final Class<?>[] parameterTypes = method.getParameterTypes();
final Annotation[][] parameterAnnotations;
parameterAnnotations = point.getTarget().getClass().getMethod(methodName, parameterTypes).getParameterAnnotations();
final Object[] args = point.getArgs();
for (int i = 0; i < args.length; i++) {
for (Annotation annotation : parameterAnnotations[i]) {
if (AuthUserId.class.equals(annotation.annotationType())) {
args[i] = userId;
break;
}
}
}
return point.proceed();
}
}
定製@interface:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiAuth {
}
參數:
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthUserId {
}
控制器:
@RequestMapping(value = "updateAccount", method = RequestMethod.POST)
@ApiAuth
public JsonNode updateAccount(@AuthUserId Long userId, @ModelAttribute UserAccountVO userAccountVO) {
userAccountVO.setUserId(userId);
TransmissionResult result = userWalletService.updateAccount(userAccountVO);
if (!result.isSuccess()) {
return responseJsonFactory.createBad(result.getCode(), result.getFailReason());
}
return responseJsonFactory.createOk(jsonMapper.convertValue(userAccountVO, JsonNode.class));
}
我發現當我發佈http時,我可以調試到arg[i]=userId
,但要返回point.proceed()
,它會拋出一個NullException.My @AuthUserId Long userId
無法獲得返回值。我不知道爲什麼。
ps:我春天的aop版本是spring-aop-4.1.4.RELEASE.jar在spring boot。
謝謝你的建議如果你給。
只是以纔回答正確理解你:a)您知道的事實方面是獨立的,並因此注入的請求將永遠是相同的? b)從我所看到的,我的印象是該方面的目的是破解用戶授權。如果是這樣,我希望你有合法理由這樣做。你能簡單地評論一下嗎? – kriegaex
NoNoNo.It是我的應用程序,我不知道如何破解用戶授權。我只是在我的應用程序中爲我的項目獲取了userId,但我仍然沒有得到。 – Hanawa