springcloud zuul過濾器 修改請求參數

HttpServletRequest request  可以通過  request.getParameter("") 得到請求頭參數

卻不能修改 請求的參數  ,但zuul 的過濾器可以

//zuul 過濾器中:
RequestContext ctx= RequestContext.getCurrentContext();//獲取當前的 上下文對象
HttpServletRequest request = ctx.getRequest();//獲取 request

//獲取請求頭參數   post請求體的拿不了
request.getParameterMap();    //必須加這句 不然ctx.getRequestQueryParams()失敗
Map<String, List<String>> requestQueryParams = ctx.getRequestQueryParams();//參數鍵值對map
System.out.println(requestQueryParams);

//添加 修改請求頭參數   請求體的改不了
//如果一開始請求頭裏沒參數 那麼requestQueryParams爲空
if (requestQueryParams==null) requestQueryParams=new HashMap<>();
List<String> arrayList = new ArrayList<>();
arrayList.add("100");    //添加values
requestQueryParams.put("userId", arrayList);     //把參數鍵值對放入map

currentContext.setRequestQueryParams(requestQueryParams);//重新設置請求頭所含有的參數

參考鏈接:https://blog.csdn.net/qq_31122833/article/details/83022904

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章