管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-09-29 d1553dc43f9bbdb95db7cbe8760b40e2986d1d4d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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(240 * 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);
                    break;
                }
            }
        }
    }
 
    /**
     * 获取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;
    }
}