Commit d4183dde by 黄超华

云中鹤获取accessToken接口修改

1 parent 5cf40c8e
......@@ -34,6 +34,10 @@
<artifactId>platform-auth-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>
<build>
......
......@@ -2,16 +2,24 @@ package cn.com.glsx.auth.modules.controller;
import cn.com.glsx.auth.config.ApiGatewayServiceConfig;
import cn.com.glsx.auth.model.GatewayService;
import cn.com.glsx.auth.modules.request.CraneQueryAccessTokenRequest;
import cn.com.glsx.auth.modules.utils.SM3Utils;
import cn.com.glsx.auth.utils.GatewayJwtUtils;
import com.alibaba.fastjson.JSON;
import com.glsx.plat.common.utils.DateUtils;
import com.glsx.plat.common.utils.MD5Util;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.annotation.ResponseResultBody;
import com.glsx.plat.core.web.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
......@@ -25,7 +33,7 @@ import java.util.Map;
*/
@Slf4j
@RestController
@RequestMapping(value = "/auth")
@RequestMapping(value = "/open/api")
@ResponseResultBody
public class AuthController {
......@@ -35,19 +43,52 @@ public class AuthController {
@Autowired
private GatewayJwtUtils gatewayJwtUtils;
@GetMapping("/access-token/get")
public Object getAccessToken(HttpServletRequest request) {
String appKey = request.getHeader("appKey");
GatewayService gatewayService = apiGatewayServiceConfig.getServiceByAppKey(appKey);
@PostMapping("/access/queryAccessToken")
public Object queryAccessToken(@Valid @RequestBody CraneQueryAccessTokenRequest request) {
try {
GatewayService gatewayService = verrfyRequestParam(request);
//生成token返回
Map<String, Object> userMap = new HashMap<>();
userMap.put("name", gatewayService.getName());
String token = gatewayJwtUtils.createToken(userMap, gatewayService);
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("token", token);
resultMap.put("expiresAt", DateUtils.format(DateUtils.addDateDays(new Date(), 1), DateUtils.NORMAL_DATE_TIME_PATTERN));
return R.ok(resultMap);
}catch (Exception e){
return R.error(e.getMessage());
}
}
private GatewayService verrfyRequestParam(CraneQueryAccessTokenRequest request){
GatewayService gatewayService = apiGatewayServiceConfig.getServiceByAppKey(request.getAppKey());
if(gatewayService == null){
return R.error("appKey错误");
throw new RuntimeException("请求参数校验不通过");
}
//验证时间戳
long diff = System.currentTimeMillis() - Long.parseLong(request.getTimestamp());
if(diff < 0 || diff > 5 * 60 * 1000L){
throw new RuntimeException("请求参数timestamp不在系统允许的范围内");
}
//验证secret
if("SM3".equals(request.getGrantType())){
if(!SM3Utils.verify(gatewayService.getSecret(), request.getAppSecret(), gatewayService.getAppKey())){
throw new RuntimeException("请求参数校验不通过");
}
}else{
if(!request.getAppSecret().equals(MD5Util.convert32(gatewayService.getSecret()).toLowerCase())){
throw new RuntimeException("请求参数校验不通过");
}
}
//验证签名
String signedParams = gatewayService.getAppKey() + gatewayService.getSecret() + request.getTimestamp() + gatewayService.getSecret();
String sign = MD5Util.convert32(signedParams).toLowerCase();
if("SM3".equals(request.getGrantType())){
sign = SM3Utils.encrypt(signedParams, gatewayService.getAppKey());
}
if(!sign.equals(request.getSign())){
throw new RuntimeException("签名错误");
}
Map<String, Object> userMap = new HashMap<>();
userMap.put("name", gatewayService.getName());
String token = gatewayJwtUtils.createToken(userMap, gatewayService);
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("token", token);
// resultMap.put("expiresAt", )
return R.ok(resultMap);
return gatewayService;
}
}
package cn.com.glsx.auth.modules.request;
import lombok.Data;
import javax.validation.constraints.NotBlank;
/**
* @author huangch
* @version V1.0
* @Title: CraneQueryAccessTokenRequest
* @Description:
* @Company: Didihu.com.cn
* @Date 2023/4/13 15:31
*/
@Data
public class CraneQueryAccessTokenRequest {
@NotBlank(message = "appKey不能为空")
private String appKey;
@NotBlank(message = "appSecret不能为空")
private String appSecret;
@NotBlank(message = "sign不能为空")
private String sign;
@NotBlank(message = "timestamp不能为空")
private String timestamp;
private String grantType;
}
package cn.com.glsx.auth.modules.utils;
import com.glsx.plat.common.utils.MD5Util;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import java.io.UnsupportedEncodingException;
import java.security.Security;
import java.util.Arrays;
public class SM3Utils {
private static final String ENCODING = "UTF-8";
static {
Security.addProvider(new BouncyCastleProvider());
}
/**
* sm3 算法加密
*
* @param paramStr 待加密的参数字符串
* @param appKey 加密的密钥 appKey(接口访问秘钥 Key)
* @return 返回 SM3 加密后固定长度为 32 的 16 进制密内容文字符串
* @explain
*/
public static String encrypt(String paramStr, String appKey) {
// 将返回的 hash 值转换成 16 进制字符串
String resultHexStr = "";
try {
// 将字符串转换成 byte 数组
byte[] srcData = paramStr.getBytes(ENCODING);
// 调用 hash()
byte[] resultHash = hmac(srcData, appKey.getBytes(ENCODING));
// 将返回的 hash 值转换成 16 进制字符串
resultHexStr = ByteUtils.toHexString(resultHash);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return resultHexStr;
}
/**
* 通过密钥进行哈希
*
* @param appKey 加密的密钥 appKey 的 byte 数组
* @param srcData 被加密参数字符串的 byte 数组
* @return
*/
public static byte[] hmac(byte[] appKey, byte[] srcData) {
KeyParameter keyParameter = new KeyParameter(appKey);
SM3Digest digest = new SM3Digest();
HMac mac = new HMac(digest);
mac.init(keyParameter);
mac.update(srcData, 0, srcData.length);
byte[] result = new byte[mac.getMacSize()];
mac.doFinal(result, 0);
return result;
}
/**
* 第 8 页 共 105 页
* 第 9 页 共 105 页
* 判断待加密的原始数据与加密后的数据是否一致
*
* @param paramStr 待加密的参数字符串
* @param resultHexStr SM3 加密后固定长度为 32 的 16 进制密内容文字符串
* @param appKey 加密的密钥 appKey 的 byte 数组
* @return 校验结果
*/
public static boolean verify(String paramStr, String resultHexStr, String appKey) {
boolean flag = false;
try {
byte[] srcData = paramStr.getBytes(ENCODING);
byte[] sm3Hash = ByteUtils.fromHexString(resultHexStr);
byte[] newHash = hmac(srcData, appKey.getBytes(ENCODING));
if (Arrays.equals(newHash, sm3Hash)) {
flag = true;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return flag;
}
public static void main(String[] args) {
// // 待加密的待加密的参数字符串
// String paramStr = "6bba9d97c252661067758b2b6f80e7c558e562b8";
// // 加密的密钥 appKey
// String appKey = "RaOF35Kw";
// // sm3 算法加密
// String resultHexStr = SM3Utils.encrypt(paramStr, appKey);
// System.out.println("待加密的待加密的参数字符串:" + paramStr);
// System.out.println("SM3 加密后的密文字符串内容:" + resultHexStr);
// System.out.println("校验结果:" + SM3Utils.verify(paramStr, resultHexStr, appKey));
System.out.println(System.currentTimeMillis());
System.out.println(MD5Util.convert32("e7794d5173c29275ed74f3e8fc2d9d8171d3d7d3a8953f27").toLowerCase());
}
}
\ No newline at end of file
#项目名称
spring.application.name=platform-auth
server.servlet.context-path=/auth
server.servlet.context-path=/
server.port=8081
#日志路径(logback-spring.xml读取,不要加入配置中心)
logging.path=/data/java_log/jetty/${spring.application.name}/${spring.application.name}.log
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!