创建注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthChecker {
}
定义 Pointcut
@Pointcut("@annotation(com.xxx.annotation.AuthChecker)")
public void pointcut() {
}
定义 advise
@Around("pointcut()")
public Object checkAuth(ProceedingJoinPoint joinPoint) throws Throwable {
//获取传入目标方法的参数
Object[] args = joinPoint.getArgs();
//获取代理方法的参数
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
//获取参数名
String[] argsName = methodSignature.getParameterNames();
if (null != argsName) {
if ("user".equalsIgnoreCase(argsName[0])) {
User dto = (User) args[0];
dto.setWorkId("changeWorkId");
args[0] = dto;
return joinPoint.proceed(args);
}
}
return joinPoint.proceed();
}
总结
uu们有更好的方法可以在评论区指出~
本文介绍了如何通过创建自定义注解、定义Pointcut和编写Advise来实现在运行时动态修改方法参数。具体示例中展示了针对参数名为'user'的UserDTO对象,设置其workId为'changeWorkId'。这种方法对于权限校验或其他参数处理场景具有一定的应用价值。

1万+

被折叠的 条评论
为什么被折叠?



