首页 > 解决方案 > 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 似乎可以解决这个问题。只是不知道从哪里开始

标签: javaspringspring-security

解决方案


定义可应用于方法和类的注解。如果它被应用到一个类,注解只是级联并应用到类中的所有方法。

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

推荐阅读