如何从 Android 应用程序调用的 CXF JAX-RS Web 服务方法中的 HTTPRequest 获取主体名称
如何获取主体名称、会话,并最好检查主体是否通过接收 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
request.getUserPrincipal.getName()
或静态SecurityContextHolder.getContext().getAuthentication().getName()
可能有效request.getUserPrincipal.getName()
or staticSecurityContextHolder.getContext().getAuthentication().getName()
might work