Is there any way to get a String[] with the roles a user has in the JSP or Servlet?
I know about request.isUserInRole("role1") but I also want to know all the roles of the user.
I searched the servlet source and it seems this is not possible, but this seems odd to me.
So... any ideas?
On JACC-compliant application servers -- in theory every Full Java EE Platform implementation -- the Java SE
Policy
can be interrogated for (almost) portable evaluation of any type of declarative security constraint specified by Servlet and EJB. I say almost because neither JACC nor the Javadoc spec ofPolicy#getPermissions(ProtectionDomain)
actually requires that the implementation compute all permissions on the fly, presumably due to performance considerations, as well as to accommodate for providers whose rendering of authorization statements depends on additional context (remote address, value of a certain HTTP GET parameter, etc.). Nonetheless,getPermissions
should normally be safe to use with the typical pre-installed JACC provider.The following example demonstrates Servlet role assignment testing:
References:
In WebLogic you can do it with:
Note that the first element on the list is the user name.
The answer is messy.
First you need to find out what type request.getUserPrincipal() returns in your webapp.
Let's say that returns org.apache.catalina.realm.GenericPrincipal.
Then cast the result of getUserPrincipal() to that type and use the methods it provides.
I said it was going to be messy. It's not very portable either.
Read in all the possible roles, or hardcode a list. Then iterate over it running the isUserInRole and build a list of roles the user is in and then convert the list to an array.