亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java如何替換RequestBody和RequestParam參數(shù)的屬性

 更新時間:2023年10月25日 08:53:30   作者:初夏的陽光丶  
近期由于接手的老項目中存在所有接口中新增一個加密串來給接口做一個加密效果,所以就研究了一下Http請求鏈路,發(fā)現(xiàn)可以通過?javax.servlet.Filter去實現(xiàn),這篇文章主要介紹了Java替換RequestBody和RequestParam參數(shù)的屬性,需要的朋友可以參考下

本文主要講解在Java環(huán)境中如何替換RequestBody和RequestParam參數(shù)中的屬性

背景

近期由于接手的老項目中存在所有接口中新增一個加密串來給接口做一個加密效果(項目歷史原因,不方便上Jwt授權(quán)這套),所以就研究了一下Http請求鏈路,發(fā)現(xiàn)可以通過 javax.servlet.Filter去實現(xiàn)

替換RequestParam參數(shù)

首先通過繼續(xù)HttpServletRequestWrapper來達(dá)到獲取和替換RequestParam中的參數(shù)信息,接下來我們通過javax.servlet.Filter去獲取ServletRequest中參數(shù)的信息,并且定義對應(yīng)規(guī)則,來實現(xiàn)替換參數(shù)
代碼示例:

package com.simplemessage.cloudpayservice.infrastructure.config.http;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
/**
 * @CreateAt: 2023/10/24 12:13
 * @ModifyAt: 2023/10/24 12:13
 * @Version 1.0
 */
public class MyRequestWrapper  extends HttpServletRequestWrapper {
    private Map params = new HashMap<>();
    public MyRequestWrapper(HttpServletRequest request, Map newParams) {
        super(request);
        if(request.getParameterMap() != null){
            this.params.putAll(request.getParameterMap());
        }
        if(newParams != null){
            this.params.putAll(newParams);
        }
    }
    /**
     * 獲取參數(shù)
     * @return
     */
    @Override
    public Map getParameterMap() {
        return params;
    }
    @Override
    public Enumeration getParameterNames() {
        Vector l = new Vector(params.keySet());
        return l.elements();
    }
    @Override
    public String[] getParameterValues(String name) {
        Object v = params.get(name);
        if (v == null) {
            return null;
        } else if (v instanceof String[]) {
            return (String[]) v;
        } else if (v instanceof String) {
            return new String[]{(String) v};
        } else {
            return new String[]{v.toString()};
        }
    }
    /**
     * 根據(jù)參數(shù)的key獲取參數(shù)
     * @param name
     * @return
     */
    @Override
    public String getParameter(String name) {
        Object v = params.get(name);
        if (v == null) {
            return null;
        } else if (v instanceof String[]) {
            String[] strArr = (String[]) v;
            if (strArr.length > 0) {
                return strArr[0];
            } else {
                return null;
            }
        } else if (v instanceof String) {
            return (String) v;
        } else {
            return v.toString();
        }
    }
}
package com.simplemessage.cloudpayservice.infrastructure.config.http;

import com.fasterxml.jackson.core.io.JsonEOFException;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.RequestFacade;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @CreateAt: 2023/10/24 12:16
 * @ModifyAt: 2023/10/24 12:16
 * @Version 1.0
 */
@Slf4j
@Component
@WebFilter(filterName = "replaceGetRequestFilter", urlPatterns = {"/*"})
public class ReplaceGetRequestFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        long start = System.currentTimeMillis();
        //獲取HttpServletRequest對象
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        //判斷當(dāng)前是否為Get請求
        if ("GET".equalsIgnoreCase(httpServletRequest.getMethod())) {
        	// 獲取參數(shù)信息
            String param= request.getParameter("param");
            //判斷參數(shù)是否為空,為空則放行
            if (StringUtils.isEmpty(param)) {
                chain.doFilter(request, response);
                return;
            } else {
                Map<String, String[]> newParameterMap = new HashMap<>();
                // 替換參數(shù)(自定義規(guī)則)
                String newParama="test";
                newParameterMap.put("param", newParama);
                // 實現(xiàn)參數(shù)替換
                MyRequestWrapper myRequestWrapper = new MyRequestWrapper(httpServletRequest, newParameterMap);
                chain.doFilter(myRequestWrapper, response);
            }

        } else {
            try {
                chain.doFilter(request, response);
            } catch (HttpMessageNotReadableException httpMessageNotReadableException) {
                log.error(((RequestFacade) request).getRequestURI() + ", " + httpMessageNotReadableException.getMessage());
            } catch (JsonEOFException jsonEOFException) {
                log.error(((RequestFacade) request).getRequestURI() + ", " + jsonEOFException.getMessage());
            }
        }
        long end = System.currentTimeMillis();
        log.info("{} 接口耗時:{} ms", httpServletRequest.getRequestURI(), (end - start));
    }

    @Override
    public void destroy() {
    }
}

