mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Trace IDs of sessions created downstream of trace filter
Closes gh-11717
This commit is contained in:
parent
7ae39eaebf
commit
9da6ddb253
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2012-2017 the original author or authors.
|
* Copyright 2012-2018 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -112,14 +112,21 @@ public class WebRequestTraceFilter extends OncePerRequestFilter implements Order
|
|||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
addTimeTaken(trace, startTime);
|
addTimeTaken(trace, startTime);
|
||||||
|
addSessionIdIfNecessary(request, trace);
|
||||||
enhanceTrace(trace, status == response.getStatus() ? response
|
enhanceTrace(trace, status == response.getStatus() ? response
|
||||||
: new CustomStatusResponseWrapper(response, status));
|
: new CustomStatusResponseWrapper(response, status));
|
||||||
this.repository.add(trace);
|
this.repository.add(trace);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Map<String, Object> getTrace(HttpServletRequest request) {
|
private void addSessionIdIfNecessary(HttpServletRequest request,
|
||||||
|
Map<String, Object> trace) {
|
||||||
HttpSession session = request.getSession(false);
|
HttpSession session = request.getSession(false);
|
||||||
|
add(trace, Include.SESSION_ID, "sessionId",
|
||||||
|
(session == null ? null : session.getId()));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Map<String, Object> getTrace(HttpServletRequest request) {
|
||||||
Throwable exception = (Throwable) request
|
Throwable exception = (Throwable) request
|
||||||
.getAttribute("javax.servlet.error.exception");
|
.getAttribute("javax.servlet.error.exception");
|
||||||
Principal userPrincipal = request.getUserPrincipal();
|
Principal userPrincipal = request.getUserPrincipal();
|
||||||
@ -143,8 +150,6 @@ public class WebRequestTraceFilter extends OncePerRequestFilter implements Order
|
|||||||
add(trace, Include.QUERY_STRING, "query", request.getQueryString());
|
add(trace, Include.QUERY_STRING, "query", request.getQueryString());
|
||||||
add(trace, Include.AUTH_TYPE, "authType", request.getAuthType());
|
add(trace, Include.AUTH_TYPE, "authType", request.getAuthType());
|
||||||
add(trace, Include.REMOTE_ADDRESS, "remoteAddress", request.getRemoteAddr());
|
add(trace, Include.REMOTE_ADDRESS, "remoteAddress", request.getRemoteAddr());
|
||||||
add(trace, Include.SESSION_ID, "sessionId",
|
|
||||||
(session == null ? null : session.getId()));
|
|
||||||
add(trace, Include.REMOTE_USER, "remoteUser", request.getRemoteUser());
|
add(trace, Include.REMOTE_USER, "remoteUser", request.getRemoteUser());
|
||||||
if (isIncluded(Include.ERRORS) && exception != null
|
if (isIncluded(Include.ERRORS) && exception != null
|
||||||
&& this.errorAttributes != null) {
|
&& this.errorAttributes != null) {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2012-2017 the original author or authors.
|
* Copyright 2012-2018 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package org.springframework.boot.actuate.trace;
|
package org.springframework.boot.actuate.trace;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.security.Principal;
|
import java.security.Principal;
|
||||||
@ -28,6 +27,9 @@ import javax.servlet.FilterChain;
|
|||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.ServletRequest;
|
import javax.servlet.ServletRequest;
|
||||||
import javax.servlet.ServletResponse;
|
import javax.servlet.ServletResponse;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -105,19 +107,16 @@ public class WebRequestTraceFilterTests {
|
|||||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||||
response.addHeader("Content-Type", "application/json");
|
response.addHeader("Content-Type", "application/json");
|
||||||
response.addHeader("Set-Cookie", "a=b");
|
response.addHeader("Set-Cookie", "a=b");
|
||||||
this.filter.doFilterInternal(request, response, new FilterChain() {
|
this.filter.doFilterInternal(request, response,
|
||||||
|
new MockFilterChain(new HttpServlet() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doFilter(ServletRequest request, ServletResponse response)
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
|
||||||
throws IOException, ServletException {
|
throws ServletException, IOException {
|
||||||
BufferedReader bufferedReader = request.getReader();
|
req.getSession(true);
|
||||||
while (bufferedReader.readLine() != null) {
|
}
|
||||||
// read the contents as normal (forces cache to fill up)
|
|
||||||
}
|
|
||||||
response.getWriter().println("Goodbye, World!");
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
}));
|
||||||
assertThat(this.repository.findAll()).hasSize(1);
|
assertThat(this.repository.findAll()).hasSize(1);
|
||||||
Map<String, Object> trace = this.repository.findAll().iterator().next().getInfo();
|
Map<String, Object> trace = this.repository.findAll().iterator().next().getInfo();
|
||||||
Map<String, Object> map = (Map<String, Object>) trace.get("headers");
|
Map<String, Object> map = (Map<String, Object>) trace.get("headers");
|
||||||
@ -136,6 +135,7 @@ public class WebRequestTraceFilterTests {
|
|||||||
assertThat(trace.get("authType")).isEqualTo("authType");
|
assertThat(trace.get("authType")).isEqualTo("authType");
|
||||||
assertThat(map.get("request").toString())
|
assertThat(map.get("request").toString())
|
||||||
.isEqualTo("{Accept=application/json, Cookie=testCookie=testValue;}");
|
.isEqualTo("{Accept=application/json, Cookie=testCookie=testValue;}");
|
||||||
|
assertThat(trace).containsKey("sessionId");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user