1. 程式人生 > >使用攔截器獲取請求引數資訊並寫入日誌

使用攔截器獲取請求引數資訊並寫入日誌

  1. 前言
    使用攔截器獲取請求的引數,ip地址等等資訊,然後寫入日誌更加方便後期異常的維護。

2.程式碼例項

public class RequestParamInfoIntorceptor extends HandlerInterceptorAdapter  {

    private Logger logger = LoggerFactory.getLogger(RequestParamInfoIntorceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws
Exception { try { if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; String beanName = handlerMethod.getBean().getClass().toString();//類 String methodName = handlerMethod.getMethod().getName();//方法名稱
if(methodName.equals("error") || methodName.equals("success")) { return super.preHandle(request, response, handler); } String uri = request.getRequestURI();//請求路徑 String remoteAddr = getIpAddr(request);//ip String method = request.getMethod(); //請求方式
Map<String,String[]> pramMap = request.getParameterMap(); StringBuffer sbf = new StringBuffer(); int count = 0; String forCunt = ""; for(Map.Entry<String, String[]> entry:pramMap.entrySet()){ forCunt = "[" + count + "]" + " : " ; sbf.append( "paramName" + forCunt + entry.getKey() + " - "+ "paramValue" + forCunt + request.getParameter(entry.getKey()) + "\n"); count ++; } logger.info(" { beanName : " + beanName + " | " + "methodName : " + methodName + " | " + "uri : " + uri + " | " + "remoteAddr : " + remoteAddr + " | " + "requestMethod : " + method + "\n" + "param : " + sbf + "}"); } } catch (Exception e) { //出錯 logger.error(LogUtil.setLogError(e, "使用者操作日誌記錄異常" , "RequestParamInfoIntorceptor")); } return super.preHandle(request, response, handler); } //獲取客戶端IP private String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return ip; } }

相關推薦

使用攔截獲取請求引數資訊寫入日誌

前言 使用攔截器獲取請求的引數,ip地址等等資訊,然後寫入日誌更加方便後期異常的維護。 2.程式碼例項 public class RequestParamInfoIntorceptor extends HandlerInterceptorAdapter

Spring RestTemplate 利用攔截列印請求引數和返回狀態

  最近在專案中遇到用RestTemplate請求另外一個服務介面,發現總是報400返回。或許由於對400錯誤不是很瞭解,除錯了很久。但是過了好一段時間,發現自己進展不大,由此,諮詢下了經驗豐富的人,也解決了RestTemplate請求另外服務介面的方法。    很多人都基

獲取Android崩潰crash資訊寫入日誌

Android崩潰是開發中不可避免的一件事,考慮不夠周全的程式碼、糟糕的網路環境、讓人頭疼的碎片化問題都可能導致crash,線上版本crash嚴重影響使用者體驗,所以crash的捕獲和收集對我們開發人員很重要。 〇、Exception的分類及捕獲 Java的異常

使用 aop攔截 springMVC的controller獲取請求引數及返回結果

有人說使用aop攔截不到springMVC的controller,一般出現此種情況大多是由於配置錯誤造成,不廢話直接進入主題: 1、applicationContext.xml 配置掃描 除@controller外的bean <context:component

SpringBoot 攔截獲取http請求引數

# SpringBoot 攔截器獲取http請求引數—— 所有騷操作基礎 [TOC] ## 獲取http請求引數是一種剛需 我想有的小夥伴肯定有過獲取http請求的需要,比如想 1. 前置獲取引數,統計請求資料 2. 做服務的介面簽名校驗 3. 敏感介面監控日誌 4. 敏感介面防重複提交 等等各式各樣

Log4Net獲取異常資訊寫入Mysql資料庫

本文介紹使用Log4net實現獲取程式的錯誤資訊,並將錯誤資訊寫入Mysql的資料庫。 第一步: 引用相關的dll外掛:log4net.dll和Mysql.Data.dll 第二步:新增log4net.config檔案。 <?xml version="1.0" e

struts2使用AbstractInterceptor攔截 獲取引數以及修改引數的值

public class AppLoginInterceptor extends AbstractInterceptor{ @Override public String intercept(ActionInvocation invocation) throws Exc

axios post 請求tomcat,攔截獲取不到getInputStream流中的資料

今天做專案,前端的大佬用axios傳送post請求,而我要利用過濾器和攔截器進行許可權的校驗,校驗的內容就是上送的某個欄位的內容,所以我們需要重寫request請求(request請求只能獲取一次,如果在攔截器中獲取,那麼控制器就獲取不到相應的引數),並且在攔截器中進行校驗,

Servlet 獲取請求資訊請求引數

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 獲取請求報文頭

python使用xlrd讀取excel資料作為requests的請求引數把返回的資料寫入excel中

實現功能: 從excel中的第一列資料作為post請求的資料,資料為json格式;把post返回的結果寫入到excel的第二列資料中 每一行的資料都不一樣,可實現迴圈呼叫 # !/usr/bin/env python # -*- coding:utf-8 -*- #import xlwt

OkHttp中Interceptor攔截之公共引數請求封裝

前言 之前在面試的時候遇到這樣的一個問題,那就是如果app中所有的請求都要加入一些引數(例如 版本號、手機號、登入使用者名稱、token等。。。)那麼怎麼做才能實現,而不至於在每次請求的時候都去進行新增這些請求頭。其實這個問題,剛開始我是拒絕的(之前沒有遇到過

Java I/O---獲取文件目錄寫入到文本

override log exce blog cor param 文件 direct exc 首先獲取指定目錄下的所有文件目錄,存入List集合中,然後創建文本文件將List遍歷寫入文本中保存。 1.主程序類 1 public class Test {

spring mvc 能過攔截記錄請求數據和響應數據

style gpo exec point ret nature request 響應 處理 spring mvc 能過攔截器記錄請求數據記錄有很多種方式,主要有以下三種: 1:過濾器 2:HandlerInterceptor攔截器 3:Aspect接口控制器 但是就我個人所

Axios 攔截 取消請求

調用 span n) iso 我們 信息 mes === pan axios 最常用的功能之一,攔截器 axios.interceptors.response.use( response => { let {data: json} = response

Mybatis之攔截--獲取執行SQL實現多客戶端數據同步

gin sign factor 方便 完成後 動態代理 exc batis 包安裝 最近的一個項目是將J2EE環境打包安裝在客戶端(使用 nwjs + NSIS 制作安裝包)運行, 所有的業務操作在客戶端完成, 數據存儲在客戶端數據庫中. 服務器端數據庫匯總各客戶端的數據進

Anguar 使用interceptor攔截設定請求頭傳入jwt token

1.建立http-interceptors.ts檔案 import { Injectable } from "@angular/core"; import { HttpInterceptor, HttpRequest, HttpHandler, HttpErrorResponse

servlet學習(二)request物件獲取請求資訊

一、作用 封存了當前請求的所有請求資訊 二、使用 獲取請求頭的資訊包括: 1.請求行:請求方式 請求URL/URI 協議版本 //獲取請求行 String method=req.getMethod(); StringBuffer url=req.get

express中的路由規則及獲取請求引數方法

express中常見的路由規則 主要使用的路由規則是get和post兩種,即 var express = require('express'); var app = express(); app.get(); // get和post兩種請求方式 app.post();

ThinkPhp5獲取請求引數

獲取請求引數前,需要呼叫Request類 use think\Request 獲取單個請求引數(通用): $id = Request::instance() -> param( ‘id’ ) 獲取所有請求引數(通用): $all = Request::instance()

學習SpringMVC——如何獲取請求引數

@RequestParam @PathVariable @QueryParam @CookieValue @ModelAndView @ModelAttribute   一、spring mvc如何匹配請求路徑——“請求路徑哪家強,RequestMapping名遠揚” @R