package com.lf.server.helper;
|
|
import com.lf.server.entity.all.StaticData;
|
|
import javax.servlet.http.Cookie;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import java.sql.Timestamp;
|
import java.util.Calendar;
|
import java.util.UUID;
|
|
/**
|
* Web帮助类
|
* @author WWW
|
*/
|
public class WebHelper {
|
private final static String UNKNOWN = "unknown";
|
|
private final static String COMMA = ",";
|
|
/**
|
* 获取GUID
|
*/
|
public static String getGuid() {
|
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());
|
}
|
|
/**
|
* 从Cookie中获取token
|
*
|
* @param request
|
* @return
|
*/
|
public static String getTokenFromCookie(HttpServletRequest request) {
|
Cookie[] cookies = request.getCookies();
|
if (cookies == null || cookies.length == 0) {
|
return null;
|
}
|
|
for (Cookie cookie : cookies) {
|
switch (cookie.getName()) {
|
case StaticData.TOKEN_COOKIE_KEY:
|
return cookie.getValue();
|
default:
|
break;
|
}
|
}
|
|
return null;
|
}
|
|
/**
|
* 向Cookie中添加token
|
*
|
* @param token
|
* @param request
|
* @param response
|
*/
|
public static void saveToken2Cookie(String token, HttpServletRequest request, HttpServletResponse response) {
|
// 先删除
|
deleteCookie(StaticData.TOKEN_COOKIE_KEY, request);
|
|
// 再保存
|
saveCookie(StaticData.TOKEN_COOKIE_KEY, token, response);
|
}
|
|
/**
|
* 保存Cookie
|
*
|
* @param cookieKey
|
* @param value
|
* @param response
|
*/
|
public static void saveCookie(String cookieKey, String value, HttpServletResponse response) {
|
Cookie cookie = new Cookie(cookieKey, value);
|
// 设置cookie失效时间,单位为秒
|
cookie.setMaxAge(4 * 60 * 60);
|
cookie.setHttpOnly(false);
|
cookie.setPath("/");
|
// cookie.setDomain("")
|
|
response.setHeader("P3P", "CP=CAO PSA OUR");
|
response.addCookie(cookie);
|
}
|
|
/**
|
* 删除cookie中的值
|
*
|
* @param cookieKey
|
* @param request
|
*/
|
public static void deleteCookie(String cookieKey, HttpServletRequest request) {
|
Cookie[] cookies = request.getCookies();
|
if (cookies != null && cookies.length > 0) {
|
for (Cookie c : cookies) {
|
if (cookieKey.equalsIgnoreCase(c.getName())) {
|
c.setMaxAge(0);
|
}
|
}
|
}
|
}
|
|
/**
|
* 获取Token
|
*
|
* @param request
|
* @return
|
*/
|
public static String getToken(HttpServletRequest request) {
|
// 1.从url参数中,获取token
|
String token = request.getParameter(StaticData.TOKEN_KEY);
|
|
// 2.为空,则从header里获取
|
if (token == null) {
|
token = request.getHeader(StaticData.TOKEN_KEY);
|
}
|
|
// 3.还为空,则从cookie里获取
|
if (token == null) {
|
token = getTokenFromCookie(request);
|
}
|
|
return token;
|
}
|
}
|