【发布时间】:2019-12-06 12:22:40
【问题描述】:
我想更改使用我的自定义注释注释的方法的主体。
-
我创建了我的自定义注释:
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface RunAsUser { } -
我已经编写了特殊的处理程序:
@Aspect @Component public class RunAsUserAnnotationHandler { @Before("@annotation(RunAsUser)") public void configureAuthentication() { AuthenticationUtil.configureAuthentication("ROLE_USER"); } @After("@annotation(RunAsUser)") public void clearAuthentication() { AuthenticationUtil.clearAuthentication(); } }其中
configureAuthentication(...)和clearAuthentication()是将另一个硬编码的主体值设置为SecurityContext并将一个返回到默认值的方法。
如果我想在注释中将主体值设置为字符串,我该如何修改我的代码?
我想看到这样的东西:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RunAsUser {
String userName();
}
并使用RunAsUser("stackoverflowUser") 编写我的代码,但我现在知道如何将此值从注释传输到我的configureAuthentication 方法。
有什么建议吗?
【问题讨论】:
标签: java spring annotations aop