diff --git a/base-buildpackage/src/main/java/com/hzya/frame/finance/bip/controller/BipApiController.java b/base-buildpackage/src/main/java/com/hzya/frame/finance/bip/controller/BipApiController.java index 07c72545..765bff94 100644 --- a/base-buildpackage/src/main/java/com/hzya/frame/finance/bip/controller/BipApiController.java +++ b/base-buildpackage/src/main/java/com/hzya/frame/finance/bip/controller/BipApiController.java @@ -2,7 +2,10 @@ package com.hzya.frame.finance.bip.controller; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; +import com.alibaba.fastjson.JSON; +import com.hzya.frame.finance.bip.entity.BIPTokenResVO; import com.hzya.frame.finance.bip.service.IBipApiService; +import com.hzya.frame.voucher.utils.ZTResult; import com.hzya.frame.web.action.DefaultController; import com.hzya.frame.web.entity.JsonResultEntity; import org.springframework.beans.factory.annotation.Autowired; @@ -24,13 +27,12 @@ public class BipApiController extends DefaultController { public JsonResultEntity getToken() { try { String bipToken = bipApiService.getToekn(); - JSONObject jsonObject = JSONUtil.parseObj(bipToken); - boolean success = (boolean) jsonObject.get("success"); - - if (success) { - return getSuccessMessageEntity(bipToken); + BIPTokenResVO bipTokenResVO = JSON.parseObject(bipToken).toJavaObject(BIPTokenResVO.class); + String success = bipTokenResVO.getSuccess(); + if ("true".equals(success)) { + return getSuccessMessageEntity("查询成功",bipTokenResVO); } else { - return getFailureMessageEntity(bipToken); + return getFailureMessageEntity("查询成功",bipTokenResVO); } } catch (Exception e) { e.printStackTrace(); diff --git a/base-buildpackage/src/main/java/com/hzya/frame/finance/bip/entity/BIPTokenResVO.java b/base-buildpackage/src/main/java/com/hzya/frame/finance/bip/entity/BIPTokenResVO.java new file mode 100644 index 00000000..b8df8e3a --- /dev/null +++ b/base-buildpackage/src/main/java/com/hzya/frame/finance/bip/entity/BIPTokenResVO.java @@ -0,0 +1,28 @@ +package com.hzya.frame.finance.bip.entity; + +import lombok.Data; + +/** + * Created by zydd on 2025-09-08 18:34 + */ +@lombok.Data +public class BIPTokenResVO { + private String success; + private String code; + private String message; + private String errorStack; + private String pageInfo; + private Data data; + + @lombok.Data + public static class Data { + private String access_token; + private String expires_in; + private String refresh_token; + private String security_key; + private String ts; + private String grant_type; + private String security_level; + } + +} diff --git a/base-buildpackage/src/main/java/com/hzya/frame/finance/bip/service/impl/IBipApiServiceImpl.java b/base-buildpackage/src/main/java/com/hzya/frame/finance/bip/service/impl/IBipApiServiceImpl.java index a38c158d..a7db13c5 100644 --- a/base-buildpackage/src/main/java/com/hzya/frame/finance/bip/service/impl/IBipApiServiceImpl.java +++ b/base-buildpackage/src/main/java/com/hzya/frame/finance/bip/service/impl/IBipApiServiceImpl.java @@ -23,165 +23,91 @@ import java.util.Properties; @Service public class IBipApiServiceImpl implements IBipApiService { - - @Value("${spring.profiles.active}") - public String activeProfile; // app_secret - private static String client_secret = null; - // 公钥 - private static String pubKey = null; - // app_id - private static String client_id = null; - // bip用户名 - private static String username = null; - private static String usercode = null; - // bip用户名密码 - private static String pwd = null; - // bip账套 - private static String busi_center = null; - private static String dsname = null; - // 获取token方式:client_credentials、password - private static String grant_type = null; - // 服务器ip:port - private static String baseUrl = null; - // 返回值压缩加密级别 - private static String secret_level = null; - // 请求参数 - private static String requestBody = null; - // openapi请求路径 - private static String apiUrl = null; + private String grant_type = "client"; + + private String apiUrl; + + private String secret_level = "L0"; + + private String baseUrl; + + @Value("${BIP.ip}") + private String ip; + @Value("${BIP.port}") + private String port; +// @Value("${BIP.biz_center}") + private String biz_center="01"; + @Value("${BIP.client_id}") + private String client_id; + @Value("${BIP.client_secret}") + private String client_secret; + @Value("${BIP.pubKey}") + private String pubKey; + @Value("${BIP.user_name}") + private String user_name; + @Value("${BIP.pwd}") + private String pwd; + + class SecretConst { + public static final String LEVEL0 = "L0"; + + public static final String LEVEL1 = "L1"; + + public static final String LEVEL2 = "L2"; + + public static final String LEVEL3 = "L3"; + + public static final String LEVEL4 = "L4"; + } - // 访问api获取到的access_token - public static String token = null; - // 重复调用检查 - public static String repeat_check = null; - // 接口调用业务标识 - public static String busi_id = null; @Override public String getToekn() throws Exception { - // 初始化数据 - init(); - //获取token - String token = getToken(); - return token; - } - - public String pushData(String url, String data, String bipUserCode) throws Exception { - Assert.notNull(url, "请求地址不能为空"); - Assert.notNull(data, "请求参数不能为空(需要JSON格式)"); - - // 初始化相关参数 - if (pubKey == null) { - init(); - } - if (bipUserCode == null) { - token = getTokenByPWD(); - } else { - token = getTokenByClient(bipUserCode); - } - // 发起api接口 - return sendApi(token, url, data); - } - - - - private void init() { - - Properties properties = new Properties(); - String filepath = "application-" + activeProfile + ".properties"; - ClassLoader classloader = Thread.currentThread().getContextClassLoader(); - InputStream inputStream = classloader.getResourceAsStream(filepath); - try { - InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8"); - properties.load(reader); - - client_secret = new String(properties.getProperty("client_secret").getBytes("utf-8"), "utf-8"); - client_id = properties.getProperty("client_id"); - pubKey = properties.getProperty("pubKey"); - username = properties.getProperty("username"); - usercode = properties.getProperty("usercode"); - pwd = properties.getProperty("pwd"); - busi_center = properties.getProperty("busi_center"); - dsname = properties.getProperty("dsname"); - baseUrl = properties.getProperty("baseUrl"); - requestBody = new String(properties.getProperty("requestBody").getBytes("utf-8"), "utf-8"); -// apiUrl = properties.getProperty("apiUrl"); - grant_type = properties.getProperty("grant_type"); - secret_level = properties.getProperty("secret_level"); - repeat_check = properties.getProperty("repeat_check"); - busi_id = properties.getProperty("busi_id"); - } catch (IOException e) { - e.printStackTrace(); - } - } - - private static String getToken() throws Exception { String token = null; if ("password".equals(grant_type)) { - // 密码模式 token = getTokenByPWD(); - } else if ("client_credentials".equals(grant_type)) { -// // 客户端模式 -// token = getTokenByClient(); + } else if ("client".equals(grant_type)) { + token = getTokenByClient(); + } else { + Assert.state(false, "token获取模式错误"); } return token; } - private static String getTokenByClient(String userCode) throws Exception { - Map paramMap = new HashMap(); - // 密码模式认证 - paramMap.put("grant_type", "client_credentials"); - // 第三方应用id - paramMap.put("client_id", client_id); - // 第三方应用secret 公钥加密 - String secret_entryption = Encryption.pubEncrypt(pubKey, client_secret); -// System.out.println("secret_entryption::" + secret_entryption); - paramMap.put("client_secret", URLEncoder.encode(secret_entryption, "utf-8")); - // 账套编码 - paramMap.put("biz_center", busi_center); - paramMap.put("usercode", userCode); - // 签名 - String sign = SHA256Util.getSHA256(client_id + client_secret + pubKey, pubKey); - paramMap.put("signature", sign); -// System.out.println("##gettoken sign::" + sign); - String url = baseUrl + "/biploud/opm/accesstoken"; - String mediaType = "application/x-www-form-urlencoded"; - String token = doPost(url, paramMap, mediaType, null, ""); - return token; - } - - private static String getTokenByPWD() throws Exception { - Map paramMap = new HashMap(); - // 密码模式认证 + private String getTokenByPWD() throws Exception { + Map paramMap = new HashMap<>(); paramMap.put("grant_type", "password"); - // 第三方应用id paramMap.put("client_id", client_id); - // 第三方应用secret 公钥加密 - paramMap.put("client_secret", URLEncoder.encode(Encryption.pubEncrypt(pubKey, client_secret), "utf-8")); -// paramMap.put("client_secret", client_secret); - // bip用户名 - paramMap.put("username", username); - // 密码 公钥加密 - paramMap.put("password", URLEncoder.encode(Encryption.pubEncrypt(pubKey, pwd), "utf-8")); -// paramMap.put("password", pwd); - // 账套编码 - paramMap.put("biz_center", busi_center); - // 签名 - String sign = SHA256Util.getSHA256(client_id + client_secret + username + pwd + pubKey, pubKey); -// System.out.println("sign_getToken::" + sign); + paramMap.put("client_secret", URLEncoder.encode(Encryption.pubEncrypt(pubKey, client_secret), "UTF-8")); + paramMap.put("username", user_name); + paramMap.put("password", URLEncoder.encode(Encryption.pubEncrypt(pubKey, pwd), "UTF-8")); + paramMap.put("biz_center", biz_center); + String sign = SHA256Util.getSHA256(String.valueOf(client_id) + client_secret + user_name + pwd + pubKey, pubKey); paramMap.put("signature", sign); - - String url = baseUrl + "/biploud/opm/accesstoken"; + String url = String.valueOf(baseUrl) + "nccloud/opm/accesstoken"; String mediaType = "application/x-www-form-urlencoded"; String token = doPost(url, paramMap, mediaType, null, ""); return token; } - private static String doPost(String baseUrl, Map paramMap, String mediaType, - Map headers, String json) throws Exception { + public String getTokenByClient() throws Exception { + Map paramMap = new HashMap<>(); + paramMap.put("grant_type", "client_credentials"); + paramMap.put("client_id", client_id); + paramMap.put("client_secret", URLEncoder.encode(Encryption.pubEncrypt(pubKey, client_secret), "UTF-8")); + paramMap.put("biz_center", biz_center); + String sign = SHA256Util.getSHA256(String.valueOf(client_id) + client_secret + pubKey, pubKey); + paramMap.put("signature", sign); + String url = String.valueOf("http://" + ip + ":" + port + "/") + "nccloud/opm/accesstoken"; + String mediaType = "application/x-www-form-urlencoded"; + String token = doPost(url, paramMap, mediaType, null, ""); + return token; + } + + private String doPost(String baseUrl, Map paramMap, String mediaType, Map headers, String json) throws Exception { HttpURLConnection urlConnection = null; InputStream in = null; OutputStream out = null; @@ -195,11 +121,10 @@ public class IBipApiServiceImpl implements IBipApiService { for (Map.Entry entry : paramMap.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); - sb.append(key + "=" + value).append("&"); + sb.append(String.valueOf(key) + "=" + value).append("&"); } baseUrl = sb.toString().substring(0, sb.toString().length() - 1); } - URL urlObj = new URL(baseUrl); urlConnection = (HttpURLConnection) urlObj.openConnection(); urlConnection.setConnectTimeout(50000); @@ -214,16 +139,15 @@ public class IBipApiServiceImpl implements IBipApiService { } } out = urlConnection.getOutputStream(); - out.write(json.getBytes("utf-8")); + out.write(json.getBytes("UTF-8")); out.flush(); int resCode = urlConnection.getResponseCode(); -// System.out.println("状态码::" + resCode); -// if (resCode == HttpURLConnection.HTTP_OK || resCode == HttpURLConnection.HTTP_CREATED || resCode == HttpURLConnection.HTTP_ACCEPTED) { - in = urlConnection.getInputStream(); -// } else { -// in = urlConnection.getErrorStream(); -// } - bufferedReader = new BufferedReader(new InputStreamReader(in, "utf-8")); + if (resCode == 200 || resCode == 201 || resCode == 202) { + in = urlConnection.getInputStream(); + } else { + in = urlConnection.getErrorStream(); + } + bufferedReader = new BufferedReader(new InputStreamReader(in, "UTF-8")); StringBuffer temp = new StringBuffer(); String line = bufferedReader.readLine(); while (line != null) { @@ -232,33 +156,31 @@ public class IBipApiServiceImpl implements IBipApiService { } String ecod = urlConnection.getContentEncoding(); if (ecod == null) { - ecod = Charset.forName("utf-8").name(); + ecod = Charset.forName("UTF-8").name(); } - result = new String(temp.toString().getBytes("utf-8"), ecod); -// System.out.println(result); + result = new String(temp.toString().getBytes("UTF-8"), ecod); } catch (Exception e) { - System.out.println(e); throw e; } finally { - if (null != bufferedReader) { + if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException e) { - e.printStackTrace(); + throw e; } } - if (null != out) { + if (out != null) { try { out.close(); } catch (IOException e) { - e.printStackTrace(); + throw e; } } - if (null != in) { + if (in != null) { try { in.close(); } catch (IOException e) { - e.printStackTrace(); + throw e; } } urlConnection.disconnect(); @@ -266,123 +188,17 @@ public class IBipApiServiceImpl implements IBipApiService { return result; } - private String sendApi(String token, String apiUrl, String putParameter) throws Exception { - // token转对象,获取api访问所用token和secret - ResultMessageUtil returnData = new Gson().fromJson(token, ResultMessageUtil.class); - Map data = (Map) returnData.getData(); - String access_token = (String) data.get("access_token"); - String security_key = (String) data.get("security_key"); - String refresh_token = (String) data.get("refresh_token"); - long expire_in = new Double((double) data.get("expires_in")).longValue(); - long ts = new Double((double) data.get("ts")).longValue(); - if (ts + expire_in < System.currentTimeMillis()) { - token = getTokenByPWD(); - returnData = new Gson().fromJson(token, ResultMessageUtil.class); - data = (Map) returnData.getData(); - access_token = (String) data.get("access_token"); - security_key = (String) data.get("security_key"); - refresh_token = (String) data.get("refresh_token"); - } -// System.out.println("ACCESS_TOKEN:" + access_token); - // 请求路径 - String url = apiUrl; - // header 参数 - Map headermap = new HashMap<>(); - headermap.put("access_token", access_token); - headermap.put("client_id", client_id); - - StringBuffer sb = new StringBuffer(); - sb.append(client_id); - if (StringUtils.isNotBlank(putParameter)) { - // sb.append(requestBody.replaceAll("\\s*|\t|\r|\n", "").trim()); - sb.append(putParameter); - } - sb.append(pubKey); - String sign = SHA256Util.getSHA256(sb.toString(), pubKey); - headermap.put("signature", sign); - - if (StringUtils.isNotBlank(busi_id)) { - headermap.put("busi_id", busi_id); - } - if (StringUtils.isNotBlank(repeat_check)) { - headermap.put("repeat_check", repeat_check); - } - // headermap.put("ucg_flag", "y"); - - String mediaType = "application/json;charset=utf-8"; - - // 表体数据json - // 根据安全级别选择加密或压缩请求表体参数 - String json = dealRequestBody(putParameter, security_key, secret_level); - - // 返回值 - String result = doPost(url, null, mediaType, headermap, json); - String result2 = dealResponseBody(result, security_key, secret_level); -// System.out.println("【RESULT】:" + result); -// System.out.println("result解密:" + result2); - - return result2; - } - private static String dealRequestBody(String source, String security_key, String level) throws Exception { - String result = null; - if (StringUtils.isEmpty(level) || SecretConst.LEVEL0.equals(level)) { - result = source; - } else if (SecretConst.LEVEL1.equals(level)) { - result = Encryption.symEncrypt(security_key, source); - } else if (SecretConst.LEVEL2.equals(level)) { - result = CompressUtil.gzipCompress(source); - } else if (SecretConst.LEVEL3.equals(level)) { - result = Encryption.symEncrypt(security_key, CompressUtil.gzipCompress(source)); - } else if (SecretConst.LEVEL4.equals(level)) { - result = CompressUtil.gzipCompress(Encryption.symEncrypt(security_key, source)); - } else { - throw new Exception("无效的安全等级"); - } - - return result; - } - - private static String dealResponseBody(String source, String security_key, String level) throws Exception { - String result = null; - - if (StringUtils.isEmpty(level) || SecretConst.LEVEL0.equals(level)) { - result = source; - } else if (SecretConst.LEVEL1.equals(level)) { - result = Decryption.symDecrypt(security_key, source); - } else if (SecretConst.LEVEL2.equals(level)) { - result = CompressUtil.gzipDecompress(source); - } else if (SecretConst.LEVEL3.equals(level)) { - result = CompressUtil.gzipDecompress(Decryption.symDecrypt(security_key, source)); - } else if (SecretConst.LEVEL4.equals(level)) { - result = Decryption.symDecrypt(security_key, CompressUtil.gzipDecompress(source)); - } else { - throw new Exception("无效的安全等级"); - } - - return result; - } - - class SecretConst { - /** - * LEVEL0 不压缩、不加密 - */ - public static final String LEVEL0 = "L0"; - /** - * LEVEL1 只加密、不压缩 - */ - public static final String LEVEL1 = "L1"; - /** - * LEVEL2 只压缩、不加密 - */ - public static final String LEVEL2 = "L2"; - /** - * LEVEL3 先压缩、后加密 - */ - public static final String LEVEL3 = "L3"; - /** - * LEVEL4 先加密、后压缩 - */ - public static final String LEVEL4 = "L4"; +// public void init(String ip, String port, String biz_center, String client_id, String client_secret, String pubKey, String user_name, String pwd) { + public void init() { + baseUrl = "http://" + ip + ":" + port + "/"; + biz_center = biz_center; + client_id = client_id; + client_secret = client_secret; + pubKey = pubKey; + user_name = user_name; + pwd = pwd; } } + + diff --git a/base-buildpackage/src/main/resources/application-cocodev.yml b/base-buildpackage/src/main/resources/application-cocodev.yml index 5d42740a..2c868037 100644 --- a/base-buildpackage/src/main/resources/application-cocodev.yml +++ b/base-buildpackage/src/main/resources/application-cocodev.yml @@ -50,6 +50,18 @@ datasource: COCOCS client_secret: d18726dcc8ad487facdc pubKey: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAur9ViNXdgV9qTxoKuLRhXqhKcQ1A4Vl6AqM8me0W5iXSCW49X2jSdnp3kEL5CqiINqPoqwQjFSdBCDrYotvp8nmIW1iifpR+IRteDWmJ6H/bHOarZYCDz3rkYYqe6UOzjWnlx0xq3VPjRyJ51z/bL26c0GiQK6sLS9k960kfv3JwmyT1DFQzXTcRExqSAqDawyvwYfjmbwF8SX50CzIz5eXfVTsQhEZBcd6EIKJL37uo+7mBJ6QLpjLyXio51CdJoLRXmPfRrmY29yyTwLIDV0ujOM0u7SPOK+loXesNnSS9Qjwg9B++GZzZdg0smVd/MS0dVLnr541RzGuaK7GuFwIDAQAB secret_level: L0 + +BIP: + ip: 172.16.10.252 + port: 8089 + biz_center: 01 + client_id: XOne + client_secret: 99e0dc95c94a4b08b511 + user_name: XONE + pwd: xone123. + pubKey: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxdybRO4Um/tqdXfmZaR0GlPP+RsWgEWCp15T+4Y5BN7xW91gUoFqXwgyQRvHqEodIdjMJ8PtbcLnfTz2MmFx2zavwnaI+2QE09cOXUUV196hvyBISq4v2tXuUWdJ/9bRE1oIv69tfe2x2eo5FMyUIZ1z8ko3pwlULIUpSbyjozzfvItD7pG5RwFCrgJQJYy9zPi4VmGb1Cx74+VdYj8K6ZK6LErN9LQ/FOeS03vyMh12hAQbZjsuHzKIONnkTovqXI4Z5sSQ8RvInajHAp8+9pYV+KWIrTWcrEI/Ha+VGjUSZDqOeuReCI3IEf6thStVtVOXoxHB/WVWocTtiZf9FwIDAQAB + busi_center: develop + ## 服务器地址 baseUrl: http://115.238.74.170:8089/