users@glassfish.java.net

Getting Principal User an instantiated class

From: <glassfish_at_javadesktop.org>
Date: Wed, 06 Oct 2010 16:05:28 PDT

I have created a web service with Jersey running on glassfish v2. I am trying to get the Principal User that was authenticated with my Custom Login Module. The call comes in to the root resource with context and information and instantiates another class that performs all the work and delivers data back to the root resource.

So in the instantiated class, i am having trouble getting the context of the request with the Principal User name.

A few things that i have tried to do:
1. I tried to set the instantiated class as an @Provider and get the @Context SecurityContext or HttpContext and these were never injected with anything they were always null. So then i thought i should try to set the package in the web.xml like this

<init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.pervasive.datasolutions.cloudreporting.api</param-value>
</init-param>

but that did not work either.

2. Get the Principal name from the Security Manager like this but at first i did not have security manager enabled so i enabled it and then i got an authorization exception trying to get the subject from the context.

final SecurityManager securityManager = System.getSecurityManager();
AccessControlContext context = (AccessControlContext)securityManager.getSecurityContext();
Subject subject = Subject.getSubject(context);
String user = null;
for (Iterator it = subject.getPrincipals().iterator(); it.hasNext();) {
     Principal p = (Principal) it.next();
     user = p.getName();
}


Anyone have any thoughts on getting the Principal user name in a class that seems to not have direct access to the Context?
[Message sent by forum member 'cp16net']

http://forums.java.net/jive/thread.jspa?messageID=484473