forked from Tencent/APIJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoVerifier.java
More file actions
executable file
·223 lines (176 loc) · 7.43 KB
/
DemoVerifier.java
File metadata and controls
executable file
·223 lines (176 loc) · 7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package apijson.demo.server;
import static apijson.demo.server.Controller.ACCESS_;
import java.rmi.ServerException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import zuo.biao.apijson.JSON;
import zuo.biao.apijson.JSONResponse;
import zuo.biao.apijson.Log;
import zuo.biao.apijson.NotNull;
import zuo.biao.apijson.RequestMethod;
import zuo.biao.apijson.RequestRole;
import zuo.biao.apijson.StringUtil;
import zuo.biao.apijson.server.AbstractVerifier;
import zuo.biao.apijson.server.JSONRequest;
import zuo.biao.apijson.server.Visitor;
/**权限验证器
* @author Lemon
*/
public class DemoVerifier extends AbstractVerifier<Long> {
private static final String TAG = "DemoVerifier";
// 由 init 方法读取数据库 Access 表来替代手动输入配置
// // <TableName, <METHOD, allowRoles>>
// // <User, <GET, [OWNER, ADMIN]>>
// static { //注册权限
// ACCESS_MAP.put(User.class.getSimpleName(), getAccessMap(User.class.getAnnotation(MethodAccess.class)));
// ACCESS_MAP.put(Privacy.class.getSimpleName(), getAccessMap(Privacy.class.getAnnotation(MethodAccess.class)));
// ACCESS_MAP.put(Moment.class.getSimpleName(), getAccessMap(Moment.class.getAnnotation(MethodAccess.class)));
// ACCESS_MAP.put(Comment.class.getSimpleName(), getAccessMap(Comment.class.getAnnotation(MethodAccess.class)));
// ACCESS_MAP.put(Verify.class.getSimpleName(), getAccessMap(Verify.class.getAnnotation(MethodAccess.class)));
// ACCESS_MAP.put(Login.class.getSimpleName(), getAccessMap(Login.class.getAnnotation(MethodAccess.class)));
// }
/**初始化,加载所有权限配置
* @return
* @throws ServerException
*/
public static JSONObject init() throws ServerException {
return init(false);
}
/**初始化,加载所有权限配置
* @param shutdownWhenServerError
* @return
* @throws ServerException
*/
public static JSONObject init(boolean shutdownWhenServerError) throws ServerException {
JSONRequest request = new JSONRequest();
{ //Access[]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
JSONRequest accessItem = new JSONRequest();
{ //Access<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
JSONRequest access = new JSONRequest();
accessItem.put(ACCESS_, access);
} //Access>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
request.putAll(accessItem.toArray(0, 0, ACCESS_));
} //Access[]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
JSONObject response = new DemoParser(RequestMethod.GET, true).parseResponse(request);
if (JSONResponse.isSuccess(response) == false) {
Log.e(TAG, "\n\n\n\n\n !!!! 查询权限配置异常 !!!\n" + response.getString(JSONResponse.KEY_MSG) + "\n\n\n\n\n");
onServerError("查询权限配置异常 !", shutdownWhenServerError);
}
JSONArray list = response.getJSONArray(ACCESS_ + "[]");
if (list == null || list.isEmpty()) {
Log.w(TAG, "init list == null || list.isEmpty(),没有可用的权限配置");
throw new NullPointerException("没有可用的权限配置");
}
Log.d(TAG, "init < for ACCESS_MAP.size() = " + ACCESS_MAP.size() + " <<<<<<<<<<<<<<<<<<<<<<<<");
ACCESS_MAP.clear();
JSONObject item;
for (int i = 0; i < list.size(); i++) {
item = list.getJSONObject(i);
if (item == null) {
continue;
}
Map<RequestMethod, RequestRole[]> map = new HashMap<>();
map.put(RequestMethod.GET, JSON.parseObject(item.getString("get"), RequestRole[].class));
map.put(RequestMethod.HEAD, JSON.parseObject(item.getString("head"), RequestRole[].class));
map.put(RequestMethod.GETS, JSON.parseObject(item.getString("gets"), RequestRole[].class));
map.put(RequestMethod.HEADS, JSON.parseObject(item.getString("heads"), RequestRole[].class));
map.put(RequestMethod.POST, JSON.parseObject(item.getString("post"), RequestRole[].class));
map.put(RequestMethod.PUT, JSON.parseObject(item.getString("put"), RequestRole[].class));
map.put(RequestMethod.DELETE, JSON.parseObject(item.getString("delete"), RequestRole[].class));
String name = item.getString("name");
String alias = item.getString("alias");
/**TODO
* 以下判断写得比较复杂,因为表设计不够好,但为了兼容旧版 APIJSON 服务 和 APIAuto 工具而保留了下来。
* 如果是 name 为接口传参的 表对象 的 key,对应一个可缺省的 tableName,判断就会简单不少。
*/
if (StringUtil.isEmpty(name, true)) {
onServerError("字段 name 的值不能为空!", shutdownWhenServerError);
}
if (StringUtil.isEmpty(alias, true)) {
if (JSONRequest.isTableKey(name) == false) {
onServerError("name: " + name + "不合法!字段 alias 的值为空时,name 必须为合法表名!", shutdownWhenServerError);
}
if (Log.DEBUG || item.getBooleanValue("debug") == false) {
ACCESS_MAP.put(name, map);
}
}
else {
if (JSONRequest.isTableKey(alias) == false) {
onServerError("alias: " + alias + "不合法!字段 alias 的值只能为 空 或者 合法表名!", shutdownWhenServerError);
}
if (Log.DEBUG || item.getBooleanValue("debug") == false) {
ACCESS_MAP.put(alias, map);
}
}
DemoSQLConfig.TABLE_KEY_MAP.put(alias, name);
}
Log.d(TAG, "init for /> ACCESS_MAP.size() = " + ACCESS_MAP.size() + " >>>>>>>>>>>>>>>>>>>>>>>");
return response;
}
private static void onServerError(String msg, boolean shutdown) throws ServerException {
Log.e(TAG, "\n权限配置文档测试未通过!\n请修改 Access 表里的记录!\n保证前端看到的权限配置文档是正确的!!!\n\n原因:\n" + msg);
if (shutdown) {
System.exit(1);
} else {
throw new ServerException(msg);
}
}
@NotNull
@Override
public DemoParser createParser() {
DemoParser parser = new DemoParser();
parser.setVisitor(visitor);
return parser;
}
/**登录校验
* @author
* @modifier Lemon
* @param session
* @throws Exception
*/
public static void verifyLogin(HttpSession session) throws Exception {
Log.d(TAG, "verifyLogin session.getId() = " + (session == null ? null : session.getId()));
new DemoVerifier().setVisitor(getVisitor(session)).verifyLogin();
}
/**获取来访用户的id
* @author Lemon
* @param session
* @return
*/
public static long getVisitorId(HttpSession session) {
if (session == null) {
return 0;
}
Long id = (Long) session.getAttribute(Controller.USER_ID);
if (id == null) {
Visitor<Long> v = getVisitor(session);
id = v == null ? 0 : value(v.getId());
session.setAttribute(Controller.USER_ID, id);
}
return value(id);
}
/**获取来访用户
* @param session
* @return
*/
@SuppressWarnings("unchecked")
public static Visitor<Long> getVisitor(HttpSession session) {
return session == null ? null : (Visitor<Long>) session.getAttribute(Controller.USER_);
}
public static long value(Long v) {
return v == null ? 0 : v;
}
}