服务器对外接口aop权鉴
1.权鉴规则
public class BeSignUtils {public static void mainString[] args) {String secret = "123456";//参数签名算法测试例子Map<String, Object> signMap = Maps.newHashMap);signMap.put"appId","as");System.out.println"得到签名sign:" + getSignsignMap, secret));}/*** 签名* @param params* @param secret* @return*/public static String getSignMap<String, Object> params, String secret) {String sign = "";StringBuilder sb = new StringBuilder);//step1:先对请求参数排序Set<String> keyset = params.keySet);TreeSet<String> sortSet = new TreeSet<String>);sortSet.addAllkeyset);Iterator<String> it = sortSet.iterator);//step2:把参数的key value链接起来 secretkey放在最后面,得到要加密的字符串while it.hasNext)) {String key = it.next);String value = params.getkey).toString);sb.appendkey).appendvalue);}sb.appendsecret);sign=SecureUtil.md5sb.toString)).toUpperCase);LogFactory.get).infosign);return sign;}private static byte[] getMD5DigestString data) throws IOException {byte[] bytes = null;try {MessageDigest md = MessageDigest.getInstance"MD5");bytes = md.digestdata.getBytes"UTF-8"));} catch GeneralSecurityException gse) {throw new IOExceptiongse);}return bytes;}private static String byte2hexbyte[] bytes) {StringBuilder sign = new StringBuilder);for int i = 0; i < bytes.length; i++) {String hex = Integer.toHexStringbytes[i] & 0xFF);if hex.length) == 1) {sign.append"0");}sign.appendhex.toUpperCase));}return sign.toString);}}
2.切面
@Aspect
@Component
public class Jurisdiction {@AutowiredSdModeBeSignService sdModeBeSignService;/*** 切入点*/@Pointcut"execution* com..controller.external.be..*.*..))")public void methodArgs) {}/*** 获取操作日志说明** @param joinPoint*/@Before"methodArgs)")public void beforeJoinPoint joinPoint) {System.out.println"Aop do!");Object arg = joinPoint.getArgs)[0];Map<String, Object> map = BeanUtil.beanToMaparg);String sign = map.get"sign").toString);String appId = map.get"appId").toString);SdModeBeSign sdModeBeSign = sdModeBeSignService.getOnenew QueryWrapper<SdModeBeSign>).eq"is_deleted", 0).eq"appId", appId));map.remove"sign");String mineSign = BeSignUtils.getSignmap,sdModeBeSign.getSecret));if!Objects.equalssign, mineSign)) {throw new ApiException"签名错误");}}}
其他代码示例
1.入参公共父类,做数据完整性校验
public class BePublicDTO {@NotEmptymessage = "sign-签名必填")private String sign;@NotEmptymessage = "appId-必填")private String appId;public String getSign) {return sign;}public void setSignString sign) {this.sign = sign;}public String getAppId) {return appId;}public void setAppIdString appId) {this.appId = appId;}}
2.用户表数据
-- ----------------------------
-- Table structure for sd_mode_be_sign
-- ----------------------------
DROP TABLE IF EXISTS `sd_mode_be_sign`;
CREATE TABLE `sd_mode_be_sign` `id` int11) NOT NULL AUTO_INCREMENT,`create_time` datetime0) NOT NULL DEFAULT CURRENT_TIMESTAMP0),`update_time` datetime0) NOT NULL DEFAULT CURRENT_TIMESTAMP0),`is_deleted` int6) NOT NULL DEFAULT 0,`appid` varchar255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '用户名',`secret` varchar255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '密码',`remark` varchar255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备注',PRIMARY KEY `id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of sd_mode_be_sign
-- ----------------------------
INSERT INTO `sd_mode_be_sign` VALUES 1, '2021-09-23 09:50:01', '2021-09-23 09:50:01', 0, 'as', '123456', '测试');
INSERT INTO `sd_mode_be_sign` VALUES 2, '2021-09-23 15:23:07', '2021-09-23 15:23:07', 0, 'WisdomTrashCan', '94f886bc2e992611e4295d011d7eba32', '智慧垃圾桶投放推送');SET FOREIGN_KEY_CHECKS = 1;