feat: PDM对接接口增加basicAuth
This commit is contained in:
+48
-4
@@ -52,12 +52,16 @@ import org.apache.shiro.SecurityUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -132,6 +136,12 @@ public class SrmsApiServiceImpl implements SrmsApiService {
|
||||
@Value("${bindPart.part_belong_url}")
|
||||
private String partBelongUrl;
|
||||
|
||||
@Value("${bindPart.auth_username}")
|
||||
private String authUsername;
|
||||
|
||||
@Value("${bindPart.auth_password}")
|
||||
private String authPassword;
|
||||
|
||||
private static final String REQUESTS = "Requests";
|
||||
|
||||
private static final String MESSAGE_HANDLER = "MessageHeader";
|
||||
@@ -783,6 +793,18 @@ public class SrmsApiServiceImpl implements SrmsApiService {
|
||||
return messageHeader;
|
||||
}
|
||||
|
||||
// 创建 HttpHeaders 并设置 Basic Auth
|
||||
private HttpHeaders getBasicAuthHeaders() {
|
||||
// 创建 HttpHeaders 并设置 Basic Auth
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
String auth = authUsername + ":" + authPassword; // 用户名:密码
|
||||
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.US_ASCII));
|
||||
String authHeader = "Basic " + new String(encodedAuth);
|
||||
headers.set("Authorization", authHeader);
|
||||
return headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validPartNoEdit(@NotNull String partNo, String partName, @NotNull String bindUserInfo) {
|
||||
try {
|
||||
@@ -796,8 +818,19 @@ public class SrmsApiServiceImpl implements SrmsApiService {
|
||||
requests.put("Remark2", "");
|
||||
requests.put("Remark3", "");
|
||||
all.put(REQUESTS, requests);
|
||||
JSONObject reData = restTemplate.postForObject(pdmIp + editValidUrl, all, JSONObject.class);
|
||||
log.info(JSON.toJSONString(reData));
|
||||
|
||||
// 创建 HttpEntity 包含请求体和头信息
|
||||
HttpHeaders headers = this.getBasicAuthHeaders();
|
||||
HttpEntity<String> entity = new HttpEntity<>(all.toString(), headers);
|
||||
// 打印完整的请求报文信息
|
||||
log.info("发送验证零部件是否可修改请求");
|
||||
log.info("IP: " + pdmIp + editValidUrl);
|
||||
log.info("Headers: " + headers.toString());
|
||||
log.info("Request body: " + all.toString());
|
||||
|
||||
JSONObject reData = restTemplate.postForObject(pdmIp + editValidUrl, entity, JSONObject.class);
|
||||
log.info("接收验证零部件是否可修改请求返回");
|
||||
log.info("Response: " + JSON.toJSONString(reData));
|
||||
if (!Objects.isNull(reData) && reData.containsKey("Success")) {
|
||||
String success = reData.getString("Success");
|
||||
if ("true".equals(success)) {
|
||||
@@ -829,8 +862,19 @@ public class SrmsApiServiceImpl implements SrmsApiService {
|
||||
requests.put("Remark2", "");
|
||||
requests.put("Remark3", "");
|
||||
all.put(REQUESTS, requests);
|
||||
JSONObject reData = restTemplate.postForObject(pdmIp + partBelongUrl, all, JSONObject.class);
|
||||
log.info(JSON.toJSONString(reData));
|
||||
|
||||
// 创建 HttpEntity 包含请求体和头信息
|
||||
HttpHeaders headers = this.getBasicAuthHeaders();
|
||||
HttpEntity<String> entity = new HttpEntity<>(all.toString(), headers);
|
||||
// 打印完整的请求报文信息
|
||||
log.info("发送校验某个零部件是否属于某个零部件分类请求");
|
||||
log.info("IP: " + pdmIp + editValidUrl);
|
||||
log.info("Headers: " + headers.toString());
|
||||
log.info("Request body: " + all.toString());
|
||||
|
||||
JSONObject reData = restTemplate.postForObject(pdmIp + editValidUrl, entity, JSONObject.class);
|
||||
log.info("接收校验某个零部件是否属于某个零部件分类返回");
|
||||
log.info("Response: " + JSON.toJSONString(reData));
|
||||
if (!Objects.isNull(reData) && reData.containsKey("Success")) {
|
||||
String success = reData.getString("Success");
|
||||
if ("true".equals(success)) {
|
||||
|
||||
Reference in New Issue
Block a user