记录Http请求和响应正文

时间:2017-07-10 16:11:17

标签: grails groovy

我试图找出如何记录我的应用程序中的每个请求及其相关响应。目前我已经创建了一个Grails拦截器来捕获请求和响应。我一直遇到的问题是,如果我在请求体到达控制器之前将其记录下来,那么我的记录器和请求体就消耗了资源。所以我尝试在控制器处理后请求记录请求体。问题是输入流已关闭,我无法再访问它。

这是我的拦截器:

import grails.plugin.springsecurity.SpringSecurityService

import javax.servlet.ServletRequest

class HttpLoggingInterceptor {

SpringSecurityService springSecurityService
HttpLoggingService httpLoggingService


HttpLoggingInterceptor() {
    match(controller: "*")
}

 boolean before() {
    httpLoggingService.logRequest(request, response, springSecurityService.principal)
    true
  } 
}

这是httpLoggingService:

import grails.transaction.Transactional
import org.apache.commons.io.IOUtils
import org.springframework.security.core.userdetails.User

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletRequestWrapper
import javax.servlet.http.HttpServletResponse
import java.nio.charset.StandardCharsets

@Transactional
class HttpLoggingService {

static final String CR = "\r\n"

def logRequest(HttpServletRequestWrapper request, HttpServletResponse response, User user) {


    log.debug("REQUEST:${CR}" +
            "Request  URL: ${request?.method} ${request?.requestURL}${CR}" +
            "Request  QueryString: ${request?.queryString}${CR}" +
            "Request Body : ${getRequestBody(request)}" +
            "Request  Client IP: ${request?.remoteAddr}${CR}" +
            "Request  User: ${getUserInfo(user)}${CR}" +
            "Request  Headers: ${getRequestHeaders(request)}${CR}" +
            "Request  Query String Parameters:         ${getRequestParameters(request)}${CR}" +
        "Response Status: ${response?.status}${CR}" +
        "Respones Body: ${getResponseBody(response)}" +
        "Response Properties: ${response?.properties}${CR}" +
        "Response Headers: ${getResponseHeaders(response)}${CR}" +
        CR
    )
}


private String getUserInfo(User user) {
    String userInfo = null
    if (user instanceof CipUserDetails) {
        userInfo = "${user?.username} [${user?.fullName}] - ${user?.authorities[0]?.role}"
    }
    userInfo
}

private String getRequestBody(HttpServletRequestWrapper request) {
    String requestBody = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8)
    requestBody
}

private String getRequestParameters(HttpServletRequest request) {
    Map parameterMap = request?.parameterMap
    String parameterString = ""
    for (String name : request?.parameterNames) {
        parameterString += "${CR}    ${name}=${parameterMap?.get(name)}"
    }
    parameterString
}

private String getRequestHeaders(HttpServletRequest request) {
    String parameterString = ""
    for (String name : request.headerNames) {
        parameterString += "${CR}    ${name}=${request?.getHeader(name)}"
    }
    parameterString
}

private String getResponseHeaders(HttpServletResponse response) {
    String parameterString = ""
    for (String name : response?.headerNames) {
        parameterString += "${CR}    ${name}=${response?.getHeader(name)}"
    }
     parameterString
 }
}

有人可以帮我弄清楚如何做到这一点吗?

0 个答案:

没有答案