123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- package org.jeecg.config.sign.util;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.net.URLDecoder;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.SortedMap;
- import java.util.TreeMap;
- import javax.servlet.http.HttpServletRequest;
- import lombok.extern.slf4j.Slf4j;
- import org.jeecg.common.util.oConvertUtils;
- import org.springframework.http.HttpMethod;
- import com.alibaba.fastjson.JSONObject;
- /**
- * http 工具类 获取请求中的参数
- *
- * @author jeecg
- * @date 20210621
- */
- @Slf4j
- public class HttpUtils {
- /**
- * 将URL的参数和body参数合并
- *
- * @author jeecg
- * @date 20210621
- * @param request
- */
- public static SortedMap<String, String> getAllParams(HttpServletRequest request) throws IOException {
- SortedMap<String, String> result = new TreeMap<>();
- // 获取URL上最后带逗号的参数变量 sys/dict/getDictItems/sys_user,realname,username
- String pathVariable = request.getRequestURI().substring(request.getRequestURI().lastIndexOf("/") + 1);
- if (pathVariable.contains(",")) {
- log.info(" pathVariable: {}",pathVariable);
- String deString = URLDecoder.decode(pathVariable, "UTF-8");
- log.info(" pathVariable decode: {}",deString);
- result.put(SignUtil.xPathVariable, deString);
- }
- // 获取URL上的参数
- Map<String, String> urlParams = getUrlParams(request);
- for (Map.Entry entry : urlParams.entrySet()) {
- result.put((String)entry.getKey(), (String)entry.getValue());
- }
- Map<String, String> allRequestParam = new HashMap<>(16);
- // get请求不需要拿body参数
- if (!HttpMethod.GET.name().equals(request.getMethod())) {
- allRequestParam = getAllRequestParam(request);
- }
- // 将URL的参数和body参数进行合并
- if (allRequestParam != null) {
- for (Map.Entry entry : allRequestParam.entrySet()) {
- result.put((String)entry.getKey(), (String)entry.getValue());
- }
- }
- return result;
- }
- /**
- * 将URL的参数和body参数合并
- *
- * @author jeecg
- * @date 20210621
- * @param queryString
- */
- public static SortedMap<String, String> getAllParams(String url, String queryString, byte[] body, String method)
- throws IOException {
- SortedMap<String, String> result = new TreeMap<>();
- // 获取URL上最后带逗号的参数变量 sys/dict/getDictItems/sys_user,realname,username
- String pathVariable = url.substring(url.lastIndexOf("/") + 1);
- if (pathVariable.contains(",")) {
- log.info(" pathVariable: {}",pathVariable);
- String deString = URLDecoder.decode(pathVariable, "UTF-8");
- log.info(" pathVariable decode: {}",deString);
- result.put(SignUtil.xPathVariable, deString);
- }
- // 获取URL上的参数
- Map<String, String> urlParams = getUrlParams(queryString);
- for (Map.Entry entry : urlParams.entrySet()) {
- result.put((String)entry.getKey(), (String)entry.getValue());
- }
- Map<String, String> allRequestParam = new HashMap<>(16);
- // get请求不需要拿body参数
- if (!HttpMethod.GET.name().equals(method)) {
- allRequestParam = getAllRequestParam(body);
- }
- // 将URL的参数和body参数进行合并
- if (allRequestParam != null) {
- for (Map.Entry entry : allRequestParam.entrySet()) {
- result.put((String)entry.getKey(), (String)entry.getValue());
- }
- }
- return result;
- }
- /**
- * 获取 Body 参数
- *
- * @date 15:04 20210621
- * @param request
- */
- public static Map<String, String> getAllRequestParam(final HttpServletRequest request) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
- String str = "";
- StringBuilder wholeStr = new StringBuilder();
- // 一行一行的读取body体里面的内容;
- while ((str = reader.readLine()) != null) {
- wholeStr.append(str);
- }
- // 转化成json对象
- return JSONObject.parseObject(wholeStr.toString(), Map.class);
- }
- /**
- * 获取 Body 参数
- *
- * @date 15:04 20210621
- * @param body
- */
- public static Map<String, String> getAllRequestParam(final byte[] body) throws IOException {
- if(body==null){
- return null;
- }
- String wholeStr = new String(body);
- // 转化成json对象
- return JSONObject.parseObject(wholeStr.toString(), Map.class);
- }
- /**
- * 将URL请求参数转换成Map
- *
- * @param request
- */
- public static Map<String, String> getUrlParams(HttpServletRequest request) {
- Map<String, String> result = new HashMap<>(16);
- if (oConvertUtils.isEmpty(request.getQueryString())) {
- return result;
- }
- String param = "";
- try {
- param = URLDecoder.decode(request.getQueryString(), "utf-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- String[] params = param.split("&");
- for (String s : params) {
- int index = s.indexOf("=");
- result.put(s.substring(0, index), s.substring(index + 1));
- }
- return result;
- }
- /**
- * 将URL请求参数转换成Map
- *
- * @param queryString
- */
- public static Map<String, String> getUrlParams(String queryString) {
- Map<String, String> result = new HashMap<>(16);
- if (oConvertUtils.isEmpty(queryString)) {
- return result;
- }
- String param = "";
- try {
- param = URLDecoder.decode(queryString, "utf-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- String[] params = param.split("&");
- for (String s : params) {
- int index = s.indexOf("=");
- result.put(s.substring(0, index), s.substring(index + 1));
- }
- return result;
- }
- }
|