package com.lf.server.helper;
|
|
import com.lf.server.entity.all.SettingData;
|
import com.lf.server.entity.all.StaticData;
|
import org.springframework.web.context.request.RequestContextHolder;
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import javax.servlet.ServletContext;
|
import javax.servlet.http.Cookie;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpSession;
|
import java.io.IOException;
|
import java.io.PrintWriter;
|
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
|
*/
|
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
|
*/
|
public static Timestamp getTimestamp(int min) {
|
Calendar now = Calendar.getInstance();
|
now.add(Calendar.MINUTE, min);
|
|
return new Timestamp(now.getTimeInMillis());
|
}
|
|
/**
|
* 从Cookie中获取token
|
*/
|
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
|
*/
|
public static void saveToken2Cookie(String token, HttpServletRequest request, HttpServletResponse response) {
|
// 先删除
|
deleteCookies(request, response);
|
|
// 再保存
|
saveCookie(StaticData.TOKEN_COOKIE_KEY, token, response);
|
}
|
|
/**
|
* 保存Cookie
|
*/
|
public static void saveCookie(String key, String value, HttpServletResponse response) {
|
Cookie cookie = new Cookie(key, value);
|
// 设置cookie失效时间,单位为秒
|
cookie.setMaxAge(SettingData.COOKIE_MAX_AGE);
|
cookie.setHttpOnly(false);
|
cookie.setPath("/");
|
//cookie.setDomain("*")
|
|
response.setHeader("P3P", "CP=CAO PSA OUR");
|
response.addCookie(cookie);
|
}
|
|
/**
|
* 删除cookie中的值
|
*/
|
public static void deleteCookie(String cookieKey, HttpServletRequest request, HttpServletResponse response) {
|
Cookie[] cookies = request.getCookies();
|
if (cookies != null && cookies.length > 0) {
|
for (Cookie c : cookies) {
|
if (cookieKey.equalsIgnoreCase(c.getName())) {
|
c.setMaxAge(0);
|
c.setPath("/");
|
response.addCookie(c);
|
}
|
}
|
}
|
}
|
|
/**
|
* 删除所有Cookie
|
*/
|
public static void deleteCookies(HttpServletRequest request, HttpServletResponse response) {
|
Cookie[] cookies = request.getCookies();
|
if (cookies != null && cookies.length > 0) {
|
for (Cookie c : cookies) {
|
c.setMaxAge(0);
|
c.setPath("/");
|
response.addCookie(c);
|
}
|
}
|
}
|
|
/**
|
* 根据键获取Cookie值
|
*/
|
public static String getCookieByKey(String key, HttpServletRequest request) {
|
Cookie[] cookies = request.getCookies();
|
if (cookies == null || cookies.length == 0) {
|
return null;
|
}
|
|
for (Cookie c : cookies) {
|
if (key.equals(c.getName())) {
|
return c.getValue();
|
}
|
}
|
|
return null;
|
}
|
|
/**
|
* 获取Token
|
*/
|
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;
|
}
|
|
/**
|
* 获取Request
|
*/
|
public static HttpServletRequest getRequest() {
|
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
return servletRequestAttributes.getRequest();
|
}
|
|
/**
|
* 获取Response
|
*/
|
public static HttpServletResponse getResponse() {
|
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
return servletRequestAttributes.getResponse();
|
}
|
|
/**
|
* 获取Session
|
*/
|
public static HttpSession getSession() {
|
return getRequest().getSession();
|
}
|
|
/**
|
* 获取真实路径
|
*/
|
public static String getRealPath(String path) {
|
HttpServletRequest req = getRequest();
|
ServletContext ctx = req.getSession().getServletContext();
|
|
return ctx.getRealPath("/" + path);
|
}
|
|
/**
|
* 输出json数据到前端
|
*/
|
public static boolean write2Page(HttpServletResponse response, String jsonPack) throws IOException {
|
response.setContentType("application/json;charset=UTF-8");
|
response.setHeader("Cache-Control", "no-cache");
|
response.setHeader("Pragma", "No-cache");
|
response.setDateHeader("Expires", 0);
|
|
PrintWriter out = response.getWriter();
|
out.print(jsonPack);
|
|
out.flush();
|
out.close();
|
|
return false;
|
}
|
}
|