views:

544

answers:

1

How can I get the principal name, session and ideally check if the principal is authenticated with the Spring Security context inside a CXF JAX-RS webservice method receiving a call from an Android client? This is the code I am currently working with. I have commented where and what I am trying to get.

Android code to call webservice:

httpclient.getCredentialsProvider().setCredentials(
          new AuthScope("192.168.1.101", 80), 
          new UsernamePasswordCredentials("joesmith", "mypasswd"));

 HttpGet httpget = new HttpGet(WEBSERVICE_URL+"/makePayload");
  httpget.setHeader("User-Agent", userAgent);
  httpget.setHeader("Content-Type", "application/xml");

  HttpResponse response;

  try {
      response = httpclient.execute(httpget);
      HttpEntity entity = response.getEntity();

      ... parse xml from response


      }

CXF, Spring webservice code:

@GET
@Path("/getPayload")
@Produces("application/XML")
public Response makePayload(@Context Request request){

         //Get user principal name
         //Get session?
         //Get Spring security context?

         Payload payload = new Payload();
         payload.setUsersOnline(new Long(200));

         return Response.ok().entity(payload).build();

 }
A: 

request.getUserPrincipal.getName() or static SecurityContextHolder.getContext().getAuthentication().getName() might work

That is not working for me -- I get the following errors: request.getUserPrincipal() = No such instance method: 'org.apache.cxf.jaxrs.impl.RequestImpl.getUserPrincipal' SecurityContextHolder.getContext().getAuthentication() = nullThe overall scope of what I am trying to do is have a request from an android app for a webservice url be treated the same as if it was a logged in user making a request on the website - that is, have Spring security intercept the request and authenticate the user.