11package 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+
39import java .rmi .AccessException ;
10+ import java .util .HashMap ;
11+ import java .util .Map ;
412
513import 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}
0 commit comments