| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 588876c commit cbde4b4
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -84,8 +84,10 @@ public static boolean isTableKey(String key) { | |||
| 84 | 84 | //JSONObject内关键词 key <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | |
| 85 | 85 | ||
| 86 | 86 | ||
| 87 | - public static final String KEY_ID = "id"; | ||
| 88 | - public static final String KEY_ID_IN = KEY_ID + "{}"; | ||
| 87 | + public static String KEY_ID = "id"; | ||
| 88 | + public static String KEY_ID_IN = KEY_ID + "{}"; | ||
| 89 | + public static String KEY_USER_ID = "userId"; | ||
| 90 | + public static String KEY_USER_ID_IN = KEY_USER_ID + "{}"; | ||
| 89 | 91 | ||
| 90 | 92 | /**set "id":id in Table layer | |
| 91 | 93 | * @param id | |
@@ -94,13 +96,28 @@ public static boolean isTableKey(String key) { | |||
| 94 | 96 | public JSONObject setId(Long id) { | |
| 95 | 97 | return puts(KEY_ID, id); | |
| 96 | 98 | } | |
| 97 | - /**set id{}:[] in Table layer | ||
| 99 | + /**set "id{}":[] in Table layer | ||
| 98 | 100 | * @param list | |
| 99 | 101 | * @return | |
| 100 | 102 | */ | |
| 101 | 103 | public JSONObject setIdIn(List<Object> list) { | |
| 102 | 104 | return puts(KEY_ID_IN, list); | |
| 103 | 105 | } | |
| 106 | + | ||
| 107 | + /**set "userId":userId in Table layer | ||
| 108 | + * @param id | ||
| 109 | + * @return | ||
| 110 | + */ | ||
| 111 | + public JSONObject setUserId(Long id) { | ||
| 112 | + return puts(KEY_USER_ID, id); | ||
| 113 | + } | ||
| 114 | + /**set "userId{}":[] in Table layer | ||
| 115 | + * @param list | ||
| 116 | + * @return | ||
| 117 | + */ | ||
| 118 | + public JSONObject setUserIdIn(List<Object> list) { | ||
| 119 | + return puts(KEY_USER_ID_IN, list); | ||
| 120 | + } | ||
| 104 | 121 | ||
| 105 | 122 | ||
| 106 | 123 | //@key关键字都放这个类 <<<<<<<<<<<<<<<<<<<<<< | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,7 +15,7 @@ | |||
| 15 | 15 | package zuo.biao.apijson.server; | |
| 16 | 16 | ||
| 17 | 17 | import 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; | ||
| 19 | 19 | import static zuo.biao.apijson.server.Operation.ADD; | |
| 20 | 20 | import static zuo.biao.apijson.server.Operation.DISALLOW; | |
| 21 | 21 | import static zuo.biao.apijson.server.Operation.NECESSARY; | |
@@ -82,7 +82,6 @@ public static JSONObject parseRequest(@NotNull final RequestMethod method, final | |||
| 82 | 82 | return null; | |
| 83 | 83 | } | |
| 84 | 84 | ||
| 85 | - //TODO globleRole要不要改成@role? 只允许服务端Request表中加上可控的ADMIN角色 | ||
| 86 | 85 | if (RequestRole.get(request.getString(JSONRequest.KEY_ROLE)) == RequestRole.ADMIN) { | |
| 87 | 86 | throw new IllegalArgumentException("角色设置错误!不允许在写操作Request中传 " + name + | |
| 88 | 87 | ":{ " + JSONRequest.KEY_ROLE + ":admin } !"); | |
@@ -105,43 +104,8 @@ public JSONObject onParseJSONObject(String key, JSONObject tobj, JSONObject robj | |||
| 105 | 104 | } | |
| 106 | 105 | } else { | |
| 107 | 106 | 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); | ||
| 145 | 109 | } | |
| 146 | 110 | } | |
| 147 | 111 | } | |
@@ -151,8 +115,61 @@ public JSONObject onParseJSONObject(String key, JSONObject tobj, JSONObject robj | |||
| 151 | 115 | }); | |
| 152 | 116 | ||
| 153 | 117 | } | |
| 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 | + } | ||
| 154 | 170 | ||
| 155 | 171 | ||
| 172 | + | ||
| 156 | 173 | /**校验并将response转换为指定的内容和结构 | |
| 157 | 174 | * @param method | |
| 158 | 175 | * @param name | |
| Back | FazBrowse Home | New Git URL |
0 commit comments