package com.lf.server.helper;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.sql.Timestamp;
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.UUID;
|
|
/**
|
* Web帮助类
|
* @author WWW
|
*/
|
public class WebHelper {
|
private final static String UNKNOWN = "unknown";
|
|
private final static String COMMA = ",";
|
|
public static String getGuid() {
|
/**
|
* 获取GUID
|
*/
|
return UUID.randomUUID().toString();
|
}
|
|
/**
|
* 获取用户ip
|
*
|
* @param request
|
* @return
|
*/
|
public static String getIpAddress(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_X_FORWARDED_FOR");
|
}
|
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_X_FORWARDED");
|
}
|
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_X_CLUSTER_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_FORWARDED_FOR");
|
}
|
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_FORWARDED");
|
}
|
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_VIA");
|
}
|
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
|
ip = request.getHeader("REMOTE_ADDR");
|
}
|
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
|
ip = request.getRemoteAddr();
|
}
|
if (ip.contains(COMMA)) {
|
return ip.split(",")[0];
|
}
|
|
return ip;
|
}
|
|
/**
|
* 获取当前时间的Timestamp
|
*/
|
public static Timestamp getCurrentTimestamp() {
|
return new Timestamp(System.currentTimeMillis());
|
}
|
|
/**
|
* 获取当前时间指定分钟数后的Timestamp
|
*
|
* @param min 分钟数
|
* @return
|
*/
|
public static Timestamp getTimestamp(int min) {
|
Calendar now = Calendar.getInstance();
|
now.add(Calendar.MINUTE, min);
|
|
return new Timestamp(now.getTimeInMillis());
|
}
|
}
|