替換RequestBody參數(shù)

主要思路就是通過獲取Post中請求的輸入流信息,解析輸入流信息,按照對應(yīng)的規(guī)則進(jìn)行替換參數(shù)信息,最后將對應(yīng)的流信息包裝進(jìn)行返回
代碼示例:

package com.simplemessage.cloudpayservice.infrastructure.config.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.List;

/**
 * @version 1.0
 * @createAt: 2023/10/24 12:23:23
 * @modifyAt: 2023/10/24 12:23:23
 */
@RestControllerAdvice
@Slf4j
public class DecryptRequestBodyHandler implements RequestBodyAdvice {
    
    /**
     * 該方法用于判斷當(dāng)前請求,是否要執(zhí)行beforeBodyRead方法
     * methodParameter方法的參數(shù)對象
     * type方法的參數(shù)類型
     * aClass 將會使用到的Http消息轉(zhuǎn)換器類類型
     * 注意:此判斷方法,會在beforeBodyRead 和 afterBodyRead方法前都觸發(fā)一次。
     * @return 返回true則會執(zhí)行beforeBodyRead
     */
    @Override
    public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
        return true;
    }

    /**
     * 在Http消息轉(zhuǎn)換器執(zhí)轉(zhuǎn)換,之前執(zhí)行
     * inputMessage 客戶端請求的信息
     * parameter 參數(shù)信息
     * targetType 參數(shù)類型
     * converterType Http消息轉(zhuǎn)換器類類型
     *
     * @return 返回 一個自定義的HttpInputMessage
     */
    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
        // 如果body是空內(nèi)容直接返回原來的請求
        if (inputMessage.getBody().available() <= 0) {
            return inputMessage;
        }
        // 請求中的header信息
        HttpHeaders headers = inputMessage.getHeaders();     
      
        // 將輸入流讀出來,注意 body 里面的流只能讀一次
        ByteArrayOutputStream requestBodyDataByte = new ByteArrayOutputStream();
        try {
        	//復(fù)制流信息
            IOUtils.copy(inputMessage.getBody(), requestBodyDataByte);
        } catch (Exception e) {
            log.error("參數(shù)流拷貝失敗: ", e.toString());
            return inputMessage;
        }
        ByteArrayOutputStream requestBodyDataByteNew = null;
        try {
            JSONObject body = JSON.parseObject(requestBodyDataByte.toByteArray(), JSONObject.class);
            if (ObjectUtils.isEmpty(body)) {
                return inputMessage;
            }
            //自定義規(guī)則西悉尼
            if (body.containsKey("param")) {
                String custId = body.getString("param"); 
                String newParam="";              
                body.put("custId", newParam);
                requestBodyDataByteNew = new ByteArrayOutputStream();
                //拷貝流信息
                IOUtils.copy(new ByteArrayInputStream(body.toJSONString().getBytes()), requestBodyDataByteNew);
            }
        } catch (Throwable e) {
            log.error("流轉(zhuǎn)換異常 ", e.toString());
        }
        // 如果上述發(fā)生異常,仍然使用原來的請求內(nèi)容
        requestBodyDataByte = requestBodyDataByteNew != null ? requestBodyDataByteNew : requestBodyDataByte;
        InputStream rawInputStream = new ByteArrayInputStream(requestBodyDataByte.toByteArray());
        inputMessage.getHeaders().set(HttpHeaders.CONTENT_LENGTH, String.valueOf(rawInputStream.available()));
        return new HttpInputMessage() {
            @Override
            public HttpHeaders getHeaders() {
                return inputMessage.getHeaders();
            }

            @Override
            public InputStream getBody() throws IOException {
                return rawInputStream;
            }
        };
    }

    /**
     * 在Http消息轉(zhuǎn)換器執(zhí)轉(zhuǎn)換,之后執(zhí)行
     * body 轉(zhuǎn)換后的對象
     * inputMessage 客戶端的請求數(shù)據(jù)
     * parameter handler方法的參數(shù)類型
     * targetType handler方法的參數(shù)類型
     * converterType 使用的Http消息轉(zhuǎn)換器類類型
     *
     * @return 返回一個新的對象
     */
    @Override
    public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return body;
    }

    /**
     * 參數(shù)與afterBodyRead相同,不過這個方法body為空的情況
     */
    @Override
    public Object handleEmptyBody(@Nullable Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return body;
    }
}

