1+ import com .alibaba .fastjson .JSONObject ;
2+ import com .fasterxml .jackson .databind .ObjectMapper ;
3+ import com .google .gson .Gson ;
4+ import java .io .BufferedReader ;
5+ import java .io .IOException ;
6+ import java .io .InputStreamReader ;
7+ import java .io .PrintWriter ;
8+ import java .util .HashMap ;
9+ import javax .servlet .http .HttpServletRequest ;
10+ import javax .servlet .http .HttpServletResponse ;
11+ import org .springframework .stereotype .Controller ;
12+ import org .springframework .web .bind .annotation .GetMapping ;
13+ import org .springframework .web .bind .annotation .RequestMapping ;
14+ import org .springframework .web .bind .annotation .RequestMethod ;
15+ import org .springframework .web .bind .annotation .RequestParam ;
16+ import org .springframework .web .bind .annotation .ResponseBody ;
17+ import org .springframework .web .multipart .MultipartFile ;
18+
19+ @ Controller
20+ public class JsonpInjection {
21+
22+ private static HashMap hashMap = new HashMap ();
23+
24+ static {
25+ hashMap .put ("username" ,"admin" );
26+ hashMap .put ("password" ,"123456" );
27+ }
28+
29+ @ GetMapping (value = "jsonp1" )
30+ @ ResponseBody
31+ public String bad1 (HttpServletRequest request ) {
32+ String resultStr = null ;
33+ String jsonpCallback = request .getParameter ("jsonpCallback" );
34+ Gson gson = new Gson ();
35+ String result = gson .toJson (hashMap );
36+ resultStr = jsonpCallback + "(" + result + ")" ;
37+ return resultStr ;
38+ }
39+
40+ @ GetMapping (value = "jsonp2" )
41+ @ ResponseBody
42+ public String bad2 (HttpServletRequest request ) {
43+ String resultStr = null ;
44+ String jsonpCallback = request .getParameter ("jsonpCallback" );
45+ resultStr = jsonpCallback + "(" + JSONObject .toJSONString (hashMap ) + ")" ;
46+ return resultStr ;
47+ }
48+
49+ @ GetMapping (value = "jsonp3" )
50+ @ ResponseBody
51+ public String bad3 (HttpServletRequest request ) {
52+ String resultStr = null ;
53+ String jsonpCallback = request .getParameter ("jsonpCallback" );
54+ String jsonStr = getJsonStr (hashMap );
55+ resultStr = jsonpCallback + "(" + jsonStr + ")" ;
56+ return resultStr ;
57+ }
58+
59+ @ GetMapping (value = "jsonp4" )
60+ @ ResponseBody
61+ public String bad4 (HttpServletRequest request ) {
62+ String resultStr = null ;
63+ String jsonpCallback = request .getParameter ("jsonpCallback" );
64+ String restr = JSONObject .toJSONString (hashMap );
65+ resultStr = jsonpCallback + "(" + restr + ");" ;
66+ return resultStr ;
67+ }
68+
69+ @ GetMapping (value = "jsonp5" )
70+ @ ResponseBody
71+ public void bad5 (HttpServletRequest request ,
72+ HttpServletResponse response ) throws Exception {
73+ String jsonpCallback = request .getParameter ("jsonpCallback" );
74+ PrintWriter pw = null ;
75+ Gson gson = new Gson ();
76+ String result = gson .toJson (hashMap );
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+ String jsonpCallback = request .getParameter ("jsonpCallback" );
88+ PrintWriter pw = null ;
89+ ObjectMapper mapper = new ObjectMapper ();
90+ String result = mapper .writeValueAsString (hashMap );
91+ String resultStr = null ;
92+ pw = response .getWriter ();
93+ resultStr = jsonpCallback + "(" + result + ")" ;
94+ pw .println (resultStr );
95+ }
96+
97+ @ RequestMapping (value = "jsonp7" , method = RequestMethod .GET )
98+ @ ResponseBody
99+ public String bad7 (HttpServletRequest request ) {
100+ String resultStr = null ;
101+ String jsonpCallback = request .getParameter ("jsonpCallback" );
102+ Gson gson = new Gson ();
103+ String result = gson .toJson (hashMap );
104+ resultStr = jsonpCallback + "(" + result + ")" ;
105+ return resultStr ;
106+ }
107+
108+ @ RequestMapping (value = "jsonp11" )
109+ @ ResponseBody
110+ public String good1 (HttpServletRequest request ) {
111+ JSONObject parameterObj = readToJSONObect (request );
112+ String resultStr = null ;
113+ String jsonpCallback = request .getParameter ("jsonpCallback" );
114+ String restr = JSONObject .toJSONString (hashMap );
115+ resultStr = jsonpCallback + "(" + restr + ");" ;
116+ return resultStr ;
117+ }
118+
119+ @ RequestMapping (value = "jsonp12" )
120+ @ ResponseBody
121+ public String good2 (@ RequestParam ("file" ) MultipartFile file ,HttpServletRequest request ) {
122+ if (null == file ){
123+ return "upload file error" ;
124+ }
125+ String fileName = file .getOriginalFilename ();
126+ System .out .println ("file operations" );
127+ String resultStr = null ;
128+ String jsonpCallback = request .getParameter ("jsonpCallback" );
129+ String restr = JSONObject .toJSONString (hashMap );
130+ resultStr = jsonpCallback + "(" + restr + ");" ;
131+ return resultStr ;
132+ }
133+
134+ public static JSONObject readToJSONObect (HttpServletRequest request ){
135+ String jsonText = readPostContent (request );
136+ JSONObject jsonObj = JSONObject .parseObject (jsonText , JSONObject .class );
137+ return jsonObj ;
138+ }
139+
140+ public static String readPostContent (HttpServletRequest request ){
141+ BufferedReader in = null ;
142+ String content = null ;
143+ String line = null ;
144+ try {
145+ in = new BufferedReader (new InputStreamReader (request .getInputStream (),"UTF-8" ));
146+ StringBuilder buf = new StringBuilder ();
147+ while ((line = in .readLine ()) != null ) {
148+ buf .append (line );
149+ }
150+ content = buf .toString ();
151+ } catch (IOException e ) {
152+ e .printStackTrace ();
153+ }
154+ String uri = request .getRequestURI ();
155+ return content ;
156+ }
157+
158+ public static String getJsonStr (Object result ) {
159+ return JSONObject .toJSONString (result );
160+ }
161+ }
0 commit comments