これは春の起動アプリケーションです。私はhttp head、bodyを捕捉するためにaopを使いました。http head、bodyの値をキャッチするときには@annotationのために何かできます。私のカスタマイズしたパラメータにデータ、私は得ることはできません。しかし、別の春のブートアプリケーションは、それを転送することができます。私はなぜか分からない。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がスローされます。@AuthUserId Long userId
は返されません。理由はわかりません。
ps:私のspring aopバージョンは、spring-aop-4.1.4.RELEASE.jarです。
お礼ありがとうございます。
へ
応答する前に正しくあなたを理解するだけでよう:ApiAspect.classで
A)あなたは様相がシングルトンであるという事実を認識しているので、注入されたリクエストは常に同じでしょうか? b)私が見ていることから、私の印象は、その側面の目的はユーザーの承認をハックすることです。もしそうなら、法的理由があることを願っています。簡単にコメントしていただけますか? – kriegaex
NoNoNo.Itは私のアプリケーションです。私はユーザー権限をハッキングする方法を知らないのです。私は自分のプロジェクトのアプリケーションでuserIdを取得します。私はまだ何も取得しません。 – Hanawa