When running with a SecurityManager enabled, the calling code must be granted permission to call the PolicyContext.getContext and the Policy.getPolicy methods. This can be facilitated by encapsulating these protected methods in a doPrivileged, and by loading this utility code within a CodeBase for which the necessary permissions can be conveniently granted. For Glassfish, this can be done by packaging the utility in its own jar and placing the
jar in glassfish/lib.
private static final String SUBJECT_HANDLER_KEY =
"javax.security.auth.Subject.container";
Subject s = (Subject) PolicyContext.getContext(SUBJECT_HANDLER_KEY);
CodeSource cs = new CodeSource(null, (java.security.cert.Certificate[]) null);
Principal principals[] = (s == null ? new Principal[0] : s.getPrincipals().toArray(new Principal[0]));
ProtectionDomain pd = new ProtectionDomain(cs, null, null, principals);
Policy policy = Policy.getPolicy();
PermissionCollection pc = policy.getPermissions(pd);
pc.implies(new WebRoleRefPermission(null, null));
HashSet
Note that the returned values will correspond to role reference values; which are values that may be used by the
application to test role membership. These values may differ from the names of application roles if the application has defined security-role-ref elements. If no security-role-ref elements have been defined, the role reference values will correspond exactly to the names of the application roles.
18 Aug · Mon 2008
Using JACC to determine a caller's roles
The following technique is supported by all JACC compatible containers given that they are configured
with a typical Policy provider capable of returning a PermissionCollection encapsulating all the
WebRoleRefPermission and/or EJBRoleRefPermission objects pertaining to a ProtectionDomain.
Thanks. Just what I needed.
Posted by Remy Monsen on February 16, 2009 at 06:43 PM EST #