|
| 1 | +package zuo.biao.apijson.server.sql; |
| 2 | + |
| 3 | +import java.rmi.AccessException; |
| 4 | + |
| 5 | +import com.alibaba.fastjson.JSONObject; |
| 6 | + |
| 7 | +import zuo.biao.apijson.StringUtil; |
| 8 | + |
| 9 | +/**权限验证类 |
| 10 | + * @author Lemon |
| 11 | + */ |
| 12 | +public class AccessVerifier { |
| 13 | + private static final String TAG = "AccessVerifier: "; |
| 14 | + |
| 15 | + private static final int ACCESS_LOGIN = 1; |
| 16 | + private static final int ACCESS_PAY = 2; |
| 17 | + |
| 18 | + public static final String KEY_CURRENT_USER_ID = "currentUserId"; |
| 19 | + public static final String KEY_LOGIN_PASSWORD = "loginPassword"; |
| 20 | + public static final String KEY_PAY_PASSWORD = "payPassword"; |
| 21 | + |
| 22 | + // public static final String[] LOGIN_ACCESS_TABLE_NAMES = {"Work", "Comment"}; |
| 23 | + public static final String[] PAY_ACCESS_TABLE_NAMES = {"Wallet"}; |
| 24 | + |
| 25 | + /**验证权限是否通过 |
| 26 | + * @param request |
| 27 | + * @param tableName |
| 28 | + * @return |
| 29 | + */ |
| 30 | + public static boolean verify(JSONObject request, String tableName) throws AccessException { |
| 31 | + try { |
| 32 | + verify(request, getAccessId(tableName)); |
| 33 | + } catch (AccessException e) { |
| 34 | + throw new AccessException(TAG + "verify tableName = " + tableName + ", error = " + e.getMessage()); |
| 35 | + } |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + /**验证权限是否通过 |
| 41 | + * @param request |
| 42 | + * @param accessId 可以直接在代码里写ACCESS_LOGIN等,或者建一个Access表,包括id和需要改权限的table的tableName列表 |
| 43 | + * @return |
| 44 | + * @throws AccessException |
| 45 | + */ |
| 46 | + public static boolean verify(JSONObject request, int accessId) throws AccessException { |
| 47 | + if (accessId < 0 || request == null) { |
| 48 | + return true; |
| 49 | + } |
| 50 | + long currentUserId = request.getLongValue(KEY_CURRENT_USER_ID); |
| 51 | + if (currentUserId <= 0) { |
| 52 | + throw new AccessException(TAG + "verify accessId = " + accessId |
| 53 | + + " >> currentUserId <= 0, currentUserId = " + currentUserId); |
| 54 | + } |
| 55 | + String password; |
| 56 | + |
| 57 | + switch (accessId) { |
| 58 | + case ACCESS_LOGIN: |
| 59 | + password = StringUtil.getString(request.getString(KEY_LOGIN_PASSWORD)); |
| 60 | + if (password.equals(StringUtil.getString(getLoginPassword(currentUserId))) == false) { |
| 61 | + throw new AccessException(TAG + "verify accessId = " + accessId |
| 62 | + + " >> currentUserId or loginPassword error" |
| 63 | + + " currentUserId = " + currentUserId + ", loginPassword = " + password); |
| 64 | + } |
| 65 | + case ACCESS_PAY: |
| 66 | + password = StringUtil.getString(request.getString(KEY_PAY_PASSWORD)); |
| 67 | + if (password.equals(StringUtil.getString(getPayPassword(currentUserId))) == false) { |
| 68 | + throw new AccessException(TAG + "verify accessId = " + accessId |
| 69 | + + " >> currentUserId or payPassword error" |
| 70 | + + " currentUserId = " + currentUserId + ", payPassword = " + password); |
| 71 | + } |
| 72 | + default: |
| 73 | + return true; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /**获取权限id |
| 78 | + * @param tableName |
| 79 | + * @return |
| 80 | + */ |
| 81 | + public static int getAccessId(String tableName) { |
| 82 | + if (StringUtil.isNotEmpty(tableName, true) == false) { |
| 83 | + return -1; |
| 84 | + } |
| 85 | + // for (int i = 0; i < LOGIN_ACCESS_TABLE_NAMES.length; i++) { |
| 86 | + // if (tableName.equals(LOGIN_ACCESS_TABLE_NAMES[i])) { |
| 87 | + // return ACCESS_LOGIN; |
| 88 | + // } |
| 89 | + // } |
| 90 | + for (int i = 0; i < PAY_ACCESS_TABLE_NAMES.length; i++) { |
| 91 | + if (tableName.equals(PAY_ACCESS_TABLE_NAMES[i])) { |
| 92 | + return ACCESS_PAY; |
| 93 | + } |
| 94 | + } |
| 95 | + return -1; |
| 96 | + } |
| 97 | + |
| 98 | + /**获取登录密码 |
| 99 | + * @param userId |
| 100 | + * @return |
| 101 | + */ |
| 102 | + public static String getLoginPassword(long userId) { |
| 103 | + // TODO 查询并返回对应userId的登录密码 |
| 104 | + return "123456";//仅测试用 |
| 105 | + } |
| 106 | + |
| 107 | + /**获取支付密码 |
| 108 | + * @param userId |
| 109 | + * @return |
| 110 | + */ |
| 111 | + public static String getPayPassword(long userId) { |
| 112 | + // TODO 查询并返回对应userId的支付密码 |
| 113 | + return "123456";//仅测试用 |
| 114 | + } |
| 115 | + |
| 116 | + /**删除请求里的权限信息 |
| 117 | + * @param requestObject |
| 118 | + * @return |
| 119 | + */ |
| 120 | + public static JSONObject removeAccessInfo(JSONObject requestObject) { |
| 121 | + if (requestObject != null) { |
| 122 | + requestObject.remove(KEY_CURRENT_USER_ID); |
| 123 | + requestObject.remove(KEY_LOGIN_PASSWORD); |
| 124 | + requestObject.remove(KEY_PAY_PASSWORD); |
| 125 | + } |
| 126 | + return requestObject; |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments