Skip to content

Commit 7fd6a60

Browse files
committed
Server:同步eclipse版至idea版
1 parent 9d27af3 commit 7fd6a60

File tree

16 files changed

+147
-527
lines changed

16 files changed

+147
-527
lines changed

APIJSON-Java-Server/APIJSON-Idea/src/main/java/apijson/demo/server/AccessVerifier.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,18 @@
2727

2828
import apijson.demo.server.model.BaseModel;
2929
import apijson.demo.server.model.Comment;
30-
import apijson.demo.server.model.Login;
3130
import apijson.demo.server.model.Moment;
32-
import apijson.demo.server.model.Password;
33-
import apijson.demo.server.model.User;
3431
import apijson.demo.server.model.Privacy;
32+
import apijson.demo.server.model.User;
3533
import apijson.demo.server.model.Verify;
36-
import apijson.demo.server.model.Wallet;
3734
import zuo.biao.apijson.JSON;
38-
import zuo.biao.apijson.JSONRequest;
3935
import zuo.biao.apijson.Log;
4036
import zuo.biao.apijson.MethodAccess;
4137
import zuo.biao.apijson.RequestMethod;
4238
import zuo.biao.apijson.RequestRole;
39+
import zuo.biao.apijson.model.Column;
40+
import zuo.biao.apijson.model.Table;
41+
import zuo.biao.apijson.model.Test;
4342
import zuo.biao.apijson.server.exception.NotLoggedInException;
4443
import zuo.biao.apijson.server.sql.SQLConfig;
4544

