mirror of
https://github.com/OpenBankProject/OBP-Hydra-Identity-Provider.git
synced 2026-02-06 10:48:13 +00:00
feature/Add logging at every request and response
This commit is contained in:
parent
475f48cfbc
commit
ed702ee124
@ -8,12 +8,15 @@ import com.nimbusds.jose.jwk.RSAKey;
|
||||
import com.openbankproject.JwsUtil;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.*;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.entity.BufferedHttpEntity;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@ -43,6 +46,7 @@ import java.util.UUID;
|
||||
|
||||
@Configuration
|
||||
public class RestTemplateConfig {
|
||||
private static final Logger logger = LoggerFactory.getLogger(RestTemplateConfig.class);
|
||||
|
||||
@Value("${mtls.keyStore.path}")
|
||||
private Resource keyStoreResource;
|
||||
@ -111,27 +115,34 @@ public class RestTemplateConfig {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
private void responseIntercept(org.apache.http.HttpResponse response, HttpContext httpContext) {
|
||||
|
||||
private void traceResponse(HttpResponse response, String body) throws IOException {
|
||||
logger.info("=========================== response begin ================================================");
|
||||
logger.info("=== Status Line : {}", response.getStatusLine());
|
||||
logger.info("=== Headers : {}", StringUtils.join(response.getAllHeaders(), "; "));
|
||||
logger.info("=== Response body: {}", body);
|
||||
logger.info("========================== response end ================================================");
|
||||
}
|
||||
private void responseIntercept(org.apache.http.HttpResponse response, HttpContext httpContext) throws IOException {
|
||||
HttpRequest req = (HttpRequest)httpContext.getAttribute("http.request");
|
||||
String uri = req.getRequestLine().getUri();
|
||||
if(forceJws(uri)) {
|
||||
// Transform non-repeatable entity => repeatable entity.
|
||||
makeInputStreamOfEntityRepeatable(response);
|
||||
// Transform non-repeatable entity => repeatable entity.
|
||||
makeInputStreamOfEntityRepeatable(response);
|
||||
|
||||
// Get HTTP body from the response
|
||||
String httpBody = "";
|
||||
try {
|
||||
if(response.getEntity() != null && response.getEntity().getContent() != null) {
|
||||
httpBody = getHttpResponseBody(response.getEntity().getContent()).toString();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(forceJws(uri)) {
|
||||
String xJwsSignature = getOrEmptyValue("x-jws-signature", response);
|
||||
String digest = getOrEmptyValue("digest", response);
|
||||
String verb = req.getRequestLine().getMethod().toLowerCase();
|
||||
|
||||
// Get HTTP body from the response
|
||||
String httpBody = "";
|
||||
try {
|
||||
if(response.getEntity() != null && response.getEntity().getContent() != null) {
|
||||
httpBody = getHttpResponseBody(response.getEntity().getContent()).toString();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
String jwsProtectedHeaderAsString = "";
|
||||
String rebuiltDetachedPayload = "";
|
||||
String x5c = "";
|
||||
@ -169,6 +180,7 @@ public class RestTemplateConfig {
|
||||
response.setStatusLine(version, 400, "The signed response cannot be verified.");
|
||||
}
|
||||
}
|
||||
traceResponse(response, httpBody);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -187,15 +199,23 @@ public class RestTemplateConfig {
|
||||
}
|
||||
}
|
||||
}
|
||||
private void requestIntercept(org.apache.http.HttpRequest request, HttpContext httpContext) {
|
||||
|
||||
private void traceRequest(HttpRequest request, String body) throws IOException {
|
||||
logger.info("=========================== request begin ================================================");
|
||||
logger.info("=== Request Line : {}", request.getRequestLine());
|
||||
logger.info("=== Headers : {}", StringUtils.join(request.getAllHeaders(), "; "));
|
||||
logger.info("=== Request body: {}", body);
|
||||
logger.info("============================= request end ================================================");
|
||||
}
|
||||
private void requestIntercept(org.apache.http.HttpRequest request, HttpContext httpContext) throws IOException {
|
||||
Header[] headers = request.getHeaders(HttpHeaders.CONTENT_TYPE);
|
||||
if(ArrayUtils.isEmpty(headers)) {
|
||||
request.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
|
||||
}
|
||||
String url = request.getRequestLine().getUri();
|
||||
String httpBody = getHttpBody(request).toString();
|
||||
if(forceJws(url)) {
|
||||
String httpMethod = request.getRequestLine().getMethod().toLowerCase();
|
||||
String httpBody = getHttpBody(request).toString();
|
||||
InetAddress ip = getInetAddress();
|
||||
JwsUtil jwsUtil = new JwsUtil();
|
||||
Map<String, String> requestHeaders = new HashMap<>();
|
||||
@ -212,6 +232,7 @@ public class RestTemplateConfig {
|
||||
request.setHeader("PSU-GEO-Location", "GEO:52.506931,13.144558");
|
||||
request.setHeader("X-Request-ID", UUID.randomUUID().toString());
|
||||
}
|
||||
traceRequest(request, httpBody);
|
||||
}
|
||||
|
||||
private boolean forceJws(String url) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user