[英]custom annotation based upon logic and modify the variable value in java spring
[英]java annotation to modify value
我有一个带有 spring 的 web 应用程序,一个 rest 端点接收一个这样加密的值:
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public resp welcome(
@RequestHeader(name = "user") String user,
@Encripted @RequestHeader(name = "password") String password
我正在像这样创建注释 @Encripted:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Encripted {
}
现在我需要操作带注释的字符串并更改其值,我该如何重写或实现以实现此目标? 我找不到 ElementType.PARAMETER 的例子,
假设您像这样在com.example.controller
中创建了WelcomeController
和Encrypted
类,
package com.example.controller;
public class WelcomeController {
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public void welcome(
@RequestHeader(name = "user") String user,
@Encrypted @RequestHeader(name = "password") String password
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Encrypted {
String value() default "";
}
为了处理注释,您需要像这样创建一个方面 class --
@Aspect
public class MyAspect {
@Pointcut("execution(@com.example.controller.WelcomeController * *(@com.example.controller.Encrypted (*), ..)) && args(password)")
public void encryptedMethod(String password) {}
@Around("encryptedMethod(password)")
public Object encryptedMethod(ProceedingJoinPoint pjp, String password) throws Throwable {
// process the password string ...
return pjp.proceed();
}
}
在此处使用 Spring 查看有关 AOP 的更多信息 -- https://www.baeldung.com/spring-aop
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.