74 lines
3.4 KiB
Java
74 lines
3.4 KiB
Java
|
package tqq9.lc123.cloud.app.api.utils;
|
|||
|
|
|||
|
import com.alibaba.fastjson.JSON;
|
|||
|
import com.alibaba.fastjson.JSONObject;
|
|||
|
import kd.bos.dataentity.entity.DynamicObject;
|
|||
|
import kd.bos.logging.Log;
|
|||
|
import kd.bos.logging.LogFactory;
|
|||
|
import kd.bos.orm.query.QCP;
|
|||
|
import kd.bos.orm.query.QFilter;
|
|||
|
import kd.bos.servicehelper.BusinessDataServiceHelper;
|
|||
|
import org.apache.commons.lang3.StringUtils;
|
|||
|
import org.springframework.http.HttpEntity;
|
|||
|
import org.springframework.http.HttpHeaders;
|
|||
|
import org.springframework.http.HttpMethod;
|
|||
|
import org.springframework.http.ResponseEntity;
|
|||
|
import org.springframework.web.client.RestTemplate;
|
|||
|
import org.springframework.web.util.UriComponentsBuilder;
|
|||
|
|
|||
|
import javax.json.JsonObject;
|
|||
|
import java.net.ConnectException;
|
|||
|
import java.util.Base64;
|
|||
|
import java.util.HashMap;
|
|||
|
|
|||
|
public class HttpClient {
|
|||
|
private static String URL;
|
|||
|
private static String USERNAME;
|
|||
|
private static String PASSWORD;
|
|||
|
static {
|
|||
|
DynamicObject URL = BusinessDataServiceHelper.loadSingle("tqq9_thirdconfig", "name",
|
|||
|
new QFilter[]{new QFilter("number", QCP.equals, "FinanceHub_Token_Url")});
|
|||
|
DynamicObject userName = BusinessDataServiceHelper.loadSingle("tqq9_thirdconfig", "name",
|
|||
|
new QFilter[]{new QFilter("number", QCP.equals, "FinanceHub_Token_Username ")});
|
|||
|
USERNAME = userName != null ? userName.getString("rbkj_value") : null;
|
|||
|
DynamicObject passWord = BusinessDataServiceHelper.loadSingle("tqq9_thirdconfig", "name",
|
|||
|
new QFilter[]{new QFilter("number", QCP.equals, "FinanceHub_Token_Password")});
|
|||
|
PASSWORD = passWord != null ? passWord.getString("rbkj_value") : null;
|
|||
|
}
|
|||
|
|
|||
|
private static RestTemplate restTemplate = new RestTemplate();
|
|||
|
private final static Log logger = LogFactory.getLog(HttpClient.class);
|
|||
|
|
|||
|
public static ResponseEntity doPost(String url, Object body,String token_name ,String token_access) throws ConnectException {
|
|||
|
HttpHeaders headers = new HttpHeaders();
|
|||
|
if (StringUtils.isNotBlank(token_access)) {
|
|||
|
headers.set(token_name, token_access);
|
|||
|
}
|
|||
|
headers.set("Content-Type", "application/json;charset=UTF-8");
|
|||
|
headers.set("Accept", "application/json;charset=UTF-8");
|
|||
|
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url);
|
|||
|
HttpEntity<String> requestEntity = new HttpEntity(JSON.toJSONString(body), headers);
|
|||
|
ResponseEntity<String> responseEntity = restTemplate.exchange(builder.build().toString(), HttpMethod.POST, requestEntity, String.class);
|
|||
|
return responseEntity;
|
|||
|
}
|
|||
|
public static String Bearer_Token() throws ConnectException {
|
|||
|
// 创建 Basic Auth 认证头
|
|||
|
String auth = USERNAME + ":" + PASSWORD;
|
|||
|
|
|||
|
// 使用 Base64 编码进行认证
|
|||
|
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes());
|
|||
|
|
|||
|
// 创建请求头
|
|||
|
// Authorization:Basic encodedAuth
|
|||
|
HttpHeaders headers = new HttpHeaders();
|
|||
|
headers.set("Authorization", "Basic " + encodedAuth);
|
|||
|
HashMap<String, String> body = new HashMap<>();
|
|||
|
body.put("sid","ERP");
|
|||
|
ResponseEntity responseEntity = doPost(URL, body, null, null);
|
|||
|
Object responseEntityBody = responseEntity.getBody();
|
|||
|
JSONObject jsonObject1 = JSON.parseObject(responseEntity.getBody().toString());
|
|||
|
String accessToken = jsonObject1.get("accessToken").toString();
|
|||
|
return accessToken;
|
|||
|
}
|
|||
|
}
|