1515package zuo .biao .apijson .server ;
1616
1717import static zuo .biao .apijson .JSONObject .KEY_ID ;
18- import static zuo .biao .apijson .JSONObject .KEY_ID_IN ;
18+ import static zuo .biao .apijson .JSONObject .KEY_USER_ID ;
1919import static zuo .biao .apijson .server .Operation .ADD ;
2020import static zuo .biao .apijson .server .Operation .DISALLOW ;
2121import static zuo .biao .apijson .server .Operation .NECESSARY ;
@@ -82,7 +82,6 @@ public static JSONObject parseRequest(@NotNull final RequestMethod method, final
8282 return null ;
8383 }
8484
85- //TODO globleRole要不要改成@role? 只允许服务端Request表中加上可控的ADMIN角色
8685 if (RequestRole .get (request .getString (JSONRequest .KEY_ROLE )) == RequestRole .ADMIN ) {
8786 throw new IllegalArgumentException ("角色设置错误!不允许在写操作Request中传 " + name +
8887 ":{ " + JSONRequest .KEY_ROLE + ":admin } !" );
@@ -105,43 +104,8 @@ public JSONObject onParseJSONObject(String key, JSONObject tobj, JSONObject robj
105104 }
106105 } else {
107106 if (RequestMethod .isQueryMethod (method ) == false ) {
108- //单个修改或删除
109- Object id = null ;
110- try {
111- id = robj .getLong (KEY_ID ); //如果必须传 id ,可在Request表中配置NECESSARY
112- } catch (Exception e ) {
113- throw new IllegalArgumentException (method .name () + "请求," + name + "/" + key
114- + " 里面的 " + KEY_ID + ":value 中value的类型只能是 Long !" );
115- }
116-
117- JSONArray idIn = null ;
118- try {
119- idIn = robj .getJSONArray (KEY_ID_IN ); //如果必须传 id{} ,可在Request表中配置NECESSARY
120- } catch (Exception e ) {
121- throw new IllegalArgumentException (method .name () + "请求," + name + "/" + key
122- + " 里面的 " + KEY_ID_IN + ":value 中value的类型只能是 [Long] !" );
123- }
124- if (idIn == null ) {
125- //批量修改或删除
126- if (id == null ) {
127- throw new IllegalArgumentException (method .name () + "请求," + name + "/" + key
128- + " 里面 " + KEY_ID + " 和 " + KEY_ID_IN + " 至少传其中一个!" );
129- }
130- } else {
131- if (idIn .size () > 10 ) { //不允许一次操作10条以上记录
132- throw new IllegalArgumentException (method .name () + "请求," + name + "/" + key
133- + " 里面的 " + KEY_ID_IN + ":[] 中[]的长度不能超过10!" );
134- }
135- //解决 id{}: ["1' OR 1='1'))--"] 绕过id{}限制
136- for (int i = 0 ; i < idIn .size (); i ++) {
137- try {
138- idIn .getLong (i );
139- } catch (Exception e ) {
140- throw new IllegalArgumentException (method .name () + "请求," + name + "/" + key
141- + " 里面的 " + KEY_ID_IN + ":[] 中所有项的类型都只能是Long!" );
142- }
143- }
144- }
107+ verifyId (method .name (), name , key , robj , KEY_ID , true );
108+ verifyId (method .name (), name , key , robj , KEY_USER_ID , false );
145109 }
146110 }
147111 }
@@ -151,8 +115,61 @@ public JSONObject onParseJSONObject(String key, JSONObject tobj, JSONObject robj
151115 });
152116
153117 }
118+
119+ /**
120+ * @param method
121+ * @param name
122+ * @param key
123+ * @param robj
124+ * @param idKey
125+ * @param atLeastOne 至少有一个不为null
126+ */
127+ private static void verifyId (@ NotNull String method , @ NotNull String name , @ NotNull String key
128+ , @ NotNull JSONObject robj , @ NotNull String idKey , boolean atLeastOne ) {
129+ //单个修改或删除
130+ Object id = null ;
131+ try {
132+ id = robj .getLong (idKey ); //如果必须传 id ,可在Request表中配置NECESSARY
133+ } catch (Exception e ) {
134+ throw new IllegalArgumentException (method + "请求," + name + "/" + key
135+ + " 里面的 " + idKey + ":value 中value的类型只能是 Long !" );
136+ }
137+
138+ //批量修改或删除
139+ String idInKey = idKey + "{}" ;
140+
141+ JSONArray idIn = null ;
142+ try {
143+ idIn = robj .getJSONArray (idInKey ); //如果必须传 id{} ,可在Request表中配置NECESSARY
144+ } catch (Exception e ) {
145+ throw new IllegalArgumentException (method + "请求," + name + "/" + key
146+ + " 里面的 " + idInKey + ":value 中value的类型只能是 [Long] !" );
147+ }
148+ if (idIn == null ) {
149+ if (atLeastOne && id == null ) {
150+ throw new IllegalArgumentException (method + "请求," + name + "/" + key
151+ + " 里面 " + idKey + " 和 " + idInKey + " 至少传其中一个!" );
152+ }
153+ } else {
154+ if (idIn .size () > 10 ) { //不允许一次操作10条以上记录
155+ throw new IllegalArgumentException (method + "请求," + name + "/" + key
156+ + " 里面的 " + idInKey + ":[] 中[]的长度不能超过10!" );
157+ }
158+ //解决 id{}: ["1' OR 1='1'))--"] 绕过id{}限制
159+ //new ArrayList<Long>(idIn) 不能检查类型,Java泛型擦除问题,居然能把 ["a"] 赋值进去还不报错
160+ for (int i = 0 ; i < idIn .size (); i ++) {
161+ try {
162+ idIn .getLong (i );
163+ } catch (Exception e ) {
164+ throw new IllegalArgumentException (method + "请求," + name + "/" + key
165+ + " 里面的 " + idInKey + ":[] 中所有项的类型都只能是Long!" );
166+ }
167+ }
168+ }
169+ }
154170
155171
172+
156173 /**校验并将response转换为指定的内容和结构
157174 * @param method
158175 * @param name
0 commit comments