服务器对外接口aop权鉴
1.权鉴规则
public class BeSignUtils {
public static void main(String[] args) {
String secret = "123456";
//参数签名算法测试例子
Map<String, Object> signMap = Maps.newHashMap();
signMap.put("appId","as");
System.out.println("得到签名sign:" + getSign(signMap, secret));
}
/**
* 签名
* @param params
* @param secret
* @return
*/
public static String getSign(Map<String, Object> params, String secret) {
String sign = "";
StringBuilder sb = new StringBuilder();
//step1:先对请求参数排序
Set<String> keyset = params.keySet();
TreeSet<String> sortSet = new TreeSet<String>();
sortSet.addAll(keyset);
Iterator<String> it = sortSet.iterator();
//step2:把参数的key value链接起来 secretkey放在最后面,得到要加密的字符串
while (it.hasNext()) {
String key = it.next();
String value = params.get(key).toString();
sb.append(key).append(value);
}
sb.append(secret);
sign=SecureUtil.md5(sb.toString()).toUpperCase();
LogFactory.get().info(sign);
return sign;
}
private static byte[] getMD5Digest(String data) throws IOException {
byte[] bytes = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
bytes = md.digest(data.getBytes("UTF-8"));
} catch (GeneralSecurityException gse) {
throw new IOException(gse);
}
return bytes;
}
private static String byte2hex(byte[] bytes) {
StringBuilder sign = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() == 1) {
sign.append("0");
}
sign.append(hex.toUpperCase());
}
return sign.toString();
}
}
2.切面
@Aspect
@Component
public class Jurisdiction {
@Autowired
SdModeBeSignService sdModeBeSignService;
/**
* 切入点
*/
@Pointcut("execution(* com..controller.external.be..*.*(..))")
public void methodArgs() {
}
/**
* 获取操作日志说明
*
* @param joinPoint
*/
@Before("methodArgs()")
public void before(JoinPoint joinPoint) {
System.out.println("Aop do!");
Object arg = joinPoint.getArgs()[0];
Map<String, Object> map = BeanUtil.beanToMap(arg);
String sign = map.get("sign").toString();
String appId = map.get("appId").toString();
SdModeBeSign sdModeBeSign = sdModeBeSignService.getOne(new QueryWrapper<SdModeBeSign>().eq("is_deleted", 0)
.eq("appId", appId));
map.remove("sign");
String mineSign = BeSignUtils.getSign(map,sdModeBeSign.getSecret());
if(!Objects.equals(sign, mineSign)) {
throw new ApiException("签名错误");
}
}
}
其他代码示例
1.入参公共父类,做数据完整性校验
public class BePublicDTO {
@NotEmpty(message = "sign-签名必填")
private String sign;
@NotEmpty(message = "appId-必填")
private String appId;
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
public String getAppId() {
return appId;
}
public void setAppId(String 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` int(11) NOT NULL AUTO_INCREMENT,
`create_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`update_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`is_deleted` int(6) NOT NULL DEFAULT 0,
`appid` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '用户名',
`secret` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '密码',
`remark` varchar(255) 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;