
4 changed files with 14 additions and 188 deletions
@ -1,128 +0,0 @@ |
|||
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.authutils.StringUtils; |
|||
import com.yxt.ss.gateway.api.service.ClientService; |
|||
import com.yxt.ss.gateway.api.utils.AppKeyConfig; |
|||
import com.yxt.ss.gateway.api.utils.ResultBean; |
|||
import com.yxt.ss.gateway.api.utils.SignatureQuery; |
|||
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:服务器对外Api业务接口 |
|||
* @author: dimengzhe |
|||
* @date: 2024/12/10 |
|||
**/ |
|||
@RestController |
|||
@RequestMapping("/ApiTestRest") |
|||
public class ApiTestRest { |
|||
|
|||
@Autowired |
|||
private ClientService clientService; |
|||
|
|||
|
|||
//appkey
|
|||
static final String APPKEY = "appKey4"; |
|||
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