Skip to content

Commit 0bffbe0

Browse files
committed
新增table的请求方法限制,取消security schema
1 parent 854cef9 commit 0bffbe0

8 files changed

Lines changed: 72 additions & 54 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package zuo.biao.apijson;
2+
3+
public class Column {
4+
5+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
*/
2020
public enum RequestMethod {
2121

22-
GET, POST, POST_GET, PUT, DELETE
22+
GET, POST_GET, POST, PUT, DELETE
2323
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import org.springframework.boot.SpringApplication;
1818
import org.springframework.boot.autoconfigure.SpringBootApplication;
1919

20+
import zuo.biao.apijson.server.sql.AccessVerifier;
21+
2022
/**application
2123
* @author Lemon
2224
*/
@@ -25,5 +27,7 @@ public class APIJSONApplication {
2527

2628
public static void main(String[] args) {
2729
SpringApplication.run(APIJSONApplication.class, args);
30+
31+
AccessVerifier.init();
2832
}
2933
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@ public class Controller {
3131
public String get(@PathVariable String request) {
3232
return new RequestParser(zuo.biao.apijson.RequestMethod.GET).parse(request);
3333
}
34-
35-
@RequestMapping(value="post", method = RequestMethod.POST)
36-
public String post(@RequestBody String request) {
37-
return new RequestParser(zuo.biao.apijson.RequestMethod.POST).parse(request);
38-
}
39-
34+
4035
/**用POST方法GET数据,request和response都非明文,浏览器看不到,用于对安全性要求高的GET请求
4136
* @param request
4237
* @return
@@ -46,6 +41,11 @@ public String post_get(@RequestBody String request) {
4641
return new RequestParser(zuo.biao.apijson.RequestMethod.POST_GET).parse(request);
4742
}
4843

44+
@RequestMapping(value="post", method = RequestMethod.POST)
45+
public String post(@RequestBody String request) {
46+
return new RequestParser(zuo.biao.apijson.RequestMethod.POST).parse(request);
47+
}
48+
4949
/**以下接口继续用POST接口是为了客户端方便,只需要做get,post请求。也可以改用实际对应的方法。
5050
* post,put方法名可以改为add,update等更客户端容易懂的名称
5151
*/

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ public QueryConfig(RequestMethod method, int count, int page) {
7171
}
7272

7373
public RequestMethod getMethod() {
74-
return method;
75-
}
76-
public QueryConfig setMethod(RequestMethod method) {
7774
if (method == null) {
7875
method = RequestMethod.GET;
7976
}
77+
return method;
78+
}
79+
public QueryConfig setMethod(RequestMethod method) {
8080
this.method = method;
8181
return this;
8282
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Set;
2525
import java.util.regex.Pattern;
2626

27-
import com.alibaba.fastjson.JSONArray;
2827
import com.alibaba.fastjson.JSONObject;
2928

3029
import zuo.biao.apijson.JSON;

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

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
package zuo.biao.apijson.server.sql;
22

3+
import static zuo.biao.apijson.RequestMethod.DELETE;
4+
import static zuo.biao.apijson.RequestMethod.GET;
5+
import static zuo.biao.apijson.RequestMethod.POST;
6+
import static zuo.biao.apijson.RequestMethod.POST_GET;
7+
import static zuo.biao.apijson.RequestMethod.PUT;
8+
39
import java.rmi.AccessException;
10+
import java.util.HashMap;
11+
import java.util.Map;
412

513
import com.alibaba.fastjson.JSONObject;
614

@@ -24,6 +32,20 @@ public class AccessVerifier {
2432
public static final String[] LOGIN_ACCESS_TABLE_NAMES = {"Wallet"};
2533
public static final String[] PAY_ACCESS_TABLE_NAMES = {};
2634

35+
36+
private static Map<String, RequestMethod[]> accessMap;
37+
38+
public static void init() {
39+
accessMap = new HashMap<String, RequestMethod[]>();
40+
accessMap.put("User", RequestMethod.values());
41+
accessMap.put("Moment", RequestMethod.values());
42+
accessMap.put("Comment", RequestMethod.values());
43+
accessMap.put("Wallet", new RequestMethod[]{POST_GET, POST, PUT, DELETE});
44+
accessMap.put("Password", new RequestMethod[]{POST_GET, POST, PUT, DELETE});
45+
accessMap.put("Login", new RequestMethod[]{POST_GET, POST, DELETE});
46+
accessMap.put("Request", new RequestMethod[]{GET, POST_GET});
47+
}
48+
2749
/**验证权限是否通过
2850
* @param request
2951
* @param config
@@ -38,6 +60,10 @@ public static boolean verify(JSONObject request, QueryConfig config) throws Exce
3860
if (request == null) {
3961
return false;
4062
}
63+
RequestMethod method = config.getMethod();
64+
65+
verifyMethod(table, method);
66+
4167

4268
long currentUserId = request.getLongValue(KEY_CURRENT_USER_ID);
4369

@@ -57,7 +83,7 @@ public static boolean verify(JSONObject request, QueryConfig config) throws Exce
5783
break;
5884
}
5985

60-
return verifyAccess(request, table, config.getMethod(), currentUserId);
86+
return verifyAccess(request, table, method, currentUserId);
6187
}
6288

6389
/**
@@ -184,10 +210,27 @@ public static JSONObject removeAccessInfo(JSONObject requestObject) {
184210
}
185211

186212

187-
// public static class Access {
188-
// public static class Get {
189-
// public static final String[] LOGIN_ACCESS_TABLE_NAMES = {"Wallet"};
190-
// }
191-
// }
213+
/**
214+
* @param table
215+
* @param method
216+
* @return
217+
* @throws AccessException
218+
*/
219+
public static boolean verifyMethod(String table, RequestMethod method) throws AccessException {
220+
RequestMethod[] methods = table == null ? null : accessMap.get(table);
221+
if (methods == null) {
222+
throw new AccessException(table + "不允许访问!");
223+
}
224+
if (method == null) {
225+
method = GET;
226+
}
227+
for (int i = 0; i < methods.length; i++) {
228+
if (method == methods[i]) {
229+
return true;
230+
}
231+
}
232+
233+
throw new AccessException(table + "不支持" + method + "方法!");
234+
}
192235

193236
}

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

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import com.alibaba.fastjson.JSONObject;
2727

28-
import zuo.biao.apijson.RequestMethod;
2928
import zuo.biao.apijson.StringUtil;
3029
import zuo.biao.apijson.Table;
3130
import zuo.biao.apijson.server.QueryConfig;
@@ -39,7 +38,6 @@ public class QueryHelper {
3938

4039
private static final String YOUR_MYSQL_URL = "jdbc:mysql://localhost:3306/";//TODO edit to an available one
4140
private static final String YOUR_MYSQL_SCHEMA = "sys";//TODO edit to an available one
42-
private static final String YOUR_MYSQL_SCHEMA_SECURITY = "security";//TODO edit to an available one
4341
private static final String YOUR_MYSQL_ACCOUNT = "root";//TODO edit to an available one
4442
private static final String YOUR_MYSQL_PASSWORD = "apijson";//TODO edit to an available one
4543

@@ -56,16 +54,10 @@ public static synchronized QueryHelper getInstance() {
5654
}
5755

5856
public Connection getConnection() throws Exception {
59-
return getConnection(null);
60-
}
61-
public Connection getConnection(String schema) throws Exception {
62-
if (schema == null) {
63-
schema = YOUR_MYSQL_SCHEMA;
64-
}
6557
//调用Class.forName()方法加载驱动程序
6658
Class.forName("com.mysql.jdbc.Driver");
6759
System.out.println(TAG + "成功加载MySQL驱动!");
68-
return DriverManager.getConnection(YOUR_MYSQL_URL + schema, YOUR_MYSQL_ACCOUNT, YOUR_MYSQL_PASSWORD);
60+
return DriverManager.getConnection(YOUR_MYSQL_URL + YOUR_MYSQL_SCHEMA, YOUR_MYSQL_ACCOUNT, YOUR_MYSQL_PASSWORD);
6961
}
7062

7163

@@ -98,7 +90,7 @@ public JSONObject select(QueryConfig config) throws Exception {
9890

9991
if (connection == null || connection.isClosed()) {
10092
System.out.println(TAG + "select connection " + (connection == null ? " = null" : ("isClosed = " + connection.isClosed()))) ;
101-
connection = getConnection(getSchema(config.getMethod()));
93+
connection = getConnection();
10294
statement = connection.createStatement(); //创建Statement对象
10395
metaData = connection.getMetaData();
10496
}
@@ -181,41 +173,16 @@ public String[] getColumnArray(QueryConfig config) throws SQLException {
181173
return list.toArray(new String[]{});
182174
}
183175

184-
/**
185-
* @param method
186-
* @return
187-
*/
188-
public static String getSchema(RequestMethod method) {
189-
return method == RequestMethod.POST_GET ? YOUR_MYSQL_SCHEMA_SECURITY : YOUR_MYSQL_SCHEMA;
190-
}
191-
192-
/**
193-
* @param table
194-
* @return
195-
* @throws Exception
196-
*/
197-
public int getCount(String table) throws Exception {
198-
return getCount(table, RequestMethod.GET);
199-
}
200-
/**
201-
* @param table
202-
* @param method
203-
* @return
204-
* @throws Exception
205-
*/
206-
public int getCount(String table, RequestMethod method) throws Exception {
207-
return getCount(table, getSchema(method));
208-
}
209176
/**
210177
* @param table
211178
* @param schema
212179
* @return
213180
* @throws Exception
214181
*/
215-
public int getCount(String table, String schema) throws Exception {
182+
public int getCount(String table) throws Exception {
216183
if (connection == null || connection.isClosed()) {
217184
System.out.println(TAG + "getCount connection " + (connection == null ? " = null" : ("isClosed = " + connection.isClosed()))) ;
218-
connection = getConnection(schema);
185+
connection = getConnection();
219186
statement = connection.createStatement(); //创建Statement对象
220187
metaData = connection.getMetaData();
221188
}

0 commit comments

Comments
 (0)