Three ways to obtain the request object on the webservice server side

  Sometimes we need to obtain the request object and the response object in the webservice. For example, we need to do this when we want to obtain the client's access ip. Here are three ways. Of course, the three ways may be to deploy the webservice in different ways to obtain the request object. Methods.

The first: first configure the injection:

@Resource
private WebServiceContext webServiceContext;

followed by the following code:

MessageContext mc = webServiceContext.getMessageContext();
HttpServletRequest request = (HttpServletRequest) (mc.get(MessageContext.SERVLET_REQUEST));

The second:

WebServiceContext context = new WebServiceContextImpl();
MessageContext ctx = context.getMessageContext();
HttpServletRequest request = (HttpServletRequest) ctx.get(AbstractHTTPDestination.HTTP_REQUEST);

The third (with a method to get the client ip address):

Message message = PhaseInterceptorChain.getCurrentMessage();
HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);

get ip:

copy code
public static String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknow".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
copy code

  Sometimes we need to obtain the request object and the response object in the webservice. For example, we need to do this when we want to obtain the client's access ip. Here are three ways. Of course, the three ways may be to deploy the webservice in different ways to obtain the request object. Methods.

The first: first configure the injection:

@Resource
private WebServiceContext webServiceContext;

followed by the following code:

MessageContext mc = webServiceContext.getMessageContext();
HttpServletRequest request = (HttpServletRequest) (mc.get(MessageContext.SERVLET_REQUEST));

The second:

WebServiceContext context = new WebServiceContextImpl();
MessageContext ctx = context.getMessageContext();
HttpServletRequest request = (HttpServletRequest) ctx.get(AbstractHTTPDestination.HTTP_REQUEST);

The third (with a method to get the client ip address):

Message message = PhaseInterceptorChain.getCurrentMessage();
HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);

get ip:

copy code
public static String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknow".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
copy code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324594424&siteId=291194637