如何在Spring表达式语言中进行类型转换

Dan*_*l L 6 spring types expression casting

我正在尝试使用@PreAuthorize注释进行访问控制.我需要在身份验证对象的详细信息中检查一些自定义字段.我有一个CustomWebAuthenticationDetails类,它是WebAuthenticationDetails的子类,包含自定义字段.通常,我会使用以下内容来访问CustomWebAuthenticationDetails中的自定义字段:

((CustomWebAuthenticationDetails)authentication.getDetails()).getCustomField()
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试在@PreAuthorize表达式中使用上述语句(甚至包括CustomWebAuthenticationDetails的完全限定路径)时,我收到以下错误:

java.lang.IllegalArgumentException: Failed to parse expression ...
Run Code Online (Sandbox Code Playgroud)

我该如何处理这里的类型转换呢?

谢谢,

丹尼尔

JB *_*zet 8

AFAIK,鉴于EL的动态和解释性质,您不需要任何演员.如果运行时对象存在该属性,它将找到它,而不关心其声明的类型:

authentication.details.customField
Run Code Online (Sandbox Code Playgroud)