同步eclipse版至idea版

This commit is contained in:
TommyLemon 2017-03-02 12:30:04 +08:00
parent 18d51dd1cb
commit a56ad31eab
4 changed files with 91 additions and 13 deletions

View File

@ -14,12 +14,19 @@ limitations under the License.*/
package zuo.biao.apijson.server;
import java.util.Random;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import zuo.biao.apijson.JSON;
import zuo.biao.apijson.StringUtil;
/**request receiver and controller
* @author Lemon
*/
@ -31,7 +38,7 @@ public class Controller {
public String get(@PathVariable String request) {
return new RequestParser(zuo.biao.apijson.RequestMethod.GET).parse(request);
}
/**用POST方法GET数据request和response都非明文浏览器看不到用于对安全性要求高的GET请求
* @param request
* @return
@ -40,23 +47,78 @@ public class Controller {
public String post_get(@RequestBody String request) {
return new RequestParser(zuo.biao.apijson.RequestMethod.POST_GET).parse(request);
}
@RequestMapping(value="post", method = RequestMethod.POST)
public String post(@RequestBody String request) {
return new RequestParser(zuo.biao.apijson.RequestMethod.POST).parse(request);
}
/**以下接口继续用POST接口是为了客户端方便只需要做getpost请求也可以改用实际对应的方法
* postput方法名可以改为addupdate等更客户端容易懂的名称
*/
*/
@RequestMapping(value="put", method = RequestMethod.POST)
public String put(@RequestBody String request) {
return new RequestParser(zuo.biao.apijson.RequestMethod.PUT).parse(request);
}
@RequestMapping(value="delete", method = RequestMethod.POST)
public String delete(@RequestBody String request) {
return new RequestParser(zuo.biao.apijson.RequestMethod.DELETE).parse(request);
}
@RequestMapping("post/authCode/{phone}")
public String postAuthCode(@PathVariable String phone) {
new RequestParser(zuo.biao.apijson.RequestMethod.DELETE).parse(newVerifyRequest(newVerify(phone, 0)));
JSONObject response = new RequestParser(zuo.biao.apijson.RequestMethod.POST).parseResponse(
newVerifyRequest(newVerify(phone, new Random().nextInt(9999) + 1000)));
JSONObject verify = null;
try {
verify = response.getJSONObject("Verify");
} catch (Exception e) {
// TODO: handle exception
}
if (verify == null || verify.getIntValue("status") != 200) {
return JSON.toJSONString(response);
}
return getAuthCode(phone);
}
@RequestMapping(value="post_get/authCode/{phone}", method = RequestMethod.POST)
public String getAuthCode(@PathVariable String phone) {
return new RequestParser(zuo.biao.apijson.RequestMethod.POST_GET).parse(newVerifyRequest(newVerify(phone, 0)));
}
@RequestMapping("check/authCode/{phone}/{code}")
public String checkAuthCode(@PathVariable String phone, @PathVariable String code) {
if (StringUtil.isNumer(code) == false) {
code = "-1";
}
return new RequestParser(zuo.biao.apijson.RequestMethod.POST_GET).parse(
newVerifyRequest(newVerify(phone, Integer.parseInt(0 + StringUtil.getNumber(code)))));
}
private JSONObject newVerify(String phone, int code) {
JSONObject verify = new JSONObject(true);
verify.put("id", phone);
if (code > 0) {
verify.put("code", code);
}
return verify;
}
private JSONObject newVerifyRequest(JSONObject verify) {
return newRequest(verify, "Verify", true);
}
private JSONObject newRequest(JSONObject object, String name, boolean needTag) {
JSONObject request = new JSONObject(true);
request.put(name, object);
if (needTag) {
request.put("tag", name);
}
return request;
}
}

View File

@ -230,17 +230,16 @@ public class QueryConfig {
* @return in ('key0', 'key1', ... )
*/
public static String getInString(Object[] in) {
if (in == null || in.length <= 0) {
return "";
}
String inString = "";
for (int i = 0; i < in.length; i++) {
inString += ((i > 0 ? "," : "") + "'" + in[i] + "'");
if (in != null) {//返回 "" 会导致 id:[] 空值时效果和没有筛选id一样
for (int i = 0; i < in.length; i++) {
inString += ((i > 0 ? "," : "") + "'" + in[i] + "'");
}
}
return " in (" + inString + ") ";
}
/**获取筛选方法
* @return
*/

View File

@ -55,6 +55,7 @@ public class RequestParser {
private boolean parseRelation;
private Map<String, String> relationMap;
/**解析请求json并获取对应结果
* @param request
* @return
@ -67,6 +68,20 @@ public class RequestParser {
return response;
}
/**解析请求json并获取对应结果
* @param request
* @return
*/
public String parse(JSONObject request) {
return JSON.toJSONString(parseResponse(request));
}
/**解析请求json并获取对应结果
* @param request
* @return requestObject
*/
public JSONObject parseResponse(JSONObject request) {
return parseResponse(JSON.toJSONString(request));
}
/**解析请求json并获取对应结果
* @param request
* @return requestObject
@ -395,7 +410,7 @@ public class RequestParser {
}
} else {//JSONArray或其它Object直接填充
transferredRequest.put(key, value);
//替换path
if (value instanceof String && StringUtil.isPath((String) value)) {
System.out.println("getObject StringUtil.isPath(value) >> parseRelation = " + parseRelation);

View File

@ -47,6 +47,7 @@ public class AccessVerifier {
accessMap.put("Password", new RequestMethod[]{POST_GET, POST, PUT, DELETE});
accessMap.put("Login", new RequestMethod[]{POST_GET, POST, DELETE});
accessMap.put("Request", new RequestMethod[]{GET, POST_GET});
accessMap.put("Verify", new RequestMethod[]{POST_GET, POST, DELETE});
}
/**验证权限是否通过
@ -236,4 +237,5 @@ public class AccessVerifier {
throw new AccessException(table + "不支持" + method + "方法!");
}
}