WebUtil.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package com.fdkankan.web.util;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import cn.hutool.http.ContentType;
  5. import cn.hutool.http.useragent.UserAgent;
  6. import cn.hutool.http.useragent.UserAgentUtil;
  7. import com.alibaba.fastjson.JSON;
  8. import java.util.Enumeration;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import javax.servlet.http.HttpServletRequest;
  12. import org.aspectj.lang.JoinPoint;
  13. import org.springframework.http.HttpHeaders;
  14. import org.springframework.http.server.reactive.ServerHttpRequest;
  15. /**
  16. * <p>
  17. * TODO
  18. * </p>
  19. *
  20. * @author dengsixing
  21. * @since 2022/6/9
  22. **/
  23. public class WebUtil {
  24. private final static String UNKNOWN = "unknown";
  25. /**
  26. * aop中获取请求参数中的参数值
  27. * @param pjp
  28. * @param request
  29. * @return
  30. */
  31. public static String getParameter(String paramName, JoinPoint pjp, HttpServletRequest request){
  32. Object[] args = pjp.getArgs();
  33. String contentType = request.getContentType();
  34. if(StrUtil.isNotEmpty(contentType) && contentType.contains(ContentType.JSON.getValue())){
  35. HashMap hashMap = JSON.parseObject(JSON.toJSONString(args[0]), HashMap.class);
  36. return (String) hashMap.get(paramName);
  37. }
  38. return request.getParameter(paramName);
  39. }
  40. public static Map<String, Object> getParameter(JoinPoint pjp, HttpServletRequest request) {
  41. Object[] args = pjp.getArgs();
  42. String contentType = request.getContentType();
  43. if (StrUtil.isNotEmpty(contentType) && contentType.contains(ContentType.JSON.getValue())) {
  44. return JSON.parseObject(JSON.toJSONString(args[0]), HashMap.class);
  45. } else {
  46. Enumeration<String> parameterNames = request.getParameterNames();
  47. if(CollUtil.isEmpty(parameterNames)){
  48. return null;
  49. }
  50. Map<String, Object> params = new HashMap<>();
  51. String name = null;
  52. do {
  53. name = parameterNames.nextElement();
  54. params.put(name, request.getParameter(name));
  55. }while (parameterNames.hasMoreElements());
  56. return params;
  57. }
  58. }
  59. /**
  60. * 获取客户端请求终端地址
  61. * @param request 客户端请求request
  62. * @return 终端ip地址
  63. */
  64. public static String getIpAddress(HttpServletRequest request) {
  65. String ip = request.getHeader("X-Forwarded-For");
  66. if(StrUtil.isNotBlank(ip) && UNKNOWN.equalsIgnoreCase(ip)){
  67. if(ip.indexOf(",") != -1){
  68. ip = ip.split(",")[0];
  69. return ip;
  70. }
  71. }
  72. ip = request.getHeader("Proxy-Client-IP");
  73. if(StrUtil.isNotBlank(ip)) return ip;
  74. ip = request.getHeader("WL-Proxy-Client-IP");
  75. if(StrUtil.isNotBlank(ip)) return ip;
  76. ip = request.getHeader("HTTP-CLIENT-IP");
  77. if(StrUtil.isNotBlank(ip)) return ip;
  78. ip = request.getHeader("HTTP-X-FORWARDED-FOR");
  79. if(StrUtil.isNotBlank(ip)) return ip;
  80. ip = request.getHeader("X-Real-IP");
  81. if(StrUtil.isNotBlank(ip)) return ip;
  82. ip = request.getRemoteAddr();
  83. return ip;
  84. }
  85. /**
  86. * <p>
  87. 获取浏览器版本
  88. * </p>
  89. * @author dengsixing
  90. * @date 2022/8/19
  91. * @param request
  92. * @return java.lang.String
  93. **/
  94. public static String getBrowser(HttpServletRequest request){
  95. String userAgentStr = request.getHeader("User-Agent");
  96. UserAgent userAgent = UserAgentUtil.parse(userAgentStr);
  97. String browserType = userAgent.getBrowser().toString();
  98. String browserVersion = userAgent.getVersion();
  99. String browserFormat = "%s(版本%s)";
  100. return String.format(browserFormat, browserType, browserVersion);
  101. }
  102. }