Java WebService获取客户端IP

    xiaoxiao2022-06-27  165

    step 1 获取WebServiceContext:

    //方法一 @Resource private WebServiceContext context ;//此方法可能为null //方法二 private WebServiceContext context = new org.apache.cxf.jaxws.context.WebServiceContextImpl(); public WebServiceContext getContext() { return context; } public void setContext(WebServiceContext context) { this.context = context; }

    step 2 获取客户端IP:

    public String getClientIp() { String remortAddress = ""; try { // 获取对象 MessageContext mc = context.getMessageContext(); HttpServletRequest request = (HttpServletRequest) (mc.get(MessageContext.SERVLET_REQUEST)); // 获取客户端IP remortAddress = request.getRemoteAddr(); System.out.println("remortAddress:" + remortAddress); } catch (Exception e) { e.printStackTrace(); } return remortAddress; }

    最新回复(0)