|
@@ -2,19 +2,22 @@ package com.zkqy.web.controller.system;
|
|
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.zkqy.common.core.domain.AjaxResult;
|
|
|
+import com.zkqy.common.core.domain.R;
|
|
|
import com.zkqy.common.utils.http.HttpUtilsOauth2;
|
|
|
|
|
|
+import okhttp3.MultipartBody;
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
+import okhttp3.Request;
|
|
|
+import okhttp3.Response;
|
|
|
import org.apache.http.HttpResponse;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestBody;
|
|
|
-import org.springframework.web.bind.annotation.RequestParam;
|
|
|
-import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
+import java.io.IOException;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Base64;
|
|
|
import java.util.HashMap;
|
|
@@ -54,40 +57,44 @@ public class ThirdPartLoginController {
|
|
|
// 转换编码凭证
|
|
|
byte[] encodedBytes = Base64.getEncoder().encode(BASIC.getBytes(StandardCharsets.UTF_8));
|
|
|
String encodedCredentials = new String(encodedBytes, StandardCharsets.UTF_8);
|
|
|
- headers.put("Authorization", " Basic " + encodedCredentials);
|
|
|
- headers.put("Content-Type", "application/json");
|
|
|
+ headers.put("Authorization", "Basic " + encodedCredentials);
|
|
|
+ headers.put("Content-Type", "multipart/form-data");
|
|
|
HttpResponse response = HttpUtilsOauth2.doPost(URL, "/oauth2/token", "post", headers, map, new HashMap<>());
|
|
|
//2、处理
|
|
|
if (response.getStatusLine().getStatusCode() == 200) {
|
|
|
// 得到code换取token结果
|
|
|
String tokenJSON = EntityUtils.toString(response.getEntity());
|
|
|
+ System.out.println("获取token" + tokenJSON);
|
|
|
Map<String, Object> tokenMap = JSONObject.parse(tokenJSON);
|
|
|
// 封装请求用户信息请求头
|
|
|
Map<String, String> getUserHeaders = new HashMap<>();
|
|
|
- getUserHeaders.put("Authorization", " Bearer " + tokenMap.get("access_token"));
|
|
|
-// getUserHeaders.put("Content-Type", "application/json");
|
|
|
+ getUserHeaders.put("Authorization", "Bearer " + tokenMap.get("access_token"));
|
|
|
+ getUserHeaders.put("Content-Type", "application/json");
|
|
|
HttpResponse userResponse = HttpUtilsOauth2.doGet(URL, "/user/login", "get", getUserHeaders, new HashMap<>());
|
|
|
if (userResponse.getStatusLine().getStatusCode() == 200) {
|
|
|
// 得到当前登录用户信息
|
|
|
String userJson = EntityUtils.toString(userResponse.getEntity());
|
|
|
Map<String, Object> retUserMap = JSONObject.parse(userJson);
|
|
|
- String usernmae = "kjjt01";
|
|
|
+ System.out.println("用户json:" + userJson);
|
|
|
Map<String, Object> data = (Map<String, Object>) retUserMap.get("data");
|
|
|
- data.get("username");
|
|
|
- if (null == data.get("companyTenant")) {
|
|
|
- // ((Map) data.get("companyTenant")).get("tenantcode").toString();
|
|
|
- String tenantCode = "kjjt01";
|
|
|
+ String usernmae = data.get("username").toString();
|
|
|
+ if (null != data.get("companyTenant")) {
|
|
|
+ String tenantCode = ((Map) data.get("companyTenant")).get("tenantCode").toString();
|
|
|
+ System.out.println("用户名:" + (tenantCode + "^_^" + usernmae + "^_^" + System.currentTimeMillis() / 1000));
|
|
|
byte[] userInfo = Base64.getEncoder().encode((tenantCode + "^_^" + usernmae + "^_^" + System.currentTimeMillis() / 1000).getBytes(StandardCharsets.UTF_8));
|
|
|
String retUserInfo = new String(userInfo, StandardCharsets.UTF_8);
|
|
|
+ System.out.println("正常跳转");
|
|
|
return "redirect:" + REDIRECT_URL + "/redirectAuth?bWVz=" + retUserInfo;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
System.out.println(e);
|
|
|
+ System.out.println("最外层跳转1");
|
|
|
// 异常处理
|
|
|
return "redirect:" + REDIRECT_URL + "/401";
|
|
|
}
|
|
|
+ System.out.println("最外层跳转2");
|
|
|
// 授权失败处理
|
|
|
return "redirect:" + REDIRECT_URL + "/401";
|
|
|
}
|
|
@@ -97,5 +104,36 @@ public class ThirdPartLoginController {
|
|
|
public AjaxResult authorize() {
|
|
|
return AjaxResult.success("认证中心", URL + "/oauth2/authorize?" + "response_type=code" + "&client_id=toolmes" + "&scope=openid" + "&redirect_uri=" + CALLBACK);
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+// @GetMapping("oauth/callback")
|
|
|
+// public Object getToken(@RequestParam String code) {
|
|
|
+// byte[] encodedBytes = Base64.getEncoder().encode(BASIC.getBytes(StandardCharsets.UTF_8));
|
|
|
+// String encode = new String(encodedBytes, StandardCharsets.UTF_8);
|
|
|
+// // 构建form-data请求体
|
|
|
+// MultipartBody requestBody = new MultipartBody.Builder()
|
|
|
+// .setType(MultipartBody.FORM)
|
|
|
+// .addFormDataPart("grant_type", "authorization_code")
|
|
|
+// .addFormDataPart("code", code)
|
|
|
+// .addFormDataPart("redirect_uri", CALLBACK)
|
|
|
+// .build();
|
|
|
+// // 构建请求
|
|
|
+// Request request = new Request.Builder()
|
|
|
+// .url(URL + "/oauth2/token")
|
|
|
+// .post(requestBody)
|
|
|
+// .header("Authorization", "Basic " + encode)
|
|
|
+// .build();
|
|
|
+// // 发送请求并获取响应
|
|
|
+// OkHttpClient client = new OkHttpClient();
|
|
|
+// try (Response response = client.newCall(request).execute()) {
|
|
|
+// String responseBody = response.body().string();
|
|
|
+// System.out.println(responseBody);
|
|
|
+// // 处理响应
|
|
|
+// return responseBody;
|
|
|
+// } catch (IOException e) {
|
|
|
+// System.out.println("code换token异常");
|
|
|
+// }
|
|
|
+// return R.fail("获取错误");
|
|
|
+// }
|
|
|
}
|
|
|
|