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 netInterfaces = (Enumeration) NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement(); Enumeration 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;i0){ 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; } }