如何从 Android 应用程序调用的 CXF JAX-RS Web 服务方法中的 HTTPRequest 获取主体名称

发布于 2024-08-21 13:33:35 字数 1113 浏览 2 评论 0原文

如何获取主体名称、会话,并最好检查主体是否通过接收 Android 客户端调用的 CXF JAX-RS Web 服务方法内的 Spring Security 上下文进行身份验证?这是我目前正在使用的代码。我已经评论了我想要得到的地方和内容。

Android调用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代码:

@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();

 }

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();

 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹过旳痕迹 2024-08-28 13:33:35

request.getUserPrincipal.getName() 或静态 SecurityContextHolder.getContext().getAuthentication().getName() 可能有效

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文