1+ import com .alibaba .fastjson .JSONObject ;
2+ import com .fasterxml .jackson .databind .ObjectMapper ;
3+ import com .google .gson .Gson ;
4+ import java .io .PrintWriter ;
5+ import java .util .HashMap ;
6+ import java .util .Random ;
7+ import javax .servlet .http .HttpServletRequest ;
8+ import javax .servlet .http .HttpServletResponse ;
9+ import org .springframework .stereotype .Controller ;
10+ import org .springframework .web .bind .annotation .GetMapping ;
11+ import org .springframework .web .bind .annotation .ResponseBody ;
12+
13+ @ Controller
14+ public class JsonHijacking {
15+
16+ private static HashMap hashMap = new HashMap ();
17+
18+ static {
19+ hashMap .put ("username" ,"admin" );
20+ hashMap .put ("password" ,"123456" );
21+ }
22+
23+
24+ @ GetMapping (value = "jsonp1" )
25+ @ ResponseBody
26+ public String bad1 (HttpServletRequest request ) {
27+ String resultStr = null ;
28+ String jsonpCallback = request .getParameter ("jsonpCallback" );
29+
30+ Gson gson = new Gson ();
31+ String result = gson .toJson (hashMap );
32+ resultStr = jsonpCallback + "(" + result + ")" ;
33+ return resultStr ;
34+ }
35+
36+ @ GetMapping (value = "jsonp2" )
37+ @ ResponseBody
38+ public String bad2 (HttpServletRequest request ) {
39+ String resultStr = null ;
40+ String jsonpCallback = request .getParameter ("jsonpCallback" );
41+
42+ resultStr = jsonpCallback + "(" + JSONObject .toJSONString (hashMap ) + ")" ;
43+
44+ return resultStr ;
45+ }
46+
47+ @ GetMapping (value = "jsonp3" )
48+ @ ResponseBody
49+ public String bad3 (HttpServletRequest request ) {
50+ String resultStr = null ;
51+ String jsonpCallback = request .getParameter ("jsonpCallback" );
52+ String jsonStr = getJsonStr (hashMap );
53+ resultStr = jsonpCallback + "(" + jsonStr + ")" ;
54+ return resultStr ;
55+ }
56+
57+ @ GetMapping (value = "jsonp4" )
58+ @ ResponseBody
59+ public String bad4 (HttpServletRequest request ) {
60+ String resultStr = null ;
61+ String jsonpCallback = request .getParameter ("jsonpCallback" );
62+ String restr = JSONObject .toJSONString (hashMap );
63+ resultStr = jsonpCallback + "(" + restr + ");" ;
64+ return resultStr ;
65+ }
66+
67+ @ GetMapping (value = "jsonp5" )
68+ @ ResponseBody
69+ public void bad5 (HttpServletRequest request ,
70+ HttpServletResponse response ) throws Exception {
71+ response .setContentType ("application/json" );
72+ String jsonpCallback = request .getParameter ("jsonpCallback" );
73+ PrintWriter pw = null ;
74+ Gson gson = new Gson ();
75+ String result = gson .toJson (hashMap );
76+
77+ String resultStr = null ;
78+ pw = response .getWriter ();
79+ resultStr = jsonpCallback + "(" + result + ")" ;
80+ pw .println (resultStr );
81+ }
82+
83+ @ GetMapping (value = "jsonp6" )
84+ @ ResponseBody
85+ public void bad6 (HttpServletRequest request ,
86+ HttpServletResponse response ) throws Exception {
87+ response .setContentType ("application/json" );
88+ String jsonpCallback = request .getParameter ("jsonpCallback" );
89+ PrintWriter pw = null ;
90+ ObjectMapper mapper = new ObjectMapper ();
91+ String result = mapper .writeValueAsString (hashMap );
92+ String resultStr = null ;
93+ pw = response .getWriter ();
94+ resultStr = jsonpCallback + "(" + result + ")" ;
95+ pw .println (resultStr );
96+ }
97+
98+ @ GetMapping (value = "jsonp7" )
99+ @ ResponseBody
100+ public String good (HttpServletRequest request ) {
101+ String resultStr = null ;
102+ String jsonpCallback = request .getParameter ("jsonpCallback" );
103+
104+ String val = "" ;
105+ Random random = new Random ();
106+ for (int i = 0 ; i < 10 ; i ++) {
107+ val += String .valueOf (random .nextInt (10 ));
108+ }
109+ // good
110+ jsonpCallback = jsonpCallback + "_" + val ;
111+ String jsonStr = getJsonStr (hashMap );
112+ resultStr = jsonpCallback + "(" + jsonStr + ")" ;
113+ return resultStr ;
114+ }
115+
116+ public static String getJsonStr (Object result ) {
117+ return JSONObject .toJSONString (result );
118+ }
119+ }
0 commit comments