检查spring安全方法调用以获得精确的参数信息

58wvjzkj  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(259)

这里是spring security,正在尝试了解如何使用 MethodInvocation 要获取的示例:
传递给方法的所有参数(名称和类型)的列表;和
每个参数对应的值
有一个 MethodInvocation#getArguments() : Object[] 方法,但对于对象数组中可以返回的类型,spring安全文档绝对没有。

mnowg1ta

mnowg1ta1#

它是一个数组,包含被调用方法的所有参数。最左边的参数从索引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。

相关问题