@@ -57,18 +56,19 @@ public class AccessVerifier {
5756

5857
// <TableName, <METHOD, allowRoles>>
5958
// <User, <GET, [OWNER, ADMIN]>>
60-
public static final Map<String, Map<RequestMethod, RequestRole[]>> accessMap;
59+
public static final Map<String, Map<RequestMethod, RequestRole[]>> ACCESS_MAP;
6160
static {
62-
accessMap = new HashMap<String, Map<RequestMethod, RequestRole[]>>();
63-
64-
accessMap.put(User.class.getSimpleName(), getAccessMap(User.class.getAnnotation(MethodAccess.class)));
65-
accessMap.put(Privacy.class.getSimpleName(), getAccessMap(Privacy.class.getAnnotation(MethodAccess.class)));
66-
accessMap.put(Moment.class.getSimpleName(), getAccessMap(Moment.class.getAnnotation(MethodAccess.class)));
67-
accessMap.put(Comment.class.getSimpleName(), getAccessMap(Comment.class.getAnnotation(MethodAccess.class)));
68-
accessMap.put(Verify.class.getSimpleName(), getAccessMap(Verify.class.getAnnotation(MethodAccess.class)));
69-
accessMap.put(Login.class.getSimpleName(), getAccessMap(Login.class.getAnnotation(MethodAccess.class)));
70-
accessMap.put(Password.class.getSimpleName(), getAccessMap(Password.class.getAnnotation(MethodAccess.class)));
71-
accessMap.put(Wallet.class.getSimpleName(), getAccessMap(Wallet.class.getAnnotation(MethodAccess.class)));
61+
ACCESS_MAP = new HashMap<String, Map<RequestMethod, RequestRole[]>>();
62+
63+
ACCESS_MAP.put(Table.class.getSimpleName(), getAccessMap(Table.class.getAnnotation(MethodAccess.class)));
64+
ACCESS_MAP.put(Column.class.getSimpleName(), getAccessMap(Column.class.getAnnotation(MethodAccess.class)));
65+
ACCESS_MAP.put(Test.class.getSimpleName(), getAccessMap(Test.class.getAnnotation(MethodAccess.class)));
66+
67+
ACCESS_MAP.put(User.class.getSimpleName(), getAccessMap(User.class.getAnnotation(MethodAccess.class)));
68+
ACCESS_MAP.put(Privacy.class.getSimpleName(), getAccessMap(Privacy.class.getAnnotation(MethodAccess.class)));
69+
ACCESS_MAP.put(Moment.class.getSimpleName(), getAccessMap(Moment.class.getAnnotation(MethodAccess.class)));
70+
ACCESS_MAP.put(Comment.class.getSimpleName(), getAccessMap(Comment.class.getAnnotation(MethodAccess.class)));
71+
ACCESS_MAP.put(Verify.class.getSimpleName(), getAccessMap(Verify.class.getAnnotation(MethodAccess.class)));
7272
}
7373

7474
/**获取权限Map,每种操作都只允许对应的角色
@@ -119,7 +119,7 @@ public static boolean verify(SQLConfig config, User visitor) throws Exception {
119119

120120
//验证角色,假定真实强制匹配<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
121121

122-
String userIdkey = Controller.USER_.equals(config.getTable()) || Controller.USER_PRIVACY_.equals(config.getTable())
122+
String userIdkey = Controller.USER_.equals(config.getTable()) || Controller.PRIVACY_.equals(config.getTable())
123123
? Controller.ID : Controller.USER_ID;
124124

125125
if (role == null) {
@@ -202,7 +202,7 @@ public static void verifyRole(String table, RequestMethod method, RequestRole ro
202202
if (role == null) {
203203
role = RequestRole.UNKNOWN;
204204
}
205-
Map<RequestMethod, RequestRole[]> map = accessMap.get(table);
205+
Map<RequestMethod, RequestRole[]> map = ACCESS_MAP.get(table);
206206

207207
if (map == null || BaseModel.isContain(map.get(method), role) == false) {
208208
throw new IllegalAccessException(table + " 不允许 " + role.name() + " 用户的 " + method.name() + " 请求!");

APIJSON-Java-Server/APIJSON-Idea/src/main/java/apijson/demo/server/Controller.java

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,12 @@
3636

3737
import apijson.demo.server.model.BaseModel;
3838
import apijson.demo.server.model.Comment;
39-
import apijson.demo.server.model.Login;
4039
import apijson.demo.server.model.Moment;
41-
import apijson.demo.server.model.Password;
42-
import apijson.demo.server.model.User;
4340
import apijson.demo.server.model.Privacy;
41+
import apijson.demo.server.model.User;
4442
import apijson.demo.server.model.Verify;
45-
import apijson.demo.server.model.Wallet;
4643
import zuo.biao.apijson.JSON;
4744
import zuo.biao.apijson.JSONResponse;
48-
import zuo.biao.apijson.Log;
4945
import zuo.biao.apijson.RequestMethod;
5046
import zuo.biao.apijson.StringUtil;
5147
import zuo.biao.apijson.server.JSONRequest;
@@ -160,19 +156,15 @@ public String delete(@RequestBody String request, HttpSession session) {
160156

161157

162158
public static final String USER_;
159+
public static final String PRIVACY_;
163160
public static final String MOMENT_;
164161
public static final String COMMENT_;
165-
public static final String WALLET_;
166-
public static final String PASSWORD_;
167-
public static final String USER_PRIVACY_;
168-
public static final String VERIFY_;
162+
public static final String VERIFY_; //加下划线后缀是为了避免 Verify 和 verify 都叫VERIFY,分不清
169163
static {
170164
USER_ = User.class.getSimpleName();
165+
PRIVACY_ = Privacy.class.getSimpleName();
171166
MOMENT_ = Moment.class.getSimpleName();
172167
COMMENT_ = Comment.class.getSimpleName();
173-
WALLET_ = Wallet.class.getSimpleName();
174-
PASSWORD_ = Password.class.getSimpleName();
175-
USER_PRIVACY_ = Privacy.class.getSimpleName();
176168
VERIFY_ = Verify.class.getSimpleName();
177169
}
178170

@@ -187,8 +179,8 @@ public String delete(@RequestBody String request, HttpSession session) {
187179
public static final String NAME = "name";
188180
public static final String PHONE = "phone";
189181
public static final String PASSWORD = "password";
190-
public static final String LOGIN_PASSWORD = "loginPassword";
191-
public static final String PAY_PASSWORD = "payPassword";
182+
public static final String _PASSWORD = "_password";
183+
public static final String _PAY_PASSWORD = "_payPassword";
192184
public static final String OLD_PASSWORD = "oldPassword";
193185
public static final String VERIFY = "verify";
194186

@@ -339,7 +331,8 @@ private JSONObject newVerifyRequest(String phone, String verify) {
339331
}
340332

341333

342-
334+
public static final int LOGIN_TYPE_PASSWORD = 0;//密码登录
335+
public static final int LOGIN_TYPE_VERIFY = 1;//验证码登录
343336
/**用户登录
344337
* @param request 只用String,避免encode后未decode
345338
* @return
@@ -379,7 +372,7 @@ public JSONObject login(@RequestBody String request, HttpSession session) {
379372
new Privacy().setPhone(phone)
380373
)
381374
);
382-
JSONResponse response = new JSONResponse(phoneResponse).getJSONResponse(USER_PRIVACY_);
375+
JSONResponse response = new JSONResponse(phoneResponse).getJSONResponse(PRIVACY_);
383376
if (JSONResponse.isSucceed(response) == false) {
384377
return response;
385378
}
@@ -403,7 +396,7 @@ public JSONObject login(@RequestBody String request, HttpSession session) {
403396

404397
//校验凭证
405398
int type = Integer.valueOf(0 + StringUtil.getNumber(typeString));
406-
if (type == Login.TYPE_PASSWORD) {//password密码登录
399+
if (type == LOGIN_TYPE_PASSWORD) {//password密码登录
407400
response = new JSONResponse(
408401
new Parser(POST_HEAD, true).parseResponse(
409402
new JSONRequest(new Privacy(userId).setPassword(password))
@@ -415,7 +408,7 @@ public JSONObject login(@RequestBody String request, HttpSession session) {
415408
if (JSONResponse.isSucceed(response) == false) {
416409
return response;
417410
}
418-
response = response.getJSONResponse(type == Login.TYPE_PASSWORD ? USER_PRIVACY_ : VERIFY_);
411+
response = response.getJSONResponse(type == LOGIN_TYPE_PASSWORD ? PRIVACY_ : VERIFY_);
419412
if (JSONResponse.isExist(response) == false) {
420413
return Parser.newErrorResult(new ConditionErrorException("账号或密码错误"));
421414
}
@@ -434,7 +427,7 @@ public JSONObject login(@RequestBody String request, HttpSession session) {
434427
session.setAttribute(USER_ID, userId);//用户id
435428
session.setAttribute(TYPE, type);//登录方式
436429
session.setAttribute(USER_, user);//用户
437-
session.setAttribute(USER_PRIVACY_, privacy);//用户隐私信息
430+
session.setAttribute(PRIVACY_, privacy);//用户隐私信息
438431
// session.setMaxInactiveInterval(1*60);//设置session过期时间
439432

440433
return response;
@@ -508,7 +501,7 @@ public JSONObject register(@RequestBody String request) {
508501
new Privacy().setPhone(phone)
509502
)
510503
);
511-
JSONObject checkUser = check == null ? null : check.getJSONObject(USER_PRIVACY_);
504+
JSONObject checkUser = check == null ? null : check.getJSONObject(PRIVACY_);
512505
if (checkUser == null || checkUser.getIntValue(JSONResponse.KEY_COUNT) > 0) {
513506
return Parser.newErrorResult(new ConflictException("手机号" + phone + "已经注册"));
514507
}
@@ -531,7 +524,7 @@ public JSONObject register(@RequestBody String request) {
531524
)
532525
)
533526
);
534-
if (JSONResponse.isSucceed(response.getJSONResponse(USER_PRIVACY_)) == false) {//创建失败,删除新增的无效User和userPrivacy
527+
if (JSONResponse.isSucceed(response.getJSONResponse(PRIVACY_)) == false) {//创建失败,删除新增的无效User和userPrivacy
535528

536529
new Parser(DELETE, true).parseResponse(
537530
new JSONRequest(
@@ -624,26 +617,39 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) {
624617
} catch (Exception e) {
625618
return Parser.newErrorResult(e);
626619
}
620+
JSONObject privacyObj;
621+
long userId;
622+
String payPassword;
623+
double change;
624+
try {
625+
privacyObj = requestObject.getJSONObject(PRIVACY_);
626+
if (privacyObj == null) {
627+
throw new NullPointerException("请设置 " + PRIVACY_ + "!");
628+
}
629+
userId = privacyObj.getLongValue(ID);
630+
payPassword = privacyObj.getString(_PAY_PASSWORD);
631+
change = privacyObj.getDoubleValue("balance+");
632+
633+
if (userId <= 0) {
634+
throw new IllegalArgumentException(PRIVACY_ + "." + ID + ":value 中value不合法!");
635+
}
636+
if (StringUtil.isPassword(payPassword) == false) {
637+
throw new IllegalArgumentException(PRIVACY_ + "." + _PAY_PASSWORD + ":value 中value不合法!");
638+
}
639+
} catch (Exception e) {
640+
return Parser.extendErrorResult(requestObject, e);
641+
}
627642

628643
//验证密码<<<<<<<<<<<<<<<<<<<<<<<
629644

630-
JSONObject pwdObj = requestObject.getJSONObject(PASSWORD_);
631-
requestObject.remove(PASSWORD_);
632-
if (pwdObj == null) {
633-
pwdObj = new JSONRequest();
634-
}
635-
if (pwdObj.getIntValue(TYPE) != Password.TYPE_PAY) {
636-
// return Parser.extendErrorResult(requestObject, new ConditionErrorException("Password type必须是支付类型!"));
637-
pwdObj.put(TYPE, Password.TYPE_PAY);
638-
}
639-
645+
privacyObj.remove("balance+");
640646
JSONResponse response = new JSONResponse(
641647
new Parser(POST_HEAD, true).setSession(session).parseResponse(
642-
new JSONRequest(PASSWORD_, pwdObj)
648+
new JSONRequest(PRIVACY_, privacyObj)
643649
)
644650
);
645-
response = response.getJSONResponse(PASSWORD_);
646-
if (response == null || response.isExist() == false) {
651+
response = response.getJSONResponse(PRIVACY_);
652+
if (JSONResponse.isExist(response) == false) {
647653
return Parser.extendErrorResult(requestObject, new ConditionErrorException("支付密码错误!"));
648654
}
649655

@@ -652,13 +658,6 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) {
652658

653659
//验证金额范围<<<<<<<<<<<<<<<<<<<<<<<
654660

655-
JSONObject wallet = requestObject.getJSONObject(WALLET_);
656-
long id = wallet == null ? 0 : wallet.getLongValue(ID);
657-
if (id <= 0) {
658-
return Parser.extendErrorResult(requestObject, new ConditionErrorException("请设置Wallet及内部的id!"));
659-
}
660-
661-
double change = wallet.getDoubleValue("balance+");
662661
if (change == 0) {
663662
return Parser.extendErrorResult(requestObject, new OutOfRangeException("balance+的值不能为0!"));
664663
}
@@ -672,20 +671,26 @@ public JSONObject putBalance(@RequestBody String request, HttpSession session) {
672671
response = new JSONResponse(
673672
new Parser(POST_GET, true).parseResponse(
674673
new JSONRequest(
675-
new Wallet(id).setUserId(AccessVerifier.getUserId(session))
674+
new Privacy(userId)
676675
)
677676
)
678677
);
679-
Wallet w = response == null ? null : response.getObject(Wallet.class);
680-
if (w == null) {
678+
Privacy privacy = response == null ? null : response.getObject(Privacy.class);
679+
long id = privacy == null ? 0 : BaseModel.value(privacy.getId());
680+
if (id != userId) {
681681
return Parser.extendErrorResult(requestObject, new Exception("服务器内部错误!"));
682682
}
683683

684-
if (w.getBalance() == null || w.getBalance().doubleValue() < -change) {
684+
if (BaseModel.value(privacy.getBalance()) < -change) {
685685
return Parser.extendErrorResult(requestObject, new OutOfRangeException("余额不足!"));
686686
}
687687
}
688688

689+
690+
privacyObj.remove(_PAY_PASSWORD);
691+
privacyObj.put("balance+", change);
692+
requestObject.put(PRIVACY_, privacyObj);
693+
requestObject.put(JSONRequest.KEY_TAG, PRIVACY_);
689694
//不免验证,里面会验证身份
690695
return new Parser(PUT).setSession(session).parseResponse(requestObject);
691696
}

APIJSON-Java-Server/APIJSON-Idea/src/main/java/apijson/demo/server/model/Comment.java

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,56 +18,7 @@
1818

1919
/**评论类
2020
* @author Lemon
21-
* @see
22-
* <br >POST:<pre>
23-
{
24-
"Comment":{
25-
"disallow":"id",
26-
"necessary":"userId,momentId,content"
27-
}
28-
}
29-
* </pre>
3021
*/
3122
@MethodAccess
32-
public class Comment extends BaseModel {
33-
private static final long serialVersionUID = 1L;
34-
35-
private Long toId;
36-
private Long momentId;
37-
private String content;
38-
public Comment() {
39-
super();
40-
}
41-
public Comment(long id) {
42-
this();
43-
setId(id);
44-
}
45-
46-
47-
public Long getToId() {
48-
return toId;
49-
}
50-
public Comment setToId(Long toId) {
51-
this.toId = toId;
52-
return this;
53-
}
54-
public Comment setUserId(Long userId) {
55-
super.setUserId(userId);
56-
return this;
57-
}
58-
public Long getMomentId() {
59-
return momentId;
60-
}
61-
public Comment setMomentId(Long momentId) {
62-
this.momentId = momentId;
63-
return this;
64-
}
65-
public String getContent() {
66-
return content;
67-
}
68-
public Comment setContent(String content) {
69-
this.content = content;
70-
return this;
71-
}
72-
73-
}
23+
public class Comment {
24+
}

0 commit comments

Comments
 (0)