java - Spring 扩展 Authentication 对象以保持控制器干燥
问题描述
public List<MyObject> find( Authentication auth )
//read details off the authentication object.
OAuth2AuthenticationDetails oauthDetails= (OAuth2AuthenticationDetails) auth.getDetails();
HashMap<String, Object> additionalInformationMap = ( HashMap<String, Object> )oauthDetails.getDecodedDetails();
目前,我的控制器中有一小段代码可以读取存储在 JWT 令牌中的附加信息。
最好我不想在多个控制器方法中编写此代码 - 很可能它会遍布整个代码库。
在 Spring 中是否有更好的方法来执行此操作,而控制器中没有。例如,我可以在过滤器或其他东西中扩展身份验证对象并将额外数据添加到扩展对象上的公共方法中吗?
编辑。通过阅读,AOP 似乎可以解决这个问题。只是不知道从哪里开始
解决方案
定义可应用于方法和类的注解。如果它被应用到一个类,注解只是级联并应用到类中的所有方法。
package com.perfectcomputersolutions.pos.annotation;
import org.springframework.stereotype.Component;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Component
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface NoNullArgs {
}
然后创建一个类,其中包含一个建议(应用方法)到一系列切入点(应用实际建议的位置)。笔记。这是一个在 Groovy 中实现的示例,它仅检查所有参数是否不为空。但是,您可以更改方法的主体来做任何您想做的事情。一旦你有了args
数组,这些就是你可以转换为预期类型的值。
package com.perfectcomputersolutions.pos.aspect
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
import org.aspectj.lang.reflect.CodeSignature
import org.springframework.core.annotation.Order
import org.springframework.stereotype.Component
@Aspect
@Order(0)
@Component
class NoNullArgsAspect {
// Use this if writing in Java.
// omitting the getMetaClass call is only for Groovy
// @Before(
// value = "@within(com.perfectcomputersolutions.pos.annotation.NoNullArgs) || @annotation(com.perfectcomputersolutions.pos.annotation.NoNullArgs)"
// )
@Before(
value = "!execution(* *.getMetaClass(..)) && @within(com.perfectcomputersolutions.pos.annotation.NoNullArgs) || @annotation(com.perfectcomputersolutions.pos.annotation.NoNullArgs)"
)
void requireNotNull(JoinPoint jp) {
def method = (CodeSignature) jp.signature
def types = method.parameterTypes
def names = method.parameterNames
def args = jp.args
for (int i = 0; i < types.length; i++)
Objects.requireNonNull(args[i], "Parameter ${names[i]} must not be null" as String)
}
}
- 见原帖。
- 请参阅Spring AOP 文档
推荐阅读
- android - 为了通过 Firebase 云消息发送通知,捕获设备注册令牌的正确方法是什么?
- wordpress - 如果我在 wordpress 的 UI 中进行更改,如何找到已更改的文件?
- conditional - 在 camunda 上创建返回条件
- soapui - 带有 mtom 附件的 SOAP 请求:没有 id 附件
在[附件]中找到 - r - 将列总计添加到数据框 R 中的新行
- python-3.x - Python 3:使用约束实现自己的类型的最佳方法
- python - 计算用户的帖子数量 - django
- amazon-web-services - AWS CodeDeploy 冻结等待终止实例的 CodePipeline 阶段
- angularjs - createUserWithEmailAndPassword 失败:第一个参数“email”必须是有效字符串
- c# - .ToDictionary C# 中的 foreach 和索引