1+ package org .joychou .controller .jsonp ;
2+
3+ import com .alibaba .fastjson .JSON ;
4+ import com .alibaba .fastjson .JSONObject ;
5+ import org .joychou .security .SecurityUtil ;
6+ import org .springframework .http .MediaType ;
7+ import org .springframework .web .bind .annotation .*;
8+ import javax .servlet .http .HttpServletRequest ;
9+ import javax .servlet .http .HttpServletResponse ;
10+
11+
12+
13+ /**
14+ * @author JoyChou ([email protected] ) @ 2018.10.24 15+ * https://github.com/JoyChou93/java-sec-code/wiki/JSONP
16+ */
17+
18+ @ RestController
19+ @ RequestMapping ("/jsonp" )
20+ public class JSONP {
21+
22+ private static String info = "{\" name\" : \" JoyChou\" , \" phone\" : \" 18200001111\" }" ;
23+ private static String [] urlwhitelist = {"joychou.com" , "joychou.org" };
24+
25+
26+ /**
27+ * Set the response content-type to application/javascript.
28+ *
29+ * http://localhost:8080/jsonp/referer?callback=test
30+ *
31+ */
32+ @ RequestMapping (value = "/referer" , produces = "application/javascript" )
33+ private static String referer (HttpServletRequest request , HttpServletResponse response ) {
34+ String callback = request .getParameter ("callback" );
35+ return callback + "(" + info + ")" ;
36+ }
37+
38+ /**
39+ * Direct access does not check Referer, non-direct access check referer.
40+ * Developer like to do jsonp testing like this.
41+ *
42+ * http://localhost:8080/jsonp/emptyReferer?callback=test
43+ *
44+ */
45+ @ RequestMapping (value = "/emptyReferer" , produces = "application/javascript" )
46+ private static String emptyReferer (HttpServletRequest request , HttpServletResponse response ) {
47+ String referer = request .getHeader ("referer" );
48+
49+ if (null != referer && !SecurityUtil .checkURLbyEndsWith (referer , urlwhitelist )) {
50+ return "error" ;
51+ }
52+
53+ String callback = request .getParameter ("callback" );
54+ return callback + "(" + info + ")" ;
55+ }
56+
57+ /**
58+ * Adding callback or cback on parameter can automatically return jsonp data.
59+ * http://localhost:8080/jsonp/advice?callback=test
60+ * http://localhost:8080/jsonp/advice?cback=test
61+ *
62+ * @return Only return object, AbstractJsonpResponseBodyAdvice can be used successfully.
63+ * Such as JSONOjbect or JavaBean. String type cannot be used.
64+ */
65+ @ RequestMapping (value = "/advice" , produces = MediaType .APPLICATION_JSON_VALUE )
66+ public JSONObject advice () {
67+ return JSON .parseObject (info );
68+
69+ }
70+
71+ /**
72+ * Safe code.
73+ * http://localhost:8080/jsonp/sec?callback=test
74+ */
75+ @ RequestMapping (value = "/sec" , produces = "application/javascript" )
76+ private static String safecode (HttpServletRequest request , HttpServletResponse response ) {
77+ String referer = request .getHeader ("referer" );
78+
79+ if (!SecurityUtil .checkURLbyEndsWith (referer , urlwhitelist )) {
80+ return "error" ;
81+ }
82+
83+ String callback = request .getParameter ("callback" );
84+ return callback + "(" + info + ")" ;
85+ }
86+
87+
88+
89+ }
0 commit comments