ControllerUtil.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. package com.sooka.common.utils;
  2. import com.google.code.kaptcha.Constants;
  3. import org.springframework.web.context.request.RequestContextHolder;
  4. import org.springframework.web.context.request.ServletRequestAttributes;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import java.io.IOException;
  9. import java.io.PrintWriter;
  10. import java.net.InetAddress;
  11. import java.net.UnknownHostException;
  12. /**
  13. * Description:util
  14. *
  15. *
  16. * @create 2017-04-06
  17. **/
  18. public class ControllerUtil {
  19. private ControllerUtil() {
  20. throw new Error("工具类不能实例化!");
  21. }
  22. /**
  23. * 判断是否为Ajav请求
  24. * @param request
  25. * @return
  26. */
  27. public static boolean isAjaxRequest(HttpServletRequest request){
  28. if (!"XMLHttpRequest" .equalsIgnoreCase(request.getHeader("X-Requested-With"))) {
  29. return false;
  30. }
  31. return true;
  32. }
  33. /**
  34. * 判断验证码是否正确
  35. * @param verifyCode
  36. * @param request
  37. * @return
  38. */
  39. public static boolean validate(String verifyCode,HttpServletRequest request) {
  40. String text = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
  41. if (StrUtil.isBlank(text)) {
  42. return false;
  43. }
  44. return verifyCode.equals(text);
  45. }
  46. /**
  47. * post请求
  48. * @return
  49. */
  50. public static boolean isPost() {
  51. HttpServletRequest request = getHttpServletRequest();
  52. String requersMethod = request.getMethod();
  53. if (requersMethod.equals("POST") || "POST".equals(requersMethod)) {
  54. return true;
  55. }
  56. return false;
  57. }
  58. /**
  59. * get请求
  60. *
  61. * @return
  62. */
  63. public static boolean isGet() {
  64. HttpServletRequest request = getHttpServletRequest();
  65. String requersMethod = request.getMethod();
  66. if (requersMethod.equals("GET") || "GET".equals(requersMethod)) {
  67. return true;
  68. }
  69. return false;
  70. }
  71. /**
  72. * 获取请求域名,域名不包括http请求协议头
  73. *
  74. * @return 返回域名地址
  75. *
  76. */
  77. public static String getDomain() {
  78. HttpServletRequest request = getHttpServletRequest();
  79. String path = request.getContextPath();
  80. String domain = request.getServerName();
  81. if (request.getServerPort() == 80) {
  82. domain += path;
  83. } else {
  84. domain += ":" + request.getServerPort() + path;
  85. }
  86. return domain;
  87. }
  88. /**
  89. * 读取服务器主机ip信息
  90. *
  91. * @return 返回主机IP,异常将会获取不到ip
  92. */
  93. public static String getHostIp() {
  94. InetAddress addr;
  95. try {
  96. addr = InetAddress.getLocalHost();
  97. return addr.getHostAddress().toString();// 获得本机IP
  98. } catch (UnknownHostException e) {
  99. e.printStackTrace();
  100. }
  101. return "";
  102. }
  103. /**
  104. *
  105. * 获取请求客户端ip
  106. *
  107. * @param request
  108. *
  109. * @return ip地址
  110. *
  111. */
  112. public static String getRemoteAddress(HttpServletRequest request) {
  113. String ip = request.getHeader("x-forwarded-for");
  114. if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
  115. ip = request.getHeader("Proxy-Client-IP");
  116. }
  117. if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
  118. ip = request.getHeader("WL-Proxy-Client-IP");
  119. }
  120. if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
  121. ip = request.getRemoteAddr();
  122. }
  123. return ip;
  124. }
  125. public static void renderErrorHtml(String msg, String desc, HttpServletRequest request, HttpServletResponse response) {
  126. try {
  127. response.setHeader("Content-type", "text/html;charset=UTF-8");
  128. response.setCharacterEncoding("UTF-8");
  129. StringBuffer content = new StringBuffer();
  130. content.append("<!DOCTYPE html>");
  131. content.append("<html>");
  132. content.append("<head>");
  133. content.append("<meta charset=\"UTF-8\">");
  134. content.append("<title>"+msg+"</title>");
  135. content.append("</head>");
  136. content.append("<body>");
  137. content.append("<p style=\"font-size:22px;padding-left:10px;\"><b>"+msg+"</b></p>");
  138. content.append("<p style=\"font-size:18px;padding-left:10px;\">描述 : " + desc + "</p>");
  139. content.append("<p style=\"font-size:18px;padding-left:10px;\">地址 : " +request.getRequestURL()+"</p>");
  140. content.append("</body>");
  141. content.append("</html>");
  142. PrintWriter out = response.getWriter();
  143. out.println(content.toString());
  144. out.flush();
  145. out.close();
  146. } catch (IOException e) {
  147. e.printStackTrace();
  148. }
  149. }
  150. public static void renderErrorJson(String msg, HttpServletResponse response) {
  151. response.setHeader("Content-type", "application/x-json;charset=UTF-8");
  152. response.setCharacterEncoding("UTF-8");
  153. try {
  154. PrintWriter out = response.getWriter();
  155. out.println(JsonUtil.toERROR(msg));
  156. out.flush();
  157. out.close();
  158. } catch (IOException e) {
  159. e.printStackTrace();
  160. }
  161. }
  162. public static void renderTimeoutJson(String msg, HttpServletResponse response){
  163. response.setHeader("Content-type", "application/x-json;charset=UTF-8");
  164. response.setCharacterEncoding("UTF-8");
  165. try {
  166. PrintWriter out = response.getWriter();
  167. out.println(JsonUtil.toTIMEOUT(msg));
  168. out.flush();
  169. out.close();
  170. } catch (IOException e) {
  171. e.printStackTrace();
  172. }
  173. }
  174. public static HttpServletRequest getHttpServletRequest(){
  175. ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  176. HttpServletRequest request = attributes.getRequest();
  177. return request;
  178. }
  179. public static HttpServletResponse getHttpServletResponse(){
  180. ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  181. HttpServletResponse response = attributes.getResponse();
  182. return response;
  183. }
  184. public static HttpSession getHttpSession(){
  185. ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  186. HttpServletRequest request = attributes.getRequest();
  187. return request.getSession();
  188. }
  189. public static boolean isMobile() {
  190. HttpServletRequest request = getHttpServletRequest();
  191. boolean b = false;
  192. boolean pcFlag = false;
  193. boolean mobileFlag = false;
  194. String via = request.getHeader("Via");
  195. String userAgent = request.getHeader("user-agent");
  196. for (int i = 0; via != null && via.trim().length()!=0 && i < mobileGateWayHeaders.length; i++) {
  197. if (via.contains(mobileGateWayHeaders[i])) {
  198. mobileFlag = true;
  199. break;
  200. }
  201. }
  202. for (int i = 0; !mobileFlag && userAgent != null && userAgent.trim().length()!=0
  203. && i < mobileUserAgents.length; i++) {
  204. if (userAgent.contains(mobileUserAgents[i])) {
  205. mobileFlag = true;
  206. break;
  207. }
  208. }
  209. for (int i = 0; userAgent != null && userAgent.trim().length()!=0 && i < pcHeaders.length; i++) {
  210. if (userAgent.contains(pcHeaders[i])) {
  211. pcFlag = true;
  212. break;
  213. }
  214. }
  215. if (mobileFlag == true && pcFlag == false) {
  216. b = true;
  217. }
  218. return b;
  219. }
  220. private static String mobileGateWayHeaders[] = new String[] { "ZXWAP", // 中兴提供的wap网关的via信息,例如:Via=ZXWAP
  221. // GateWayZTE
  222. // Technologies,
  223. "chinamobile.com", // 中国移动的诺基亚wap网关,例如:Via=WTP/1.1
  224. // GDSZ-PB-GW003-WAP07.gd.chinamobile.com (Nokia
  225. // WAP Gateway 4.1 CD1/ECD13_D/4.1.04)
  226. "monternet.com", // 移动梦网的网关,例如:Via=WTP/1.1
  227. // BJBJ-PS-WAP1-GW08.bj1.monternet.com. (Nokia WAP
  228. // Gateway 4.1 CD1/ECD13_E/4.1.05)
  229. "infoX", // 华为提供的wap网关,例如:Via=HTTP/1.1 GDGZ-PS-GW011-WAP2
  230. // (infoX-WISG
  231. // Huawei Technologies),或Via=infoX WAP Gateway V300R001
  232. // Huawei Technologies
  233. "XMS 724Solutions HTG", // 国外电信运营商的wap网关,不知道是哪一家
  234. "wap.lizongbo.com", // 自己测试时模拟的头信息
  235. "Bytemobile",// 貌似是一个给移动互联网提供解决方案提高网络运行效率的,例如:Via=1.1 Bytemobile OSN
  236. // WebProxy/5.1
  237. };
  238. private static String[] pcHeaders = new String[] { "Windows 98", "Windows ME", "Windows 2000", "Windows XP",
  239. "Windows NT", "Ubuntu" };
  240. private static String[] mobileUserAgents = new String[] { "Nokia", // 诺基亚,有山寨机也写这个的,总还算是手机,Mozilla/5.0
  241. // (Nokia5800
  242. // XpressMusic)UC
  243. // AppleWebkit(like
  244. // Gecko)
  245. // Safari/530
  246. "SAMSUNG", // 三星手机
  247. // SAMSUNG-GT-B7722/1.0+SHP/VPP/R5+Dolfin/1.5+Nextreaming+SMM-MMS/1.2.0+profile/MIDP-2.1+configuration/CLDC-1.1
  248. "MIDP-2", // j2me2.0,Mozilla/5.0 (SymbianOS/9.3; U; Series60/3.2
  249. // NokiaE75-1 /110.48.125 Profile/MIDP-2.1
  250. // Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML like
  251. // Gecko) Safari/413
  252. "CLDC1.1", // M600/MIDP2.0/CLDC1.1/Screen-240X320
  253. "SymbianOS", // 塞班系统的,
  254. "MAUI", // MTK山寨机默认ua
  255. "UNTRUSTED/1.0", // 疑似山寨机的ua,基本可以确定还是手机
  256. "Windows CE", // Windows CE,Mozilla/4.0 (compatible; MSIE 6.0;
  257. // Windows CE; IEMobile 7.11)
  258. "iPhone", // iPhone是否也转wap?不管它,先区分出来再说。Mozilla/5.0 (iPhone; U; CPU
  259. // iPhone OS 4_1 like Mac OS X; zh-cn) AppleWebKit/532.9
  260. // (KHTML like Gecko) Mobile/8B117
  261. "iPad", // iPad的ua,Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X;
  262. // zh-cn) AppleWebKit/531.21.10 (KHTML like Gecko)
  263. // Version/4.0.4 Mobile/7B367 Safari/531.21.10
  264. "Android", // Android是否也转wap?Mozilla/5.0 (Linux; U; Android
  265. // 2.1-update1; zh-cn; XT800 Build/TITA_M2_16.22.7)
  266. // AppleWebKit/530.17 (KHTML like Gecko) Version/4.0
  267. // Mobile Safari/530.17
  268. "BlackBerry", // BlackBerry8310/2.7.0.106-4.5.0.182
  269. "UCWEB", // ucweb是否只给wap页面? Nokia5800
  270. // XpressMusic/UCWEB7.5.0.66/50/999
  271. "ucweb", // 小写的ucweb貌似是uc的代理服务器Mozilla/6.0 (compatible; MSIE 6.0;)
  272. // Opera ucweb-squid
  273. "BREW", // 很奇怪的ua,例如:REW-Applet/0x20068888 (BREW/3.1.5.20; DeviceId:
  274. // 40105; Lang: zhcn) ucweb-squid
  275. "J2ME", // 很奇怪的ua,只有J2ME四个字母
  276. "YULONG", // 宇龙手机,YULONG-CoolpadN68/10.14 IPANEL/2.0 CTC/1.0
  277. "YuLong", // 还是宇龙
  278. "COOLPAD", // 宇龙酷派YL-COOLPADS100/08.10.S100 POLARIS/2.9 CTC/1.0
  279. "TIANYU", // 天语手机TIANYU-KTOUCH/V209/MIDP2.0/CLDC1.1/Screen-240X320
  280. "TY-", // 天语,TY-F6229/701116_6215_V0230 JUPITOR/2.2 CTC/1.0
  281. "K-Touch", // 还是天语K-Touch_N2200_CMCC/TBG110022_1223_V0801 MTK/6223
  282. // Release/30.07.2008 Browser/WAP2.0
  283. "Haier", // 海尔手机,Haier-HG-M217_CMCC/3.0 Release/12.1.2007
  284. // Browser/WAP2.0
  285. "DOPOD", // 多普达手机
  286. "Lenovo", // 联想手机,Lenovo-P650WG/S100 LMP/LML Release/2010.02.22
  287. // Profile/MIDP2.0 Configuration/CLDC1.1
  288. "LENOVO", // 联想手机,比如:LENOVO-P780/176A
  289. "HUAQIN", // 华勤手机
  290. "AIGO-", // 爱国者居然也出过手机,AIGO-800C/2.04 TMSS-BROWSER/1.0.0 CTC/1.0
  291. "CTC/1.0", // 含义不明
  292. "CTC/2.0", // 含义不明
  293. "CMCC", // 移动定制手机,K-Touch_N2200_CMCC/TBG110022_1223_V0801 MTK/6223
  294. // Release/30.07.2008 Browser/WAP2.0
  295. "DAXIAN", // 大显手机DAXIAN X180 UP.Browser/6.2.3.2(GUI) MMP/2.0
  296. "MOT-", // 摩托罗拉,MOT-MOTOROKRE6/1.0 LinuxOS/2.4.20 Release/8.4.2006
  297. // Browser/Opera8.00 Profile/MIDP2.0 Configuration/CLDC1.1
  298. // Software/R533_G_11.10.54R
  299. "SonyEricsson", // 索爱手机,SonyEricssonP990i/R100 Mozilla/4.0
  300. // (compatible; MSIE 6.0; Symbian OS; 405) Opera
  301. // 8.65 [zh-CN]
  302. "GIONEE", // 金立手机
  303. "HTC", // HTC手机
  304. "ZTE", // 中兴手机,ZTE-A211/P109A2V1.0.0/WAP2.0 Profile
  305. "HUAWEI", // 华为手机,
  306. "webOS", // palm手机,Mozilla/5.0 (webOS/1.4.5; U; zh-CN)
  307. // AppleWebKit/532.2 (KHTML like Gecko) Version/1.0
  308. // Safari/532.2 Pre/1.0
  309. "GoBrowser", // 3g GoBrowser.User-Agent=Nokia5230/GoBrowser/2.0.290
  310. // Safari
  311. "IEMobile", // Windows CE手机自带浏览器,
  312. "WAP2.0"// 支持wap 2.0的
  313. };
  314. }