ecf4ca6f5b4e65f45af87d0538368cfe22f92763.svn-base 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package org.jeecg.config.sign.util;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.UnsupportedEncodingException;
  6. import java.net.URLDecoder;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import java.util.SortedMap;
  10. import java.util.TreeMap;
  11. import javax.servlet.http.HttpServletRequest;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.jeecg.common.util.oConvertUtils;
  14. import org.springframework.http.HttpMethod;
  15. import com.alibaba.fastjson.JSONObject;
  16. /**
  17. * http 工具类 获取请求中的参数
  18. *
  19. * @author jeecg
  20. * @date 20210621
  21. */
  22. @Slf4j
  23. public class HttpUtils {
  24. /**
  25. * 将URL的参数和body参数合并
  26. *
  27. * @author jeecg
  28. * @date 20210621
  29. * @param request
  30. */
  31. public static SortedMap<String, String> getAllParams(HttpServletRequest request) throws IOException {
  32. SortedMap<String, String> result = new TreeMap<>();
  33. // 获取URL上最后带逗号的参数变量 sys/dict/getDictItems/sys_user,realname,username
  34. String pathVariable = request.getRequestURI().substring(request.getRequestURI().lastIndexOf("/") + 1);
  35. if (pathVariable.contains(",")) {
  36. log.info(" pathVariable: {}",pathVariable);
  37. String deString = URLDecoder.decode(pathVariable, "UTF-8");
  38. log.info(" pathVariable decode: {}",deString);
  39. result.put(SignUtil.xPathVariable, deString);
  40. }
  41. // 获取URL上的参数
  42. Map<String, String> urlParams = getUrlParams(request);
  43. for (Map.Entry entry : urlParams.entrySet()) {
  44. result.put((String)entry.getKey(), (String)entry.getValue());
  45. }
  46. Map<String, String> allRequestParam = new HashMap<>(16);
  47. // get请求不需要拿body参数
  48. if (!HttpMethod.GET.name().equals(request.getMethod())) {
  49. allRequestParam = getAllRequestParam(request);
  50. }
  51. // 将URL的参数和body参数进行合并
  52. if (allRequestParam != null) {
  53. for (Map.Entry entry : allRequestParam.entrySet()) {
  54. result.put((String)entry.getKey(), (String)entry.getValue());
  55. }
  56. }
  57. return result;
  58. }
  59. /**
  60. * 将URL的参数和body参数合并
  61. *
  62. * @author jeecg
  63. * @date 20210621
  64. * @param queryString
  65. */
  66. public static SortedMap<String, String> getAllParams(String url, String queryString, byte[] body, String method)
  67. throws IOException {
  68. SortedMap<String, String> result = new TreeMap<>();
  69. // 获取URL上最后带逗号的参数变量 sys/dict/getDictItems/sys_user,realname,username
  70. String pathVariable = url.substring(url.lastIndexOf("/") + 1);
  71. if (pathVariable.contains(",")) {
  72. log.info(" pathVariable: {}",pathVariable);
  73. String deString = URLDecoder.decode(pathVariable, "UTF-8");
  74. log.info(" pathVariable decode: {}",deString);
  75. result.put(SignUtil.xPathVariable, deString);
  76. }
  77. // 获取URL上的参数
  78. Map<String, String> urlParams = getUrlParams(queryString);
  79. for (Map.Entry entry : urlParams.entrySet()) {
  80. result.put((String)entry.getKey(), (String)entry.getValue());
  81. }
  82. Map<String, String> allRequestParam = new HashMap<>(16);
  83. // get请求不需要拿body参数
  84. if (!HttpMethod.GET.name().equals(method)) {
  85. allRequestParam = getAllRequestParam(body);
  86. }
  87. // 将URL的参数和body参数进行合并
  88. if (allRequestParam != null) {
  89. for (Map.Entry entry : allRequestParam.entrySet()) {
  90. result.put((String)entry.getKey(), (String)entry.getValue());
  91. }
  92. }
  93. return result;
  94. }
  95. /**
  96. * 获取 Body 参数
  97. *
  98. * @date 15:04 20210621
  99. * @param request
  100. */
  101. public static Map<String, String> getAllRequestParam(final HttpServletRequest request) throws IOException {
  102. BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
  103. String str = "";
  104. StringBuilder wholeStr = new StringBuilder();
  105. // 一行一行的读取body体里面的内容;
  106. while ((str = reader.readLine()) != null) {
  107. wholeStr.append(str);
  108. }
  109. // 转化成json对象
  110. return JSONObject.parseObject(wholeStr.toString(), Map.class);
  111. }
  112. /**
  113. * 获取 Body 参数
  114. *
  115. * @date 15:04 20210621
  116. * @param body
  117. */
  118. public static Map<String, String> getAllRequestParam(final byte[] body) throws IOException {
  119. if(body==null){
  120. return null;
  121. }
  122. String wholeStr = new String(body);
  123. // 转化成json对象
  124. return JSONObject.parseObject(wholeStr.toString(), Map.class);
  125. }
  126. /**
  127. * 将URL请求参数转换成Map
  128. *
  129. * @param request
  130. */
  131. public static Map<String, String> getUrlParams(HttpServletRequest request) {
  132. Map<String, String> result = new HashMap<>(16);
  133. if (oConvertUtils.isEmpty(request.getQueryString())) {
  134. return result;
  135. }
  136. String param = "";
  137. try {
  138. param = URLDecoder.decode(request.getQueryString(), "utf-8");
  139. } catch (UnsupportedEncodingException e) {
  140. e.printStackTrace();
  141. }
  142. String[] params = param.split("&");
  143. for (String s : params) {
  144. int index = s.indexOf("=");
  145. result.put(s.substring(0, index), s.substring(index + 1));
  146. }
  147. return result;
  148. }
  149. /**
  150. * 将URL请求参数转换成Map
  151. *
  152. * @param queryString
  153. */
  154. public static Map<String, String> getUrlParams(String queryString) {
  155. Map<String, String> result = new HashMap<>(16);
  156. if (oConvertUtils.isEmpty(queryString)) {
  157. return result;
  158. }
  159. String param = "";
  160. try {
  161. param = URLDecoder.decode(queryString, "utf-8");
  162. } catch (UnsupportedEncodingException e) {
  163. e.printStackTrace();
  164. }
  165. String[] params = param.split("&");
  166. for (String s : params) {
  167. int index = s.indexOf("=");
  168. result.put(s.substring(0, index), s.substring(index + 1));
  169. }
  170. return result;
  171. }
  172. }