|
|
@@ -4,48 +4,104 @@ import com.google.gson.Gson;
|
|
|
import com.zsElectric.boot.common.constant.ConnectivityConstants;
|
|
|
import com.zsElectric.boot.common.util.AESCryptoUtils;
|
|
|
import com.zsElectric.boot.common.util.HmacMD5Util;
|
|
|
+import com.zsElectric.boot.common.util.electric.QueryTokenResponseData;
|
|
|
import com.zsElectric.boot.common.util.electric.RequestParmsEntity;
|
|
|
-import com.zsElectric.boot.common.util.electric.RequestParmsEntitys;
|
|
|
import com.zsElectric.boot.common.util.electric.ResponseParmsEntity;
|
|
|
import com.zsElectric.boot.common.util.electric.queryToken.*;
|
|
|
|
|
|
+import jakarta.annotation.Resource;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
+@Slf4j
|
|
|
@RestController
|
|
|
@RequestMapping("/api/third-party")
|
|
|
public class ThirdPartyAuthController {
|
|
|
|
|
|
+ @Resource
|
|
|
+ private JwtTokenUtil jwtTokenUtil;
|
|
|
|
|
|
@PostMapping("/v1/get")
|
|
|
- @TokenRequired
|
|
|
+// @TokenRequired
|
|
|
public String get() {
|
|
|
return "get";
|
|
|
}
|
|
|
|
|
|
@PostMapping("/query_token")
|
|
|
- public ResponseParmsEntity getToken(@RequestBody RequestParmsEntitys request) throws Exception {
|
|
|
- //todo 验证签名
|
|
|
- if (!HmacMD5Util.verify(request.getOperatorID() + request.getData() + request.getTimeStamp() + request.getSeq(),
|
|
|
- ConnectivityConstants.SIG_SECRET, request.getSig())) {
|
|
|
- return new ResponseParmsEntity()
|
|
|
+ public ResponseParmsEntity getToken(@RequestBody RequestParmsEntity request) throws Exception {
|
|
|
+ ResponseParmsEntity responseParmsEntity = new ResponseParmsEntity();
|
|
|
+ try {
|
|
|
+ //验证签名
|
|
|
+ if (!HmacMD5Util.verify(request.getOperatorID() + request.getData() + request.getTimeStamp() + request.getSeq(),
|
|
|
+ ConnectivityConstants.SIG_SECRET, request.getSig())) {
|
|
|
+ return responseParmsEntity
|
|
|
+ .setRet(4001)
|
|
|
+ .setMsg("签名验证失败")
|
|
|
+ .setData("")
|
|
|
+ .setSig(HmacMD5Util.genSign(responseParmsEntity.getRet(), responseParmsEntity.getMsg(), responseParmsEntity.getData(),
|
|
|
+ ConnectivityConstants.SIG_SECRET));
|
|
|
+ }
|
|
|
+
|
|
|
+ String data = request.getData();
|
|
|
+ String string = AESCryptoUtils.decrypt(data, ConnectivityConstants.DATA_SECRET, ConnectivityConstants.DATA_SECRET_IV);
|
|
|
+ QueryTokenRequestParms queryTokenRequestParms = new Gson().fromJson(string, QueryTokenRequestParms.class);
|
|
|
+ if (queryTokenRequestParms == null || queryTokenRequestParms.getOperatorID() == null || queryTokenRequestParms.getOperatorSecret() == null) {
|
|
|
+ responseParmsEntity
|
|
|
+ .setRet(4003)
|
|
|
+ .setMsg("参数错误")
|
|
|
+ .setData("");
|
|
|
+ responseParmsEntity.setSig(HmacMD5Util.genSign(responseParmsEntity.getRet(), responseParmsEntity.getMsg(), responseParmsEntity.getData(),
|
|
|
+ ConnectivityConstants.SIG_SECRET));
|
|
|
+ return responseParmsEntity;
|
|
|
+ }
|
|
|
+
|
|
|
+ //判断运营商ID与密钥是否正确
|
|
|
+ if (!queryTokenRequestParms.getOperatorID().equals(ConnectivityConstants.PLATFORM_OPERATOR_ID) && !queryTokenRequestParms.getOperatorSecret().equals(ConnectivityConstants.PLATFORM_OPERATOR_SECRET)) {
|
|
|
+ responseParmsEntity
|
|
|
+ .setRet(4004)
|
|
|
+ .setMsg("OperatorID或OperatorSecret错误!")
|
|
|
+ .setData("");
|
|
|
+ responseParmsEntity.setSig(HmacMD5Util.genSign(responseParmsEntity.getRet(), responseParmsEntity.getMsg(), responseParmsEntity.getData(),
|
|
|
+ ConnectivityConstants.SIG_SECRET));
|
|
|
+ }
|
|
|
+
|
|
|
+ //todo redis获取token,不存在则创建
|
|
|
+ String accessToken = jwtTokenUtil.generateToken(queryTokenRequestParms.getOperatorID());
|
|
|
+ Integer remainingTTL = jwtTokenUtil.getRemainingTTL(accessToken).intValue();
|
|
|
+ //构建Data(token信息)
|
|
|
+ QueryTokenResponseData queryTokenResponseData = new QueryTokenResponseData();
|
|
|
+ queryTokenResponseData.setOperatorID(queryTokenRequestParms.getOperatorID());
|
|
|
+ queryTokenResponseData.setAccessToken(accessToken);
|
|
|
+ queryTokenResponseData.setTokenAvailableTime(remainingTTL);
|
|
|
+ queryTokenResponseData.setSuccStat(0);
|
|
|
+ queryTokenResponseData.setFailReason(0);
|
|
|
+
|
|
|
+ log.info("生成token信息:{}", new Gson().toJson(queryTokenResponseData));
|
|
|
+
|
|
|
+ String encodeData = AESCryptoUtils.encrypt(new Gson().toJson(queryTokenResponseData), ConnectivityConstants.PLATFORM_DATA_SECRET,
|
|
|
+ ConnectivityConstants.PLATFORM_DATA_SECRET_IV);
|
|
|
+
|
|
|
+ responseParmsEntity
|
|
|
.setRet(0)
|
|
|
- .setMsg("签名验证失败")
|
|
|
- .setData("")
|
|
|
- .setSig("");
|
|
|
+ .setMsg("成功")
|
|
|
+ .setData(encodeData)
|
|
|
+ .setSig(HmacMD5Util.genSign(responseParmsEntity.getRet(), responseParmsEntity.getMsg(), responseParmsEntity.getData(),
|
|
|
+ ConnectivityConstants.SIG_SECRET));
|
|
|
+ return responseParmsEntity;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("系统错误:{}", e.getMessage());
|
|
|
+ responseParmsEntity
|
|
|
+ .setRet(500)
|
|
|
+ .setMsg("系统错误")
|
|
|
+ .setData("");
|
|
|
+ responseParmsEntity.setSig(HmacMD5Util.genSign(responseParmsEntity.getRet(), responseParmsEntity.getMsg(), responseParmsEntity.getData(),
|
|
|
+ ConnectivityConstants.SIG_SECRET));
|
|
|
+ return responseParmsEntity;
|
|
|
}
|
|
|
-
|
|
|
- String data = request.getData();
|
|
|
- String string = AESCryptoUtils.decrypt(data, ConnectivityConstants.DATA_SECRET, ConnectivityConstants.DATA_SECRET_IV);
|
|
|
- QueryTokenRequestParms queryTokenRequestParms = new Gson().fromJson(string, QueryTokenRequestParms.class);
|
|
|
- return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
// @GetMapping("/token/validate")
|
|
|
// public ResponseEntity<?> validateToken(@RequestHeader("Authorization") String authHeader) {
|
|
|
// if (authHeader == null || !authHeader.startsWith("Bearer ")) {
|