Переглянути джерело

Token认证:Bearer Token

wanfa99 2 роки тому
батько
коміт
0029dea8d3

+ 7 - 3
jeecg-boot-module-system/src/main/java/org/jeecg/modules/openApi/controller/TestController.java

@@ -2,6 +2,7 @@ package org.jeecg.modules.openApi.controller;
 
 import com.alibaba.fastjson.JSONObject;
 import org.jeecg.modules.openApi.service.ISenYuDataSourceOne;
+import org.jeecg.modules.system.util.InterfaceConnUtils;
 import org.jeecg.modules.system.util.JsonChangeUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -24,11 +25,14 @@ public class TestController {
     private ISenYuDataSourceOne senYuDataSourceOne;
 
     @GetMapping(value = "/Query")
-    public JSONObject save(HttpServletRequest request, HttpServletResponse response) {
+    public JSONObject save(HttpServletRequest request, HttpServletResponse response) throws Exception {
         JSONObject result = new JSONObject();
-        String sql="select  * from Inventory where cInvCode='0101000001'";
+        String sql="select  top 1 *  from Inventory";
         List<Map<String, Object>> list=senYuDataSourceOne.queryForList(sql);
         List<JSONObject> listJSONObject= JsonChangeUtils.toListJson(list);
-        return  listJSONObject.get(0);
+        result=listJSONObject.get(0);
+        //调用采购订单接口测试
+        InterfaceConnUtils.doPostToBearer(result,"https://zoneda.onestep-cloud.com/hitf/v1/rest/invoke?namespace=MFY1001&serverCode=NBSY-ZCOM&interfaceCode=hzmc-communication.senyu-po-interface.createPoInterface");
+        return  result;
     }
 }

+ 44 - 2
jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/util/InterfaceConnUtils.java

@@ -24,10 +24,10 @@ import java.util.Map;
 public class InterfaceConnUtils {
 
     /**
-     * post请求-推数据
+     * post请求-推数据,Token认证:Basic Auth
      * @throws Exception
      */
-    public static JSONObject doPost(JSONObject json, String url,String type) throws Exception {
+    public static JSONObject doPostToBasicAuth(JSONObject json, String url) throws Exception {
         //定义测试返回JSON
         JSONObject onejb = new JSONObject();
         HttpClient httpClient = new DefaultHttpClient();
@@ -60,5 +60,47 @@ public class InterfaceConnUtils {
         }
           return onejb;
     }
+    /**
+     * post请求-推数据,Token认证:Bearer Token
+     * @throws Exception
+     */
+    public static JSONObject doPostToBearer(JSONObject json, String url) throws Exception {
+        //定义返回JSON
+        JSONObject onejb = new JSONObject();
+        String token=null;
+        HttpClient httpClient = new DefaultHttpClient();
+        //获取token的Url
+        String tokenUrl="https://zoneda.onestep-cloud.com/oauth/oauth/token?grant_type=client_credentials&client_id=SY-NO-PASS-LOGIN&client_secret=SY-NO-PASS-LOGIN";
+        try {
+            HttpPost hpGetToken=new HttpPost(tokenUrl);
+            //调用接口
+            HttpResponse response = httpClient.execute(hpGetToken);
+            //返回信息
+            String resultTokenStr = EntityUtils.toString(response.getEntity());
+            JSONObject tokenjb=JSONObject.parseObject(resultTokenStr);
+            token=tokenjb.getString("access_token");
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        HttpPost httpPost=new HttpPost(url);
+        httpPost.setHeader("Content-Type", "application/json");
+        httpPost.setHeader("Authorization","Bearer " + token);
 
+        StringEntity entity;
+        entity = new StringEntity(json.toString(),"utf-8");
+        entity.setContentEncoding("utf-8");
+        entity.setContentType("application/json");
+        httpPost.setEntity(entity);
+        try {
+            //调用接口
+            HttpResponse response = httpClient.execute(httpPost);
+            //返回信息
+            String resultStr = EntityUtils.toString(response.getEntity());
+            onejb=JSONObject.parseObject(resultStr);
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return onejb;
+    }
 }