| | |
| | | package com.ruoyi.goods.fliter; |
| | | |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.ruoyi.common.core.utils.StringUtils; |
| | | import lombok.extern.log4j.Log4j2; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.servlet.*; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | 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; |
| | | |
| | | @Component |
| | | @Log4j2 |
| | | public class ParamsAuthFilter implements Filter { |
| | | |
| | | // 令牌自定义标识 |
| | | //@Value("${token.header}") |
| | | private String header = "Authorization"; |
| | | |
| | | |
| | | @Override |
| | | public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { |
| | | HttpServletRequest request = (HttpServletRequest) servletRequest; |
| | | HttpServletResponse response = (HttpServletResponse) servletResponse; |
| | | String requestUrl = request.getRequestURI(); |
| | | String requestUrlUpperCase = requestUrl.toUpperCase(); |
| | | if(requestUrlUpperCase.contains("EXPORT") |
| | | ||requestUrlUpperCase.contains("IMPORTTEMPLATE") |
| | | ){ |
| | | filterChain.doFilter(request, response); |
| | | return; |
| | | } |
| | | RepeatedlyRequestWrapper wrapper = new RepeatedlyRequestWrapper(request, response); |
| | | getRequestParams(wrapper); |
| | | |
| | | ResponseWrapper responseWrapper = new ResponseWrapper(response); |
| | | filterChain.doFilter(wrapper, responseWrapper); |
| | | |
| | | // 返回的结果 |
| | | String content = responseWrapper.getTextContent(); |
| | | log.info("-------------------- Response Data ----------------------"); |
| | | log.info(requestUrl + " -> " + content); |
| | | |
| | | response.getOutputStream().write(content.getBytes()); |
| | | } |
| | | |
| | | |
| | | private void getRequestParams(HttpServletRequest request){ |
| | | Map<String, String> params = getUrlParams(request); |
| | | try { |
| | | String requestUrl = request.getRequestURI(); |
| | | Map<String, String> urlParams = getUrlParams(request); |
| | | Map<String, String> bodyParams = getBodyParams(request); |
| | | |
| | | params.putAll(urlParams); |
| | | params.putAll(bodyParams); |
| | | log.info("-------------------- request header ----------------------"); |
| | | log.info(header + " -> " + request.getHeader(header)); |
| | | log.info("-------------------- request params ----------------------"); |
| | | log.info(requestUrl + " -> " + JSONObject.toJSONString(params)); |
| | | |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | private Map<String, String> getBodyParams(HttpServletRequest request) throws IOException { |
| | | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(request.getInputStream())); |
| | | StringBuilder stringBuilder = new StringBuilder(); |
| | | String buffer = ""; |
| | | while((buffer = bufferedReader.readLine()) != null){ |
| | | stringBuilder.append(buffer); |
| | | } |
| | | String requestBodyStr = stringBuilder.toString(); |
| | | if(StringUtils.isNotBlank(requestBodyStr)){ |
| | | Map<String, String> result = new HashMap<>(); |
| | | try { |
| | | result = JSONObject.parseObject(stringBuilder.toString(), Map.class); |
| | | } catch (Exception e) { |
| | | log.info("requestBodyStr-error -> " + requestBodyStr); |
| | | e.printStackTrace(); |
| | | } |
| | | return result; |
| | | } |
| | | return new HashMap<>(); |
| | | } |
| | | |
| | | private Map<String, String> getUrlParams(HttpServletRequest request){ |
| | | String queryParams = ""; |
| | | try { |
| | | queryParams = request.getQueryString(); |
| | | if(StringUtils.isNotBlank(queryParams)) { |
| | | queryParams = URLDecoder.decode(request.getQueryString(), "utf-8"); |
| | | } |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | Map<String, String> result = new HashMap<>(); |
| | | if(StringUtils.isNotBlank(queryParams)) { |
| | | String[] spilt = queryParams.split("&"); |
| | | for (String s : spilt) { |
| | | int index = s.indexOf("="); |
| | | result.put(s.substring(0, index), s.substring(index + 1)); |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | } |
| | | //package com.ruoyi.goods.fliter; |
| | | // |
| | | // |
| | | //import com.alibaba.fastjson2.JSONObject; |
| | | //import com.ruoyi.common.core.utils.StringUtils; |
| | | //import lombok.extern.log4j.Log4j2; |
| | | //import org.springframework.stereotype.Component; |
| | | // |
| | | //import javax.servlet.*; |
| | | //import javax.servlet.http.HttpServletRequest; |
| | | //import javax.servlet.http.HttpServletResponse; |
| | | //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; |
| | | // |
| | | //@Component |
| | | //@Log4j2 |
| | | //public class ParamsAuthFilter implements Filter { |
| | | // |
| | | // // 令牌自定义标识 |
| | | // //@Value("${token.header}") |
| | | // private String header = "Authorization"; |
| | | // |
| | | // |
| | | // @Override |
| | | // public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { |
| | | // HttpServletRequest request = (HttpServletRequest) servletRequest; |
| | | // HttpServletResponse response = (HttpServletResponse) servletResponse; |
| | | // String requestUrl = request.getRequestURI(); |
| | | // String requestUrlUpperCase = requestUrl.toUpperCase(); |
| | | // if(requestUrlUpperCase.contains("EXPORT") |
| | | // ||requestUrlUpperCase.contains("IMPORTTEMPLATE") |
| | | // ){ |
| | | // filterChain.doFilter(request, response); |
| | | // return; |
| | | // } |
| | | // RepeatedlyRequestWrapper wrapper = new RepeatedlyRequestWrapper(request, response); |
| | | // getRequestParams(wrapper); |
| | | // |
| | | // ResponseWrapper responseWrapper = new ResponseWrapper(response); |
| | | // filterChain.doFilter(wrapper, responseWrapper); |
| | | // |
| | | // // 返回的结果 |
| | | // String content = responseWrapper.getTextContent(); |
| | | // log.info("-------------------- Response Data ----------------------"); |
| | | // log.info(requestUrl + " -> " + content); |
| | | // |
| | | // response.getOutputStream().write(content.getBytes()); |
| | | // } |
| | | // |
| | | // |
| | | // private void getRequestParams(HttpServletRequest request){ |
| | | // Map<String, String> params = getUrlParams(request); |
| | | // try { |
| | | // String requestUrl = request.getRequestURI(); |
| | | // Map<String, String> urlParams = getUrlParams(request); |
| | | // Map<String, String> bodyParams = getBodyParams(request); |
| | | // |
| | | // params.putAll(urlParams); |
| | | // params.putAll(bodyParams); |
| | | // log.info("-------------------- request header ----------------------"); |
| | | // log.info(header + " -> " + request.getHeader(header)); |
| | | // log.info("-------------------- request params ----------------------"); |
| | | // log.info(requestUrl + " -> " + JSONObject.toJSONString(params)); |
| | | // |
| | | // }catch (Exception e){ |
| | | // e.printStackTrace(); |
| | | // } |
| | | // } |
| | | // |
| | | // private Map<String, String> getBodyParams(HttpServletRequest request) throws IOException { |
| | | // BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(request.getInputStream())); |
| | | // StringBuilder stringBuilder = new StringBuilder(); |
| | | // String buffer = ""; |
| | | // while((buffer = bufferedReader.readLine()) != null){ |
| | | // stringBuilder.append(buffer); |
| | | // } |
| | | // String requestBodyStr = stringBuilder.toString(); |
| | | // if(StringUtils.isNotBlank(requestBodyStr)){ |
| | | // Map<String, String> result = new HashMap<>(); |
| | | // try { |
| | | // result = JSONObject.parseObject(stringBuilder.toString(), Map.class); |
| | | // } catch (Exception e) { |
| | | // log.info("requestBodyStr-error -> " + requestBodyStr); |
| | | // e.printStackTrace(); |
| | | // } |
| | | // return result; |
| | | // } |
| | | // return new HashMap<>(); |
| | | // } |
| | | // |
| | | // private Map<String, String> getUrlParams(HttpServletRequest request){ |
| | | // String queryParams = ""; |
| | | // try { |
| | | // queryParams = request.getQueryString(); |
| | | // if(StringUtils.isNotBlank(queryParams)) { |
| | | // queryParams = URLDecoder.decode(request.getQueryString(), "utf-8"); |
| | | // } |
| | | // } catch (UnsupportedEncodingException e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // Map<String, String> result = new HashMap<>(); |
| | | // if(StringUtils.isNotBlank(queryParams)) { |
| | | // String[] spilt = queryParams.split("&"); |
| | | // for (String s : spilt) { |
| | | // int index = s.indexOf("="); |
| | | // result.put(s.substring(0, index), s.substring(index + 1)); |
| | | // } |
| | | // } |
| | | // return result; |
| | | // } |
| | | //} |