如果引入feign,会显得很臃肿,推荐使用 http组件来调用。
调用的时候需要用到认证,你可以通过secure的配置来指定这些接口是需要token还是basic认证还是其他的认证。
配置好之后再用http组件调用即可。
按照该文档方法进行了配置,一直提示
{"msg":"缺失令牌,鉴权失败","code":401,"data":null}
配置如下:
#动态签名认证配置 sign: - method: ALL pattern: /dashboard/sign crypto: "sha1" - method: ALL pattern: /blade-business/taxrawdata/pull crypto: "sha1"
代码如下:
// secure Map<String, String> headers = new HashMap<>(); long timestamp = DateUtil.now().getTime(); String nonce = "job"; String signature = DigestUtil.sha1Hex(timestamp + nonce); headers.put("nonce", nonce); headers.put("timestamp", String.valueOf(timestamp)); headers.put("signature", signature); HttpRequest.get("http://localhost/blade-business/taxrawdata/pull") .log(LogLevel.BODY) .queryMap(queryMap) .cacheControl(CacheControl.FORCE_NETWORK) .addHeader(headers) .execute() .onResponse(responseSpec -> { if (responseSpec.isOk()) return ReturnT.SUCCESS; return new ReturnT(ReturnT.FAIL.getCode(), responseSpec.message()); });
日志:
org.springblade.core.http.Slf4jLogger : --> GET http://localhost/blade-business/taxrawdata/pull?endDate=20210622&startDate=20210619 org.springblade.core.http.Slf4jLogger : signature: 5e138fd57bd5f65faf01954d4740ef43ac3d0a5f org.springblade.core.http.Slf4jLogger : nonce: job org.springblade.core.http.Slf4jLogger : timestamp: 1624332240162 org.springblade.core.http.Slf4jLogger : User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36 org.springblade.core.http.Slf4jLogger : --> END GET
目前接口都能顺利调用通,但一直没用鉴权成功。
我晕,编辑好的咋个成这样~
配置:
#动态签名认证配置
sign:
- method: ALL
pattern: /dashboard/sign
crypto: "sha1"
- method: ALL
pattern: /blade-business/taxrawdata/pull
crypto: "sha1"
代码
// secure
Map<String, String> headers = new HashMap<>();
long timestamp = DateUtil.now().getTime();
String nonce = "223";
String signature = DigestUtil.sha1Hex(timestamp + nonce);
headers.put("nonce", nonce);
headers.put("timestamp", String.valueOf(timestamp));
headers.put("signature", signature);
HttpRequest.get("http://localhost/blade-business/taxrawdata/pull")
.log(LogLevel.BODY)
.queryMap(queryMap)
.cacheControl(CacheControl.FORCE_NETWORK)
.addHeader(headers)
.execute()
.onResponse(responseSpec -> {
if (responseSpec.isOk()) return ReturnT.SUCCESS;
return new ReturnT(ReturnT.FAIL.getCode(), responseSpec.message());
});
日志:
org.springblade.core.http.Slf4jLogger : --> GET http://localhost/blade-business/taxrawdata/pull?endDate=20210622&startDate=20210619
org.springblade.core.http.Slf4jLogger : signature: 5e138fd57bd5f65faf01954d4740ef43ac3d0a5f
org.springblade.core.http.Slf4jLogger : nonce: 223
org.springblade.core.http.Slf4jLogger : timestamp: 1624332240162
org.springblade.core.http.Slf4jLogger : User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
org.springblade.core.http.Slf4jLogger : --> END GET
错误信息
{"msg":"缺失令牌,鉴权失败","code":401,"data":null}
网关默认会对Blade-Auth请求头进行校验
扫一扫访问 Blade技术社区 移动端