java - Spring 扩展 Authentication 对象以保持 Controller 干燥

标签 java spring spring-security

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

目前我的 Controller 中有一小段代码可以读取存储在 JWT token 中的附加信息。

我最好不要在多个 Controller 方法中编写这段代码——它很可能会贯穿整个代码库。

在 Spring 中是否有更好的方法来执行此操作而不是在 Controller 中。例如我可以在过滤器或其他东西中扩展 Authentication 对象,并将额外的数据添加到扩展对象的公共(public)方法中吗?

编辑。从周围阅读,似乎 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 数组,这些就是您可以转换为预期类型的​​值。 p>

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)
    }
}

关于java - Spring 扩展 Authentication 对象以保持 Controller 干燥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54312555/

相关文章:

java - APNS - 无法使用 Java 向 iOS 设备发送通知

java - 处理 Spring/Tomcat 初始化失败

spring - 正在将事件配置文件设置为 "native",这在 spring boot 2 配置服务器中是必需的

java - Spring Hibernate - CrudRepository 和 SessionFactory 之间的区别

spring - Grails Spring Security @Secured 不工作

java - hibernate 中字符串字段与字符串数组的交集

java - 带有 JDBC 准备语句的字符串中的圆括号

java - Spring MVC 的 C# Session_OnStart 模拟

java - isAnonymous() 和 isAuthenticated() 在错误页面上都返回 false

java - 如何获取另一个类的元素之间的元素计数?