Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do the type casting in Spring expression language

I'm trying to use the @PreAuthorize annotation for access control. I need to check some custom fields in the details of the authentication object. I have a CustomWebAuthenticationDetails class, which is a subclass of WebAuthenticationDetails and contains custom fields. Normally, I would use the following to get to my custom fields in CustomWebAuthenticationDetails:

((CustomWebAuthenticationDetails)authentication.getDetails()).getCustomField()

However, when I try to use the above statement (even including the fully qualified path to CustomWebAuthenticationDetails) in the @PreAuthorize expression, I get the following error:

java.lang.IllegalArgumentException: Failed to parse expression ...

How am I supposed to do with the type casting here?

Thanks,

Daniel

like image 339
Daniel L Avatar asked Jun 09 '12 02:06

Daniel L


1 Answers

AFAIK, given the dynamic and interpreted nature of EL, you don't need any cast. If the property exists for the runtime object, it will find it, without caring about its declared type:

authentication.details.customField
like image 194
JB Nizet Avatar answered Nov 03 '22 00:11

JB Nizet