做者: "唐甜"spring
sign插件用来对请求进行签名认证的插件markdown
AK / SK(访问密钥ID /秘密访问密钥)即访问密钥,包含访问密钥ID(AK)和秘密访问密钥(SK)两部分,主要用于对用户的调用行为进行鉴权和认证。app
sign
的支持<!-- soul sign plugin start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-sign</artifactId>
<version>${last.version}</version>
</dependency>
<!-- soul sign plugin end-->
复制代码
添加选择器 添加规则器
ide
在本身服务中增长一个对外访问的方法spring-boot
@GetMapping("/authUrl")
public String authUrl() {
Map<String, String> map = Maps.newHashMapWithExpectedSize(2);
//timestamp为毫秒数的字符串形式 String.valueOf(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli())
String timetamp = String.valueOf(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli()) ;
System.out.println(timetamp);
map.put("timestamp",timetamp); //值应该为毫秒数的字符串形式
map.put("path", "/dubbo/findAll");
map.put("version", "1.0.0");
List<String> storedKeys = Arrays.stream(map.keySet()
.toArray(new String[]{}))
.sorted(Comparator.naturalOrder())
.collect(Collectors.toList());
final String sign = storedKeys.stream()
.map(key -> String.join("", key, map.get(key)))
.collect(Collectors.joining()).trim()
.concat("D19CF79F647A465AB9C5C66F430CAD28");//SECRETkey
return DigestUtils.md5DigestAsHex(sign.getBytes()).toUpperCase();
}
复制代码
下面须要注意的 学习
经过的返回 5min超时的返回
appKey填写错误的返回 签名
错误的返回
替换签名插件的返回
ui
SignPlugin插件调用DefaultSignService中signVerify方法判断签名插件是否可用,若是可用获取在全局插件存入的soulContext并调用verify方法lua
if (signData != null && signData.getEnabled()) {
final SoulContext soulContext = exchange.getAttribute(Constants.CONTEXT);
assert soulContext != null;
return verify(soulContext, exchange);
}
复制代码
验证方法中判断请求头信息是否正确若是不正确就抛出log.error(“符号参数不完整,{}”,soulContext)异常url
if (StringUtils.isBlank(soulContext.getAppKey())
|| StringUtils.isBlank(soulContext.getSign())
|| StringUtils.isBlank(soulContext.getTimestamp())) {
log.error("sign parameters are incomplete,{}", soulContext);
return Pair.of(Boolean.FALSE, Constants.SIGN_PARAMS_ERROR);
}
复制代码
判断请求时间是否超时spa
if (between > delay) {
return Pair.of(Boolean.FALSE, String.format(SoulResultEnum.SING_TIME_IS_TIMEOUT.getMsg(), delay));
}
复制代码
没有超时继续调用sign方法获取认证数据,这个数据在soulAdmin中配置
AppAuthData appAuthData = SignAuthDataCache.getInstance().obtainAuthData(soulContext.getAppKey());
复制代码
后面对appAuthData数据进行判断,数据有错误就不经过对获取的参数再次签名,判断假定的和再次签名的是否同样
String sigKey = SignUtils.generateSign(appAuthData.getAppSecret(), buildParamsMap(soulContext));
复制代码
若是都验证都经过就完成了认证访问请求。