java 如何获取 SOAP 请求客户端计算机的源 IP?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2647902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the Source IP of a SOAP requesting client machine?
提问by user450937
how do you get source ip, username, password, etc... of the client machine that sends a soap request? is there any of these details that one can pull for logging purposes?
你如何获得发送soap请求的客户端机器的源IP、用户名、密码等?是否有任何这些细节可以用于记录目的?
I am using Java to handle the incoming SOAP requests. The service simply adds 2 numbers and is working, but I just need to get some client details.
我正在使用 Java 来处理传入的 SOAP 请求。该服务只是添加了 2 个数字并且正在运行,但我只需要获取一些客户端详细信息。
Thanks, Lavanya
谢谢,拉瓦尼亚
回答by Catchwa
If you're using JAX-WS, inject a WebServiceContext like so:
如果您使用的是 JAX-WS,请像这样注入一个 WebServiceContext:
import javax.annotation.Resource
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
@WebService()
public class Test
{
@Resource WebServiceContext context;
@WebMethod(operationName = "getInfo")
public String getInfo()
{
HttpServletRequest request = (HttpServletRequest)context.getMessageContext().get(MessageContext.SERVLET_REQUEST);
return "IP: "+request.getRemoteAddr()+", Port: "+request.getRemotePort()+", Host: "+request.getRemoteHost();
}
}
Will return something like:
会返回类似的东西:
IP: 127.0.0.1, Port: 2636, Host: localhost
IP: 127.0.0.1, Port: 2636, Host: localhost
Look at the APIfor the rest of the methods. Basically, once you have your HttpServletRequestobject, the rest is pretty easy.
查看API以了解其余方法。基本上,一旦你有了HttpServletRequest对象,剩下的就很简单了。
回答by Fazal
I am not surte I fully understand your idea of getting username and password of client machine. In general with Soap look at Soap Header, they are supposed to carry the authentication information (which could be username, password or some kind of security toke). For the IP, your Soap is coming over Http and therefore when you receive your request you can try and look at your Http headers to see what information it gives you. Though I have never tried to get the IP of the client from it, but it might be there in the HTTP header
我不知道我完全理解您获取客户端机器的用户名和密码的想法。一般来说,Soap 查看 Soap Header,它们应该携带身份验证信息(可能是用户名、密码或某种安全令牌)。对于 IP,您的 Soap 是通过 Http 传输的,因此当您收到请求时,您可以尝试查看 Http 标头以查看它为您提供的信息。虽然我从未尝试从中获取客户端的 IP,但它可能存在于 HTTP 标头中
回答by anonymous
What soap stack are u using. If u deployed it as a war file using axis it is pretty easy to do it. u need to get hold of the httprequestobject and call the HTTPServletRequest.getRemoteAddr() method on it.
你在用什么肥皂栈。如果您使用轴将其部署为War文件,则很容易做到。您需要获取 httprequestobject 并在其上调用 HTTPServletRequest.getRemoteAddr() 方法。
回答by Saikat1529
I have figured the solution like below -
我想出了如下解决方案 -
@Endpoint
public class DataEndpoints {
....
....
private HttpServletRequest httpServletRequest;
@Autowired
public void setRequest(HttpServletRequest request) {
this.httpServletRequest = request;
}
@PayloadRoot(namespace = employeeNS, localPart = "syncRelation")
@ResponsePayload
public SyncRelationResponse dataSync(@RequestPayload SyncOrderRelation request) {
String incoming = "IP Address -> " + this.httpServletRequest.getRemoteAddr();
}
}
By using the following method, I can directly access HttpServletRequest. And then I can access all data i need.
通过使用下面的方法,我可以直接访问HttpServletRequest。然后我可以访问我需要的所有数据。
I hope it will help someone in this context.
我希望它会在这种情况下帮助某人。

