Skip to content

Commit 289b506

Browse files
committed
Server:Parser权限面验证改为只是方法权限免验证;StructureVerifier移到公共包
1 parent 15c57dc commit 289b506

3 files changed

Lines changed: 18 additions & 199 deletions

File tree

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/server/StructureVerifier.java renamed to APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/StructureVerifier.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
See the License for the specific language governing permissions and
1313
limitations under the License.*/
1414

15-
package zuo.biao.apijson.server;
15+
package zuo.biao.apijson;
1616

1717
import static zuo.biao.apijson.RequestMethod.GET;
1818
import static zuo.biao.apijson.RequestMethod.POST;
@@ -25,10 +25,8 @@
2525

2626
import com.alibaba.fastjson.JSONObject;
2727

28-
import zuo.biao.apijson.JSON;
29-
import zuo.biao.apijson.Log;
30-
import zuo.biao.apijson.RequestMethod;
31-
import zuo.biao.apijson.StringUtil;
28+
import zuo.biao.apijson.server.JSONRequest;
29+
import zuo.biao.apijson.server.Parser;
3230
import zuo.biao.apijson.server.sql.QueryConfig;
3331
import zuo.biao.apijson.server.sql.QueryHelper;
3432

@@ -42,15 +40,15 @@ public class StructureVerifier {
4240
public static final int TYPE_REQUEST = 1;
4341
public static final int TYPE_RESPONSE = 2;
4442

45-
private int type;
43+
private int type;//TODO request,response
4644

4745
public StructureVerifier(int type) {
4846
this.type = type;
4947
}
5048

5149

5250

53-
51+
5452
/**获取正确的请求,非GET请求必须是服务器指定的
5553
* @param method
5654
* @param request
@@ -227,5 +225,7 @@ public static boolean isContainKeyInArray(String key, String[] array) {
227225
}
228226

229227

228+
229+
230230

231231
}

‎APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/server/Controller.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import zuo.biao.apijson.JSONResponse;
3030
import zuo.biao.apijson.RequestMethod;
3131
import zuo.biao.apijson.StringUtil;
32+
import zuo.biao.apijson.StructureVerifier;
3233
import zuo.biao.apijson.server.exception.ConditionNotMatchException;
3334
import zuo.biao.apijson.server.exception.ConflictException;
3435
import zuo.biao.apijson.server.model.Login;
@@ -228,7 +229,7 @@ public String login(@PathVariable String typeString, @PathVariable String phone,
228229
public String register(@RequestBody String request) {
229230
JSONObject requestObject = null;
230231
try {
231-
requestObject = Parser.getCorrectRequest(RequestMethod.POST
232+
requestObject = StructureVerifier.getCorrectRequest(RequestMethod.POST
232233
, Parser.parseRequest(request, RequestMethod.POST));
233234
} catch (Exception e) {
234235
return JSON.toJSONString(Parser.newErrorResult(e));

‎APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/server/Parser.java‎

Lines changed: 9 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,14 @@
1616

1717
import static zuo.biao.apijson.RequestMethod.GET;
1818
import static zuo.biao.apijson.RequestMethod.HEAD;
19-
import static zuo.biao.apijson.RequestMethod.POST;
2019
import static zuo.biao.apijson.RequestMethod.POST_GET;
2120
import static zuo.biao.apijson.RequestMethod.POST_HEAD;
2221
import static zuo.biao.apijson.RequestMethod.PUT;
2322
import static zuo.biao.apijson.StringUtil.UTF_8;
2423

2524
import java.io.UnsupportedEncodingException;
2625
import java.net.URLDecoder;
27-
import java.util.ArrayList;
2826
import java.util.HashMap;
29-
import java.util.List;
3027
import java.util.Map;
3128
import java.util.Set;
3229
import java.util.concurrent.TimeoutException;
@@ -42,6 +39,7 @@
4239
import zuo.biao.apijson.Log;
4340
import zuo.biao.apijson.RequestMethod;
4441
import zuo.biao.apijson.StringUtil;
42+
import zuo.biao.apijson.StructureVerifier;
4543
import zuo.biao.apijson.server.exception.ConditionNotMatchException;
4644
import zuo.biao.apijson.server.exception.ConflictException;
4745
import zuo.biao.apijson.server.sql.AccessVerifier;
@@ -71,10 +69,10 @@ public Parser(RequestMethod method) {
7169
}
7270

7371
private final RequestMethod requestMethod;
74-
private final boolean noVerify;
72+
private final boolean noVerify;//方法免验证。但内容还是需要验证,避免服务端写错代码导致产生脏数据甚至清空Table
7573
/**
7674
* @param requestMethod null ? requestMethod = GET
77-
* @param noVerify 仅限于为服务端提供免验证特权,普通请求不要设置为true! 如果对应Table有权限也建议用默认值false,保持和客户端权限一致
75+
* @param noVerify 仅限于为服务端提供方法免验证特权,普通请求不要设置为true! 如果对应Table有权限也建议用默认值false,保持和客户端权限一致
7876
*/
7977
public Parser(RequestMethod method, boolean noVerify) {
8078
super();
@@ -115,15 +113,13 @@ public String parse(JSONObject request) {
115113
public JSONObject parseResponse(String request) {
116114
Log.d(TAG, "\n\n\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"
117115
+ requestMethod + "/parseResponse request = \n" + request + "\n\n");
118-
119-
requestObject = parseRequest(request, requestMethod);
120-
if (noVerify == false) {
121-
try {
122-
requestObject = getCorrectRequest(requestMethod, requestObject);
123-
} catch (Exception e) {
124-
return newErrorResult(e);
125-
}
116+
117+
try {
118+
requestObject = StructureVerifier.getCorrectRequest(requestMethod, parseRequest(request, requestMethod));
119+
} catch (Exception e) {
120+
return newErrorResult(e);
126121
}
122+
127123
return parseResponse(requestObject);
128124
}
129125
/**解析请求json并获取对应结果
@@ -287,184 +283,6 @@ public static JSONObject newErrorResult(Exception e) {
287283

288284

289285

290-
/**获取正确的请求,非GET请求必须是服务器指定的
291-
* @param method
292-
* @param request
293-
* @return
294-
* @throws Exception
295-
*/
296-
public static JSONObject getCorrectRequest(RequestMethod method, JSONObject request) throws Exception {
297-
return getCorrectRequest(method, request, null);
298-
}
299-
/**获取正确的请求,非GET请求必须是服务器指定的
300-
* @param method
301-
* @param request
302-
* @param queryHelper
303-
* @return
304-
*/
305-
public static JSONObject getCorrectRequest(RequestMethod method, JSONObject request, QueryHelper queryHelper)
306-
throws Exception {
307-
if (isPublicMethod(method)) {
308-
return request;//需要指定JSON结构的get请求可以改为post请求。一般只有对安全性要求高的才会指定,而这种情况用明文的GET方式几乎肯定不安全
309-
}
310-
311-
String tag = request.getString(JSONRequest.KEY_TAG);
312-
if (StringUtil.isNotEmpty(tag, true) == false) {
313-
throw new IllegalArgumentException("请指定tag!一般是table名称");
314-
}
315-
316-
//获取指定的JSON结构 <<<<<<<<<<<<<<
317-
QueryConfig config = new QueryConfig(GET, "Request");
318-
config.setColumn("structure");
319-
320-
Map<String, Object> where = new HashMap<String, Object>();
321-
where.put("method", method.name());
322-
where.put(JSONRequest.KEY_TAG, tag);
323-
config.setWhere(where);
324-
325-
JSONObject object = null;
326-
String error = "";
327-
if (queryHelper == null) {
328-
queryHelper = new QueryHelper();
329-
}
330-
try {
331-
object = queryHelper.select(config);
332-
} catch (Exception e) {
333-
e.printStackTrace();
334-
error = e.getMessage();
335-
}
336-
queryHelper.close();
337-
338-
if (object == null) {//empty表示随意操作 || object.isEmpty()) {
339-
throw new UnsupportedOperationException("非GET请求必须是服务端允许的操作! \n " + error);
340-
}
341-
object = getJSONObject(object, "structure");//解决返回值套了一层 "structure":{}
342-
343-
JSONObject target = null;
344-
if (isTableKey(tag) && object.containsKey(tag) == false) {//tag是table名
345-
target = new JSONObject(true);
346-
target.put(tag, object);
347-
} else {
348-
target = object;
349-
}
350-
//获取指定的JSON结构 >>>>>>>>>>>>>>
351-
352-
request.remove(JSONRequest.KEY_TAG);
353-
return fillTarget(method, target, request, "");
354-
}
355-
356-
357-
public static final String NECESSARY_COLUMNS = "necessaryColumns";
358-
public static final String DISALLOW_COLUMNS = "disallowColumns";
359-
360-
/**从request提取target指定的内容
361-
* @param target
362-
* @param request
363-
* @return
364-
*/
365-
public static JSONObject fillTarget(RequestMethod method
366-
, JSONObject target, final JSONObject request, String requestName) throws Exception {
367-
Log.i(TAG, "filterTarget requestName = " + requestName
368-
+ " target = \n" + JSON.toJSONString(target)
369-
+ "\n request = \n" + JSON.toJSONString(request) + "\n >> return null;");
370-
if (target == null || request == null) {// || request.isEmpty()) {
371-
Log.i(TAG, "filterTarget target == null || request == null >> return null;");
372-
return null;
373-
}
374-
375-
/**方法三:遍历request,transferredRequest只添加target所包含的object
376-
* ,且移除target中DISALLOW_COLUMNS,期间判断NECESSARY_COLUMNS是否都有
377-
*/
378-
String necessarys = StringUtil.getNoBlankString(target.getString(NECESSARY_COLUMNS));
379-
String[] necessaryColumns = StringUtil.split(necessarys);
380-
381-
//判断必要字段是否都有
382-
if (necessaryColumns != null) {
383-
for (String s : necessaryColumns) {
384-
if (request.containsKey(s) == false) {
385-
throw new IllegalArgumentException(requestName
386-
+ "不能缺少 " + s + " 等[" + necessarys + "]内的任何字段!");
387-
}
388-
}
389-
}
390-
391-
String disallows = StringUtil.getNoBlankString(target.getString(DISALLOW_COLUMNS));
392-
String[] disallowColumns = null;
393-
394-
Set<String> set = request.keySet();
395-
if ("!".equals(disallows)) {//所有非necessaryColumns,改成 !necessary 更好
396-
if (set != null) {
397-
List<String> disallowList = new ArrayList<String>();
398-
for (String key : set) {
399-
if (isContainKeyInArray(key, necessaryColumns) == false) {
400-
disallowList.add(key);
401-
}
402-
}
403-
disallowColumns = disallowList.toArray(new String[]{});
404-
}
405-
} else {
406-
disallowColumns = StringUtil.split(disallows);
407-
}
408-
409-
410-
//填充target
411-
JSONObject transferredRequest = new JSONObject(true);
412-
if (set != null) {
413-
Object value;
414-
JSONObject result;
415-
for (String key : set) {
416-
value = request.get(key);
417-
if (value instanceof JSONObject) {//JSONObject,往下一级提取
418-
if (target.containsKey(key)) {//只填充target有的object
419-
result = fillTarget(method, getJSONObject(target, key), (JSONObject) value, key);//往下一级提取
420-
Log.i(TAG, "fillTarget key = " + key + "; result = " + result);
421-
if (result == null || result.isEmpty()) {//只添加!=null的值,可能数据库返回数据不够count
422-
throw new IllegalArgumentException(requestName
423-
+ "不能缺少 " + key + " 等[" + necessarys + "]内的任何JSONObject!");
424-
}
425-
if (method == POST && result.containsKey(QueryConfig.ID) == false) {//为注册用户返回id
426-
result.put(QueryConfig.ID, System.currentTimeMillis());
427-
}
428-
transferredRequest.put(key, result);
429-
}
430-
} else {//JSONArray或其它Object
431-
if (isContainKeyInArray(key, disallowColumns)) {
432-
throw new IllegalArgumentException(requestName
433-
+ "不允许传 " + key + " 等[" + disallows + "]内的任何字段!");
434-
}
435-
transferredRequest.put(key, value);
436-
}
437-
}
438-
}
439-
440-
Log.i(TAG, "filterTarget return transferredRequest = " + JSON.toJSONString(transferredRequest));
441-
return transferredRequest;
442-
}
443-
444-
/**array至少有一个值在request的key中
445-
* @param key
446-
* @param array
447-
* @return
448-
*/
449-
public static boolean isContainKeyInArray(String key, String[] array) {
450-
if (array == null || array.length <= 0 || key == null) {
451-
Log.i(TAG, "isContainKeyInArray"
452-
+ " array == null || array.length <= 0 || key == null >> return false;");
453-
return false;
454-
}
455-
456-
for (String s : array) {
457-
if (key.equals(s)) {
458-
return true;
459-
}
460-
}
461-
462-
return false;
463-
}
464-
465-
466-
467-
468286

469287
/**获取单个对象,该对象处于parentObject内
470288
* @param parentPath parentObject的路径

0 commit comments

Comments
 (0)