cfx 取IP

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;

import org.apache.cxf.message.Message;
import org.apache.cxf.phase.PhaseInterceptorChain;
import org.apache.cxf.transport.http.AbstractHTTPDestination;
import org.springframework.stereotype.Component;

@Component
public class CxfUtil {
	
	@Context
	private WebServiceContext context;
	
	//webService获取访问者ip方法
	public String getClientIpCxf() {
		MessageContext ctx = context.getMessageContext();
		HttpServletRequest request = (HttpServletRequest)ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
		return request.getRemoteAddr(); 
	}
	
	//restful获取访问者ip方法
	public String getClientIpCxfRest() {
		Message message = PhaseInterceptorChain.getCurrentMessage();
		HttpServletRequest request = (HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST);
		return request.getRemoteAddr(); 
	}


}

 

你可能感兴趣的:(IP)