Spring boot WebRequest

WebRequest 是用于获取前台传递过来的一些信息!例如

  • WebRequest 获取 远程IP地址
  • WebRequest 获取 Cookie
  • WebRequest获取 Session
public class WebRequest {

	private final static  String DEFAULT_ADD_SOURCE = "+";
	private final static  String DEFAULT_ADD_TARGET = "+";

	public static HttpServletRequest getHttpServletRequest(){  
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder  
                .getRequestAttributes())
                .getRequest();
        return request;  
    }

	public static HttpServletResponse getHttpServletResponse(){  
        HttpServletResponse res = ((ServletRequestAttributes)RequestContextHolder  
                .getRequestAttributes()).getResponse();
        return res;  
    }

	public static HttpSession getSession()
	{
		return (HttpSession) getHttpServletRequest().getSession();
	}

	public static String getHeader(String key)
	{
		HttpServletRequest request = getHttpServletRequest();
		return request.getHeader(key);
	}

	public static double getDouble(String key)
	{
		String value = getString(key);
		return StringUtils.asDouble(value);
	}

	public static long getLong(String key)
	{
		String value = getString(key);
		return StringUtils.asLong(value);
	}

	public static int getInt(String key)
	{
		String value = getString(key);
		return StringUtils.asInt(value);
	}

	public static String getString(String key)
	{
		return getString(key, null);
	}

	public static String[] getStrings(String key, String split)
	{
		String value = getString(key, null);
		if(StringUtils.isEmpty(value)) return null;
		return value.split(split);
	}

	public static boolean getBoolean(String key)
	{
		String value = getString(key);
		return StringUtils.asBoolean(value, false);
	}

	public static boolean getPurge()
	{
		String value = getString("purge");
		return StringUtils.asBoolean(value, false);
	}

	public static int getLimit()
	{
		String value = getString("limit");
		int limit = StringUtils.asInt(value);
		if (0 == limit) limit = 20;
		if (limit > 20) limit = 20;
		return limit;
	}

	public static String getLoginToken()
	{
		return getString("loginToken");
	}

	public static String getAccessToken()
	{
		return getString("accessToken");
	}

	public static Date getDate(String key, String parttern)
	{
		String value = getString(key);
		return DateUtils.convertDate(parttern, value);
	}

	public static Date getDate(String key)
	{
		return getDate(key, DateUtils.TYPE_YYYYMMDDHHMMSS);
	}

	public static String getString(String key, String def)
	{
		HttpServletRequest request = getHttpServletRequest();
		String value = request.getParameter(key);
		if(StringUtils.isEmpty(value)) return def;
		value = UrlUtils.decode(value);
		value = value.replace(DEFAULT_ADD_SOURCE, DEFAULT_ADD_TARGET);
		value = value.trim();
		return value;
	}

	public static String getCookie(String key)
	{
		HttpServletRequest request = getHttpServletRequest();
		Cookie[] cookies =  request.getCookies();
		if(cookies == null || cookies.length == 0) return null;

		for(Cookie cookie : cookies){
			if(cookie.getName().equals(key)){
				return cookie.getValue();
			}
	    }
		return null;
	}

	public static void setCookie(String key, String value, int expires)
	{
		HttpServletResponse response = getHttpServletResponse();
		Cookie cookie = new Cookie(key, value);
		cookie.setMaxAge(expires);
		response.addCookie(cookie);
	}

	public static void logForm()
	{
		HttpServletRequest request = getHttpServletRequest();
		Enumeration<String> names = request.getParameterNames();
		while(names.hasMoreElements())
		{
			String name = names.nextElement();
			String value = request.getParameter(name);
			System.out.println(name + " = " + value);
		}
	}

	public static void logHeader()
	{
		HttpServletRequest request = getHttpServletRequest();
		Enumeration<String> names = request.getHeaderNames();
		while(names.hasMoreElements())
		{
			String name = names.nextElement();
			String value = request.getParameter(name);
			System.out.println(name + " = " + value);
		}
	}

	public static JSONObject getJSON(String key)
	{
		HttpServletRequest request = getHttpServletRequest();
		String value = request.getParameter(key);
		return FastJsonHelper.toJSONObject(value);
	}

	public static String getRemoteIP() {
		HttpServletRequest request = getHttpServletRequest();
		String ip = request.getHeader("X-Real-IP");
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			 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 || "unknown".equalsIgnoreCase(ip)) {
	      ip = request.getHeader("WL-Proxy-Client-IP");
	    }
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	      ip = request.getHeader("HTTP_CLIENT_IP");
	    }
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	      ip = request.getHeader("HTTP_X_FORWARDED_FOR");
	    }
	    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	      ip = request.getRemoteAddr();
	    }
	    return ip;
	  }

	public static String getRemoteIP(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 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

}