|
|
@@ -1,5 +1,8 @@
|
|
|
package com.usoftchina.saas.transfers.auth.interceptor;
|
|
|
|
|
|
+import com.usoftchina.saas.account.api.CompanyApi;
|
|
|
+import com.usoftchina.saas.account.dto.CompanyDTO;
|
|
|
+import com.usoftchina.saas.base.Result;
|
|
|
import com.usoftchina.saas.transfers.auth.annotation.IgnoreOpenApiAuth;
|
|
|
import com.usoftchina.saas.transfers.auth.configuration.OpenApiConfig;
|
|
|
import com.usoftchina.saas.utils.StringUtils;
|
|
|
@@ -25,6 +28,9 @@ public class OpenApiAuthInterceptor extends HandlerInterceptorAdapter{
|
|
|
@Autowired
|
|
|
private OpenApiConfig openApiConfig;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private CompanyApi companyApi;
|
|
|
+
|
|
|
// 已使用签名
|
|
|
private Map<String, Long> signatureCache = new ConcurrentHashMap<>();
|
|
|
|
|
|
@@ -44,26 +50,37 @@ public class OpenApiAuthInterceptor extends HandlerInterceptorAdapter{
|
|
|
}
|
|
|
|
|
|
String sign = request.getParameter(openApiConfig.getSignatureParam());
|
|
|
- if (!StringUtils.isEmpty(sign)) {
|
|
|
+ String companyuu = request.getParameter(openApiConfig.getCompanyuu());
|
|
|
+ if (!StringUtils.isEmpty(sign) && StringUtils.isEmpty(companyuu)) {
|
|
|
String urlMessage = request.getRequestURL() + "?"
|
|
|
+ request.getQueryString().substring(0, request.getQueryString().indexOf(openApiConfig.getSignatureParam()) - 1);
|
|
|
|
|
|
logger.info("urlMessage:{}", urlMessage);
|
|
|
String servletPath = request.getServletPath();
|
|
|
logger.info("servletPath:{}", servletPath);
|
|
|
+
|
|
|
boolean check = false;
|
|
|
+ //获取密钥
|
|
|
+ Result<CompanyDTO> companyResult = companyApi.getCompanyByUu(Long.valueOf(companyuu));
|
|
|
+ if (companyResult.isSuccess()) {
|
|
|
+ String localSign = null;
|
|
|
|
|
|
- if (servletPath.indexOf("openapi") > -1) {
|
|
|
- check = sign.equals(HmacUtils.encode(urlMessage));
|
|
|
- }
|
|
|
- if (check) {
|
|
|
- String timestamp = request.getParameter(openApiConfig.getTimestampParam());
|
|
|
- long now = System.currentTimeMillis();
|
|
|
- if (!StringUtils.isEmpty(timestamp) && Math.abs(now - Long.parseLong(timestamp)) <= openApiConfig.getTimeout()
|
|
|
- && !signatureCache.containsKey(sign)) {
|
|
|
- // 加入历史记录
|
|
|
- signatureCache.put(sign, now);
|
|
|
- return true;
|
|
|
+ localSign = companyResult.getData().getAccessKey() == null ? HmacUtils.encode(urlMessage) :
|
|
|
+ HmacUtils.encode(urlMessage, companyResult.getData().getAccessKey());
|
|
|
+
|
|
|
+ if (servletPath.indexOf("openapi") > -1) {
|
|
|
+ check = sign.equals(localSign);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (check) {
|
|
|
+ String timestamp = request.getParameter(openApiConfig.getTimestampParam());
|
|
|
+ long now = System.currentTimeMillis();
|
|
|
+ if (!StringUtils.isEmpty(timestamp) && Math.abs(now - Long.parseLong(timestamp)) <= openApiConfig.getTimeout()
|
|
|
+ && !signatureCache.containsKey(sign)) {
|
|
|
+ // 加入历史记录
|
|
|
+ signatureCache.put(sign, now);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|