派生自 projectDept/qhighschool

EricsHu
2022-12-05 068fc7f2e81178e55fa191a13709af64b1a163f6
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
package com.qxueyou.scc.base.util;
 
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
 
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import ua_parser.Client;
import ua_parser.Parser;
 
 
 
public final class RequestClientUtils {
    
    private static Logger log = LogManager.getLogger(RequestClientUtils.class);
    
    private static final String[][] agentArrs = new String[][]{
        {"micromessenger","Weixin"},
        {"android","Android"},
        {"mac os","Mac"},
        {"windows phone","Windows"}};
        
    
    /**
     * 获取客户端ip地址
     * 
     * @param request
     * @return
     */
    public static String getRemoteIP(HttpServletRequest request) {
        
        if (request.getHeader("x-real-ip") == null) {
            return request.getRemoteAddr();
        }
        return request.getHeader("x-real-ip");
        
    }
    
    /**
     * 获取服务器本地IP
     * @return
     */
    public static String getLocalIP() {
        InetAddress ip = null;
        try {
            Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface.getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
                Enumeration<InetAddress> ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    ip = (InetAddress) ips.nextElement();
                    // 127.开头的都是lookback地址
                    if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
                        return ip.getHostAddress();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 获取COOKIE : KEY
     * @param httpRequest
     * @return
     */
    public static Cookie getKeyCookie(HttpServletRequest httpRequest) {
        Cookie[] cookies = httpRequest.getCookies();
        Cookie cookieKey = null;
        
        if(cookies==null){
            return null;
        }
        
        for(Cookie cookie:cookies){
            if("KEY".equals(cookie.getName())){
                cookieKey = cookie;
                break;
            }
        }
        return cookieKey;
    }
 
    /**
     * 解析客户端
     * @param userAgent userAgent
     * @return
     */
    public static String parseClient(String userAgent){
        
        if(userAgent==null){
            return null;
        }
        
        int leftBracketStartIdx = userAgent.indexOf('(');
        int rightBracketStartIdx = userAgent.indexOf(')');
        
        
        if(leftBracketStartIdx<0 || rightBracketStartIdx<0){
            return userAgent;
        }
        
        return userAgent.substring(leftBracketStartIdx+1, rightBracketStartIdx);
    }
    
    
    /**
     * 判断是PC端还是移动端
     * 
     * @return
     */
    public static boolean  isMobileDevice(String requestHeader){
        /**
         * android : 所有android设备
         * iphone : iphone 
         * windows phone:Nokia等windows系统的手机
         * ipad :ipad
         */
        String lowerRequestHeader = requestHeader;
        String[] deviceArray = new String[]{"android","iphone","windows phone","ipad"};
        if(lowerRequestHeader == null) {
            return false;
        }
        lowerRequestHeader = lowerRequestHeader.toLowerCase();
        for(int i=0;i<deviceArray.length;i++){
            if(lowerRequestHeader.indexOf(deviceArray[i])>0){
                return true;
            }
        }
        
        return false;
    }
    
    /**
     * 判断是PC端还是移动端
     * 
     * @return
     */
    public static String  getAgentDevice(String requestHeader){
        
        for(String[] agent:agentArrs){
            if(requestHeader.toLowerCase().indexOf(agent[0])>0){
                return agent[1];
            }
        }
        return "未知";
    }
    
    /**
     * 判断是否在微信浏览器打开
     * 
     * @return
     */
    public static boolean  isWeixin(String userAgent){
        
        return getAgentDevice(userAgent).equals("Weixin");
    }
    
    /**
     * 获取头信息
     * 
     * @param userAgent
     * @return
     */
    public static Client userAgent(String userAgent){
        Parser uaParser;
        try {
            uaParser = new Parser();
            return uaParser.parse(userAgent);
        } catch (IOException e) {
            log.error("解析头信息失败", e);
        }
        return null;
    }
}