Browse Source

碧合获取token请求

wangmh 7 years ago
parent
commit
e0e74ae82e

+ 54 - 0
sso-server/src/main/java/com/uas/sso/bihe/entity/OAuthRoot.java

@@ -10,6 +10,14 @@ public class OAuthRoot {
 
 
     private OAuthInfo data;
     private OAuthInfo data;
 
 
+    private String appName;
+
+    private String id;
+
+    private String ip;
+
+    private String msg;
+
     public String getCode() {
     public String getCode() {
         return code;
         return code;
     }
     }
@@ -25,4 +33,50 @@ public class OAuthRoot {
     public void setData(OAuthInfo data) {
     public void setData(OAuthInfo data) {
         this.data = data;
         this.data = data;
     }
     }
+
+    public String getAppName() {
+        return appName;
+    }
+
+    public void setAppName(String appName) {
+        this.appName = appName;
+    }
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    public String getIp() {
+        return ip;
+    }
+
+    public void setIp(String ip) {
+        this.ip = ip;
+    }
+
+    public String getMsg() {
+        return msg;
+    }
+
+    public void setMsg(String msg) {
+        this.msg = msg;
+    }
+
+    public enum Code {
+        OK("0");
+
+        private String code;
+
+        Code(String code) {
+            this.code = code;
+        }
+
+        public String getCode() {
+            return code;
+        }
+    }
 }
 }

+ 27 - 11
sso-server/src/main/java/com/uas/sso/bihe/service/impl/BiHeServiceImpl.java

@@ -5,9 +5,26 @@ import com.uas.sso.bihe.entity.OAuthRoot;
 import com.uas.sso.bihe.service.BiHeService;
 import com.uas.sso.bihe.service.BiHeService;
 import com.uas.sso.common.util.HttpUtil;
 import com.uas.sso.common.util.HttpUtil;
 import com.uas.sso.core.BHParam;
 import com.uas.sso.core.BHParam;
+import com.uas.sso.exception.VisibleError;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.util.EntityUtils;
+import org.springframework.http.MediaType;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 import org.springframework.ui.ModelMap;
 import org.springframework.ui.ModelMap;
 
 
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLSession;
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.util.*;
+
 /**
 /**
  * @author: huyy
  * @author: huyy
  * @date: 2018/7/18 11:26
  * @date: 2018/7/18 11:26
@@ -19,20 +36,19 @@ public class BiHeServiceImpl implements BiHeService {
         String url = "https://opengwtest.bgycc.com/open/oauth/token";
         String url = "https://opengwtest.bgycc.com/open/oauth/token";
         String appid = BHParam.DEFAULT_APPID;
         String appid = BHParam.DEFAULT_APPID;
         String appSecret = BHParam.DEFAULT_APPSECRET;
         String appSecret = BHParam.DEFAULT_APPSECRET;
-        ModelMap data = new ModelMap();
-        data.put("code", "XIH1G4");
-        data.put("appId", appid);
-        data.put("appSecret", appSecret);
-
+        Map<String, String> map = new HashMap<>();
+        map.put("code", "XIH1G4");
+        map.put("appSecret", "8a9d398c70f842309472a69bc730038d");
+        map.put("appId", "a9f624cbbdb947049f5638880b0ecbb2");
         try {
         try {
-            HttpUtil.ResponseWrap res = HttpUtil.doHttpsPost(url, data.toString(), 10000);
-            if (res.getContent() != null) {
-                System.out.println(res.getContent().toString());
-                OAuthRoot oAuthRoot = JSON.parseObject(res.getContent().toString(), OAuthRoot.class);
-                return oAuthRoot;
+            String content = com.uas.sso.util.HttpUtil.doPost(url, JSON.toJSONString(map));
+            OAuthRoot oAuthRoot = JSON.parseObject(content, OAuthRoot.class);
+            if (!OAuthRoot.Code.OK.getCode().equals(oAuthRoot.getCode())) {
+                throw new VisibleError(oAuthRoot.getMsg());
             }
             }
+
         } catch (Exception e) {
         } catch (Exception e) {
-            System.out.println(e);
+            e.printStackTrace();
         }
         }
         return null;
         return null;
     }
     }

+ 28 - 0
sso-server/src/main/java/com/uas/sso/util/HttpUtil.java

@@ -0,0 +1,28 @@
+package com.uas.sso.util;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.util.EntityUtils;
+import org.springframework.http.MediaType;
+
+/**
+ * @author wangmh
+ * @create 2018-07-18 18:01
+ * @desc http请求工具类
+ **/
+public class HttpUtil {
+
+    public static String doPost(String postUrl, String formData) throws Exception {
+        HttpClient httpClient = new DefaultHttpClient();
+        HttpPost post = new HttpPost(postUrl);
+        StringEntity postingString = new StringEntity(formData, "UTF-8");
+        post.setEntity(postingString);
+        post.setHeader("Content-type", MediaType.APPLICATION_JSON.toString());
+        HttpResponse response = httpClient.execute(post);
+        String content = EntityUtils.toString(response.getEntity());
+        return content;
+    }
+}