1、问题背景
WS服务器要限制客户端IP(项目场景中IP无法伪装),及记录调用日志。
2、实现
先看示例代码,示例是用javax.jws.WebService写的,经测试有效。
import javax.annotation.Resource;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
@WebService
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT)
public class TokenWebService {
@Resource
private WebServiceContext wsContext;
@WebMethod
public String U_validate(
@WebParam(name = "a", targetNamespace = "http://a.org/") String a,
@WebParam(name = "b", targetNamespace = "http://a.org/") String b,
@WebParam(name = "c", targetNamespace = "http://a.org/") String c) {
//获取对象
MessageContext mc = wsContext.getMessageContext();
HttpServletRequest request = (HttpServletRequest)(mc.get(MessageContext.SERVLET_REQUEST));
//获取客户端IP
String remortAddress = request.getRemoteAddr();
System.out.println("remortAddress:"+remortAddress);
return "无效的参数";
}
}