Skip to content

Commit 562b956

Browse files
committed
add a jsonp case
1 parent 27df4d1 commit 562b956

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/main/java/org/joychou/controller/CORS.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public String seccode(HttpServletRequest request, HttpServletResponse response)
5757
}
5858
response.setHeader("Access-Control-Allow-Origin", origin);
5959
response.setHeader("Access-Control-Allow-Credentials", "true");
60-
return JSONP.getUserInfo(request);
60+
return JSONP.getUserInfo2JsonStr(request);
6161
}
6262

6363

src/main/java/org/joychou/controller/jsonp/JSONP.java

+24-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import com.alibaba.fastjson.JSON;
44
import com.alibaba.fastjson.JSONObject;
55

6+
import com.netflix.ribbon.proxy.annotation.Http;
67
import org.joychou.security.SecurityUtil;
78
import org.springframework.http.MediaType;
89
import org.springframework.security.web.csrf.CsrfToken;
910
import org.springframework.web.bind.annotation.*;
11+
import org.springframework.web.servlet.ModelAndView;
12+
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
1013

1114
import javax.servlet.http.HttpServletRequest;
1215
import java.security.Principal;
@@ -27,7 +30,7 @@ public class JSONP {
2730

2831

2932
// get current login username
30-
public static String getUserInfo(HttpServletRequest request) {
33+
public static String getUserInfo2JsonStr(HttpServletRequest request) {
3134
Principal principal = request.getUserPrincipal();
3235

3336
String username = principal.getName();
@@ -46,7 +49,7 @@ public static String getUserInfo(HttpServletRequest request) {
4649
@RequestMapping(value = "/referer", produces = "application/javascript")
4750
private String referer(HttpServletRequest request) {
4851
String callback = request.getParameter("callback");
49-
return callback + "(" + getUserInfo(request) + ")";
52+
return callback + "(" + getUserInfo2JsonStr(request) + ")";
5053
}
5154

5255
/**
@@ -64,7 +67,7 @@ private String emptyReferer(HttpServletRequest request) {
6467
}
6568

6669
String callback = request.getParameter("callback");
67-
return callback + "(" + getUserInfo(request) + ")";
70+
return callback + "(" + getUserInfo2JsonStr(request) + ")";
6871
}
6972

7073
/**
@@ -77,10 +80,26 @@ private String emptyReferer(HttpServletRequest request) {
7780
*/
7881
@RequestMapping(value = "/advice", produces = MediaType.APPLICATION_JSON_VALUE)
7982
public JSONObject advice(HttpServletRequest request) {
80-
return JSON.parseObject(getUserInfo(request));
83+
return JSON.parseObject(getUserInfo2JsonStr(request));
84+
}
85+
8186

87+
/**
88+
* http://localhost:8080/jsonp/mappingJackson2JsonView?callback=test
89+
* Reference: https://p0sec.net/index.php/archives/122/ from p0
90+
* Affected version: java-sec-code test case version: 4.3.6
91+
* - Spring Framework 5.0 to 5.0.6
92+
* - Spring Framework 4.1 to 4.3.17
93+
*/
94+
@RequestMapping(value = "/mappingJackson2JsonView", produces = MediaType.APPLICATION_JSON_VALUE)
95+
public ModelAndView mappingJackson2JsonView(HttpServletRequest req) {
96+
ModelAndView view = new ModelAndView(new MappingJackson2JsonView());
97+
Principal principal = req.getUserPrincipal();
98+
view.addObject("username", principal.getName() );
99+
return view;
82100
}
83101

102+
84103
/**
85104
* Safe code.
86105
* http://localhost:8080/jsonp/sec?callback=test
@@ -94,7 +113,7 @@ private String safecode(HttpServletRequest request) {
94113
}
95114

96115
String callback = request.getParameter("callback");
97-
return callback + "(" + getUserInfo(request) + ")";
116+
return callback + "(" + getUserInfo2JsonStr(request) + ")";
98117
}
99118

100119

src/main/java/org/joychou/controller/jsonp/JSONPAdvice.java

+1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ public class JSONPAdvice extends AbstractJsonpResponseBodyAdvice {
1212
public JSONPAdvice(@Value("${joychou.security.jsonp.callback}") String[] callback) {
1313
super(callback); // Can set multiple paramNames
1414
}
15+
1516
}

0 commit comments

Comments
 (0)