到此這篇關(guān)于Java替換RequestBody和RequestParam參數(shù)的屬性的文章就介紹到這了,更多相關(guān)Java替換RequestBody和RequestParam參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot實現(xiàn)MapperScan添加動態(tài)配置(占位符)

    SpringBoot實現(xiàn)MapperScan添加動態(tài)配置(占位符)

    這篇文章主要介紹了SpringBoot實現(xiàn)MapperScan添加動態(tài)配置(占位符),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。
    2022-01-01
  • Java設(shè)計模式之靜態(tài)工廠模式詳解

    Java設(shè)計模式之靜態(tài)工廠模式詳解

    這篇文章主要介紹了Java設(shè)計模式之靜態(tài)工廠模式,簡單說明了靜態(tài)工廠模式的概念、原理、實現(xiàn)與使用方法,需要的朋友可以參考下
    2017-09-09
  • SpringEvents與異步事件驅(qū)動案例詳解

    SpringEvents與異步事件驅(qū)動案例詳解

    本文深入探討了SpringBoot中的事件驅(qū)動架構(gòu),特別是通過Spring事件機制實現(xiàn)組件解耦和系統(tǒng)擴展性增強,介紹了事件的發(fā)布者、事件本身、事件監(jiān)聽器和事件處理器的概念,感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • MyBatis-Plus攔截器對敏感數(shù)據(jù)實現(xiàn)加密

    MyBatis-Plus攔截器對敏感數(shù)據(jù)實現(xiàn)加密

    做課程項目petstore時遇到需要加密屬性的問題,而MyBatis-Plus為開發(fā)者提供了攔截器的相關(guān)接口,本文主要介紹通過MyBatis-Plus的攔截器接口自定義一個攔截器類實現(xiàn)敏感數(shù)據(jù)如用戶密碼的加密功能,感興趣的可以了解一下
    2021-11-11
  • RocketMQ特性Broker存儲事務(wù)消息實現(xiàn)

    RocketMQ特性Broker存儲事務(wù)消息實現(xiàn)

    這篇文章主要為大家介紹了RocketMQ特性Broker存儲事務(wù)消息實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • 淺談Thread.sleep(0)到底有什么用

    淺談Thread.sleep(0)到底有什么用

    為什么要用sleep,主要是為了暫停當(dāng)前線程,把cpu片段讓出給其他線程,減緩當(dāng)前線程的執(zhí)行,本文主要介紹了Thread.sleep(0)到底有什么用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • spring和quartz整合,并簡單調(diào)用(實例講解)

    spring和quartz整合,并簡單調(diào)用(實例講解)

    下面小編就為大家?guī)硪黄猻pring和quartz整合,并簡單調(diào)用(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • java之static關(guān)鍵字用法實例解析

    java之static關(guān)鍵字用法實例解析

    這篇文章主要介紹了java之static關(guān)鍵字用法實例解析,包括了static關(guān)鍵字的原理講解及用法分析,并附帶了實例說明,需要的朋友可以參考下
    2014-09-09
  • Java中十進(jìn)制和十六進(jìn)制的相互轉(zhuǎn)換方法

    Java中十進(jìn)制和十六進(jìn)制的相互轉(zhuǎn)換方法

    下面小編就為大家?guī)硪黄狫ava中十進(jìn)制和十六進(jìn)制的相互轉(zhuǎn)換方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-08-08
  • Spring條件注解用法案例分析

    Spring條件注解用法案例分析

    這篇文章主要介紹了Spring條件注解用法,結(jié)合具體實例形式分析了Spring條件注解相關(guān)原理、使用方法及操作注意事項,需要的朋友可以參考下
    2019-11-11

最新評論