22
33import java .rmi .AccessException ;
44
5- import org .springframework .web .bind .annotation .RequestMethod ;
6-
75import com .alibaba .fastjson .JSONObject ;
86
7+ import zuo .biao .apijson .RequestMethod ;
98import zuo .biao .apijson .StringUtil ;
9+ import zuo .biao .apijson .server .QueryConfig ;
1010
1111/**权限验证类
1212 * @author Lemon
@@ -21,39 +21,65 @@ public class AccessVerifier {
2121 public static final String KEY_LOGIN_PASSWORD = "loginPassword" ;
2222 public static final String KEY_PAY_PASSWORD = "payPassword" ;
2323
24- // public static final String[] LOGIN_ACCESS_TABLE_NAMES = {"Work", "Comment "};
25- public static final String [] PAY_ACCESS_TABLE_NAMES = {"Wallet" };
24+ public static final String [] LOGIN_ACCESS_TABLE_NAMES = {"Wallet " };
25+ public static final String [] PAY_ACCESS_TABLE_NAMES = {};
2626
2727 /**验证权限是否通过
28- * @param method
2928 * @param request
30- * @param tableName
29+ * @param config
3130 * @return
31+ * @throws Exception
3232 */
33- public static boolean verify (RequestMethod method , JSONObject request , String tableName ) throws AccessException {
34- return verify (method , request , getAccessId (tableName ));
35- }
33+ public static boolean verify (JSONObject request , QueryConfig config ) throws Exception {
34+ String table = config == null ? null : config .getTable ();
35+ if (table == null ) {
36+ return true ;
37+ }
38+ if (request == null ) {
39+ return false ;
40+ }
3641
42+ long currentUserId = request .getLongValue (KEY_CURRENT_USER_ID );
3743
38- /**验证权限是否通过
39- * @param method
44+ switch (config .getMethod ()) {
45+ case GET :
46+ case POST_GET :
47+ if ("Wallet" .equals (table ) || "Password" .equals (table )) {
48+ verifyUserId (currentUserId , config );
49+ }
50+ break ;
51+ case POST :
52+ case PUT :
53+ case PATCH :
54+ case DELETE :
55+ verifyUserId (currentUserId , config );
56+ break ;
57+ default :
58+ break ;
59+ }
60+
61+ return verifyAccess (request , table , config .getMethod (), currentUserId );
62+ }
63+
64+ /**
4065 * @param request
41- * @param accessId 可以直接在代码里写ACCESS_LOGIN等,或者建一个Access表,包括id和需要改权限的table的tableName列表
42- * @return
66+ * @param table
67+ * @param method
68+ * @param currentUserId
69+ * @return
4370 * @throws AccessException
4471 */
45- public static boolean verify (RequestMethod method , JSONObject request , int accessId ) throws AccessException {
46- if (accessId < 0 || request == null ) {
72+ private static boolean verifyAccess (JSONObject request , String table , RequestMethod method , long currentUserId ) throws AccessException {
73+ int accessId = getAccessId (method , table );
74+ if (accessId < 0 ) {
4775 return true ;
4876 }
49- long currentUserId = request .getLongValue (KEY_CURRENT_USER_ID );
5077 if (currentUserId <= 0 ) {
5178 System .out .println (TAG + "verify accessId = " + accessId
5279 + " >> currentUserId <= 0, currentUserId = " + currentUserId );
5380 throw new AccessException ("请设置" + KEY_CURRENT_USER_ID + "和对应的password!" );
5481 }
5582 String password ;
56-
5783 switch (accessId ) {
5884 case ACCESS_LOGIN :
5985 password = StringUtil .getString (request .getString (KEY_LOGIN_PASSWORD ));
@@ -63,6 +89,7 @@ public static boolean verify(RequestMethod method, JSONObject request, int acces
6389 + " currentUserId = " + currentUserId + ", loginPassword = " + password );
6490 throw new AccessException (KEY_CURRENT_USER_ID + "或" + KEY_LOGIN_PASSWORD + "错误!" );
6591 }
92+ break ;
6693 case ACCESS_PAY :
6794 password = StringUtil .getString (request .getString (KEY_PAY_PASSWORD ));
6895 if (password .equals (StringUtil .getString (getPayPassword (currentUserId ))) == false ) {
@@ -71,9 +98,36 @@ public static boolean verify(RequestMethod method, JSONObject request, int acces
7198 + " currentUserId = " + currentUserId + ", payPassword = " + password );
7299 throw new AccessException (KEY_CURRENT_USER_ID + "或" + KEY_PAY_PASSWORD + "错误!" );
73100 }
74- default :
75- return true ;
101+ break ;
76102 }
103+ return true ;
104+ }
105+ /**
106+ * @param currentUserId
107+ * @param config
108+ * @return
109+ * @throws Exception
110+ */
111+ private static boolean verifyUserId (long currentUserId , QueryConfig config ) throws Exception {
112+ // if (currentUserId <= 0 || config == null) {
113+ // return true;
114+ // }
115+ // Map<String, Object> where = config.getWhere();
116+ // long userId = 0;
117+ // String table = StringUtil.getString(config.getTable());
118+ // if (where != null) {
119+ // try {
120+ // String key = "User".equals(table) ? Table.ID : Table.USER_ID;
121+ // userId = (long) where.get(key);
122+ // } catch (Exception e) {
123+ // // TODO: handle exception
124+ // }
125+ // }
126+ // if (userId != currentUserId) {
127+ // throw new IllegalArgumentException(table + "的userId和currentUserId不一致!");
128+ // }
129+
130+ return true ;
77131 }
78132
79133
@@ -82,17 +136,17 @@ public static boolean verify(RequestMethod method, JSONObject request, int acces
82136 * @param tableName
83137 * @return
84138 */
85- public static int getAccessId (String tableName ) {
86- if (StringUtil .isNotEmpty (tableName , true ) == false ) {
139+ public static int getAccessId (RequestMethod method , String table ) {
140+ if (StringUtil .isNotEmpty (table , true ) == false ) {
87141 return -1 ;
88142 }
89- // for (int i = 0; i < LOGIN_ACCESS_TABLE_NAMES.length; i++) {
90- // if (tableName .equals(LOGIN_ACCESS_TABLE_NAMES[i])) {
91- // return ACCESS_LOGIN;
92- // }
93- // }
143+ for (int i = 0 ; i < LOGIN_ACCESS_TABLE_NAMES .length ; i ++) {
144+ if (table .equals (LOGIN_ACCESS_TABLE_NAMES [i ])) {
145+ return ACCESS_LOGIN ;
146+ }
147+ }
94148 for (int i = 0 ; i < PAY_ACCESS_TABLE_NAMES .length ; i ++) {
95- if (tableName .equals (PAY_ACCESS_TABLE_NAMES [i ])) {
149+ if (table .equals (PAY_ACCESS_TABLE_NAMES [i ])) {
96150 return ACCESS_PAY ;
97151 }
98152 }
@@ -105,7 +159,7 @@ public static int getAccessId(String tableName) {
105159 */
106160 public static String getLoginPassword (long userId ) {
107161 // TODO 查询并返回对应userId的登录密码
108- return "123456 " ;//仅测试用
162+ return "apijson123 " ;//仅测试用
109163 }
110164
111165 /**获取支付密码
@@ -130,4 +184,11 @@ public static JSONObject removeAccessInfo(JSONObject requestObject) {
130184 return requestObject ;
131185 }
132186
187+
188+ // public static class Access {
189+ // public static class Get {
190+ // public static final String[] LOGIN_ACCESS_TABLE_NAMES = {"Wallet"};
191+ // }
192+ // }
193+
133194}
0 commit comments