dev@jersey.java.net

Re: [Jersey] How to retrieve user info from your WS

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 18 Nov 2008 10:49:23 +0100

On Nov 17, 2008, at 9:37 PM, Jerry Atrick wrote:

> So normally in a servlet, I'd do a simple:
>
> HttpServletRequest request;
>
> request.getRemoteAddr();
>
> to retrieve the user currently hitting my WS.
>
>
> How would I do the same from a jersey webservice?
>
> I originally attempted to do something like:
>
> @GET
> @ProduceMime("text/xml" )
> @Path("/ftpRestart" )
> public String echoCallingIP(@Context HttpRequestContext
> context ) {
>
> context.getRequest();
>
> }
>
> But the HttpRequestContext reveals nothing useful at all here.
>
> I'm sure there's a way to do this, but perhaps I'm just going about
> it the wrong way. Anyone have any clues on how to do this (I swear
> this should be a lot simpler than I'm making it out to be).
>

Do the following:

     @GET
     @ProduceMime("text/xml" )
     @Path("/ftpRestart" )
     public String echoCallingIP(@Context HttpServletRequest request ) {
         ...
     }

There is currently no way to get access to the remote address using
the Jersey request abstraction, which is probably a mistake.

If you want to obtain the authenticated user then you can do:

     @GET
     @ProduceMime("text/xml" )
     @Path("/ftpRestart" )
     public String echoCallingIP(@Context
javax.ws.rs.core.SecurityContext sc) {
         Principal p = sc.getUserPrincipal();
     }

Paul.