PDA

View Full Version : How to get user principal in home class?


nhinhi
07-24-2012, 02:40 PM
Hi all,
I have a class which implements Service
public interface IMyService {
public void getWorkSpace();
}

public class MyServlet implements Service, IMyService {
public void getWorkSpace() {
//here, I need to get the user principal ???
//(something like request.getUserPrincipal())
//java.security.Principal userPrincipal = request.getUserPrincipal();
System.out.println("Current user princial: " + CURRENT_USER_PRINCIPAL.getName());
}
}

In my web.xml:
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>home-api</param-name>
<param-value>IMyService</param-value>
</init-param>
<init-param>
<param-name>home-class</param-name>
<param-value>MyServlet</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/myservlet</url-pattern>
</servlet-mapping>
<security-constraint>
<display-name>My security</display-name>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<url-pattern>/myservlet</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>user</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
<security-role>
<description>
All authenticated role
</description>
<role-name>user</role-name>
</security-role>

At client side:
HessianProxyFactory factory = new HessianProxyFactory();
factory.setUser("myUser");
factory.setPassword("myPassword");
...
IMyService myService = (IMyService)factory.create(IMyService.class, url);
myService.getWorkSpace();
//here, I need to output the line: "Current user principal: myUser" ????

My web application is deployed in websphere server. The WebSphere server will be responsible for authentication the user to myservlet.
My question is in the title, how can I get the authenticated user in MyServlet?
Thank you for your help