派生自 projectDept/qhighschool

胡仁荣
2022-10-31 fe7381d6e8ec1f427408de0297ac7f41533202f6
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;
    }
}