正在检查Spring Security MethodInvocation以获取精确的参数信息



Spring Security在这里,并试图找出如何使用MethodInvocation实例来获得:

  1. 传递给方法的所有参数(名称和类型(的列表;以及
  2. 每个参数的相应值

MethodInvocation#getArguments() : Object[]方法,但关于对象数组内可以返回的类型,Spring Security文档绝对没有。

它是一个数组,包含被调用方法的所有参数。最左边的参数从索引0开始,依此类推

假设调用的方法是:

void hello(Integer int , String str, Boolean bool);

并且它被调用:

hello(1000, "world" , true);

然后MethodInvocation#getArguments()将返回一个数组:

  • 在索引0处:整数1000
  • 在索引1处:字符串";世界
  • 在索引2处:布尔值为true

您可以在每个参数对象上使用getClass()来访问它们的类型信息,如果您想访问该类型的特定方法,则可以将其强制转换为实际类。类似的东西:

Object[] args = methodInvocation.getArguments();
args[0].getClass() // return you Integer class
if(args[0] instanceof Integer){
((Integer)arg[0]).intValue(); // cast it to the integer and access a specific method provided by the Integer
}

如果被调用的方法没有任何输入参数,则返回null。

最新更新