利用spring获取ip地址

  • Post author:
  • Post category:其他


package com.test;

import javax.servlet.http.HttpServletRequest;

import org.springframework.util.StringUtils;

public class RequestUtil {



/**



* 获取IP地址



* @param req



* @return String IP地址



*/



public static String getIP(HttpServletRequest request) {



String ip = “”;



ip = request.getHeader(“Cdn-Src-Ip”);



if(StringUtils.isEmpty(ip)||ip.equalsIgnoreCase(“unknown”)){



ip = request.getHeader(“X-Forwared-For”);



if(StringUtils.isEmpty(ip)||ip.equalsIgnoreCase(“unknown”)){



ip = request.getHeader(“X-Real-IP”);



if(“unknown”.equalsIgnoreCase(ip)){



return  request.getRemoteAddr();



}else{



return ip;



}



}else{



return ip;



}



}else{



return ip;



}



}




}



版权声明:本文为heweimingming原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。