java注解修改值

发布于 2025-01-15 01:36:44 字数 504 浏览 3 评论 0原文

我有一个带有 spring 的 Web 应用程序,一个休息端点接收一个像这样加密的值:

@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 的示例,

I have a web application with spring, one rest endpoint receives a value encrypted like this:

@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public resp welcome(
    @RequestHeader(name = "user") String user, 
    @Encripted @RequestHeader(name = "password") String password 

I'm creating the annotation @Encripted like this:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Encripted {
}

now I need to manipulate the String that got annotated and change its value, how could I override or implement to achieve this goal? I can't find example with ElementType.PARAMETER,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云胡 2025-01-22 01:36:44

假设您在 com.example.controller 中创建了 WelcomeControllerEncrypted 类,

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 "";
}

为了处理注释,您需要创建一个方面像这样的类 --

@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

Lets say you created WelcomeController and Encrypted classes in com.example.controller like so,

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 "";
}

In order to process the Annotation you need to create an Aspect class like so --

@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();
    }
}

Check more on the AOP with Spring here -- https://www.baeldung.com/spring-aop

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文