
3 changed files with 199 additions and 94 deletions
@ -0,0 +1,71 @@ |
|||
package com.yxt.ss.gateway.api; |
|||
|
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import java.io.UnsupportedEncodingException; |
|||
import java.net.URLEncoder; |
|||
import java.security.MessageDigest; |
|||
import java.security.NoSuchAlgorithmException; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* @description: |
|||
* @author: dimengzhe |
|||
* @date: 2024/12/10 |
|||
**/ |
|||
@Service |
|||
public class ClientService { |
|||
|
|||
/** |
|||
* 生成请求签名 |
|||
* |
|||
* @param parameters 请求参数 |
|||
* @param secret 密钥 |
|||
* @return 签名 |
|||
*/ |
|||
public String generateSignature(Map<String, String> parameters, String secret) throws UnsupportedEncodingException, NoSuchAlgorithmException { |
|||
|
|||
// 2. 拼接参数字符串
|
|||
String content = joinParameters(parameters); |
|||
|
|||
// 3. 将密钥加在参数字符串的前后
|
|||
content = secret + content + secret; |
|||
|
|||
// 4. 计算签名 (MD5)
|
|||
return md5(content); |
|||
} |
|||
|
|||
/** |
|||
* 拼接参数字符串 |
|||
* |
|||
* @param tree 排序后的参数 |
|||
* @return 拼接后的参数字符串 |
|||
*/ |
|||
public String joinParameters(Map<String, String> tree) throws UnsupportedEncodingException { |
|||
StringBuilder builder = new StringBuilder(); |
|||
for (Map.Entry<String, String> entry : tree.entrySet()) { |
|||
if (builder.length() > 0) { |
|||
builder.append("&"); |
|||
} |
|||
builder.append(entry.getKey()).append("="); |
|||
builder.append(URLEncoder.encode(entry.getValue(), "UTF-8")); |
|||
} |
|||
return builder.toString(); |
|||
} |
|||
|
|||
/** |
|||
* 计算 MD5 |
|||
* |
|||
* @param content 要计算 MD5 的字符串 |
|||
* @return MD5 值 |
|||
*/ |
|||
public String md5(String content) throws NoSuchAlgorithmException { |
|||
MessageDigest md = MessageDigest.getInstance("MD5"); |
|||
byte[] bytes = md.digest(content.getBytes()); |
|||
StringBuilder sb = new StringBuilder(); |
|||
for (byte b : bytes) { |
|||
sb.append(String.format("%02x", b)); |
|||
} |
|||
return sb.toString(); |
|||
} |
|||
} |
@ -0,0 +1,128 @@ |
|||
package com.yxt.ss.gateway.api.rest; |
|||
|
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import com.fasterxml.jackson.databind.ObjectMapper; |
|||
import com.yxt.ss.gateway.api.ClientService; |
|||
import com.yxt.ss.gateway.api.utils.ResultBean; |
|||
import com.yxt.ss.gateway.api.utils.SignatureQuery; |
|||
import com.yxt.ss.gateway.api.utils.SignatureUtil; |
|||
import okhttp3.*; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.web.bind.annotation.PostMapping; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
|
|||
import java.io.IOException; |
|||
import java.io.UnsupportedEncodingException; |
|||
import java.security.NoSuchAlgorithmException; |
|||
import java.util.Map; |
|||
import java.util.TreeMap; |
|||
import java.util.concurrent.TimeUnit; |
|||
|
|||
/** |
|||
* @description: |
|||
* @author: dimengzhe |
|||
* @date: 2024/12/10 |
|||
**/ |
|||
@RestController |
|||
@RequestMapping("/client") |
|||
public class ClientRest { |
|||
|
|||
@Autowired |
|||
private ClientService clientService; |
|||
|
|||
|
|||
//appkey
|
|||
static final String APPKEY = "appKey4"; |
|||
//secret
|
|||
static final String SECRET = "secret"; |
|||
|
|||
//开发端,生成签名并调用服务器端验证签名、appKey等值。
|
|||
@PostMapping("/getSign") |
|||
ResultBean getSign(SignatureQuery query) { |
|||
ResultBean<String> rb = ResultBean.fireFail(); |
|||
try { |
|||
Map<String, String> formData = query.getParameters(); |
|||
//使用treeMap排序
|
|||
Map<String, String> tree = new TreeMap<>(formData); |
|||
tree.put("_app", APPKEY); |
|||
tree.put("_t", String.valueOf(System.currentTimeMillis() / 1000)); |
|||
tree.put("_s", ""); |
|||
// 生成签名
|
|||
String sign = clientService.generateSignature(tree, SECRET); |
|||
//添加签名值map
|
|||
tree.put("_sign", sign); |
|||
//发起请求
|
|||
ResultBean resultBean = client(tree); |
|||
if (!resultBean.getSuccess()) { |
|||
return rb.setMsg(resultBean.getMsg()); |
|||
} |
|||
//通过验证继续调用接口
|
|||
|
|||
|
|||
return rb.success(); |
|||
} catch (UnsupportedEncodingException e) { |
|||
return rb.setMsg("Unsupported encoding: " + e.getMessage()); |
|||
} catch (NoSuchAlgorithmException e) { |
|||
return rb.setMsg("Algorithm not found: " + e.getMessage()); |
|||
} |
|||
} |
|||
|
|||
//发起请求验证签名等
|
|||
public ResultBean client(Map<String, String> data) { |
|||
ResultBean rb = ResultBean.fireFail(); |
|||
OkHttpClient client = new OkHttpClient.Builder() |
|||
.connectTimeout(10, TimeUnit.SECONDS) |
|||
.writeTimeout(10, TimeUnit.SECONDS) |
|||
.readTimeout(30, TimeUnit.SECONDS) |
|||
.build(); |
|||
|
|||
try { |
|||
// 构建URL
|
|||
String endPoint = "http://127.0.0.1:9999"; |
|||
String path = "/signature/validate"; |
|||
|
|||
// 创建FormData
|
|||
FormBody.Builder formBuilder = new FormBody.Builder(); |
|||
for (Map.Entry<String, String> entry : data.entrySet()) { |
|||
formBuilder.add(entry.getKey(), entry.getValue()); |
|||
} |
|||
RequestBody formBody = formBuilder.build(); |
|||
|
|||
// 构建POST请求
|
|||
String url = endPoint + path; |
|||
System.out.println("Request URL: " + url); |
|||
System.out.println("Request Data: " + data); |
|||
|
|||
Request request = new Request.Builder() |
|||
.url(url) |
|||
.post(formBody) |
|||
.build(); |
|||
|
|||
// 发送请求
|
|||
try (Response response = client.newCall(request).execute()) { |
|||
String responseBody = response.body().string(); |
|||
// 使用 Jackson 解析 JSON 响应
|
|||
ObjectMapper objectMapper = new ObjectMapper(); |
|||
JsonNode jsonNode = objectMapper.readTree(responseBody); |
|||
String success = jsonNode.path("success").asText(); |
|||
String msg = jsonNode.path("msg").asText(); |
|||
if ("false".equals(success)) { |
|||
return rb.setMsg(msg); |
|||
} |
|||
if (response.isSuccessful()) { |
|||
System.out.println("Response: " + response.body().string()); |
|||
} else { |
|||
System.err.println("Request failed: " + response.message()); |
|||
} |
|||
|
|||
|
|||
} |
|||
} catch (IOException e) { |
|||
System.err.println("Network error: " + e.getMessage()); |
|||
} catch (Exception e) { |
|||
System.err.println("Unexpected error: " + e.getMessage()); |
|||
} |
|||
return rb.success(); |
|||
} |
|||
} |
Loading…
Reference in new issue