WebUtil.java 3.9 KB

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