|
@@ -1,6 +1,7 @@
|
|
|
package org.jeecg.modules.iotedgeCollectData.service;
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import org.apache.http.client.HttpClient;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
@@ -286,7 +287,6 @@ public class RestClientService {
|
|
|
restTemplate.setErrorHandler(new MyResponseErrorHandler());
|
|
|
ResponseEntity<JSONObject> response = restTemplate.exchange(url, HttpMethod.POST,httpEntity, JSONObject.class);
|
|
|
String accessToken = (String) response.getBody().get("accessToken");
|
|
|
- System.out.println("-----------"+accessToken.length());
|
|
|
configService.updateClientToken(accessToken);
|
|
|
return accessToken;
|
|
|
}
|
|
@@ -340,4 +340,200 @@ public class RestClientService {
|
|
|
return response.getBody();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * author: dzc
|
|
|
+ * version: 1.0
|
|
|
+ * des: 调用 SSO 接口获取 clientId 、 clientSecret
|
|
|
+ * date: 2024/8/5
|
|
|
+ */
|
|
|
+ public JSONObject postClinetInfo(){
|
|
|
+ ArrayList<String> scopList = new ArrayList<>();
|
|
|
+ scopList.add("Admin");
|
|
|
+ String ssoToken = getSSOToken();
|
|
|
+ String url = configService.getConfigValue(ConfigInfo.SSO_CREATECLIENT);
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("appName",System.getenv("appName"));
|
|
|
+ jsonObject.put("appId",System.getenv("appId"));
|
|
|
+ jsonObject.put("cluster",System.getenv("cluster"));
|
|
|
+ jsonObject.put("workspace",System.getenv("workspace"));
|
|
|
+ jsonObject.put("namespace",System.getenv("namespace"));
|
|
|
+ jsonObject.put("datacenter",System.getenv("datacenter"));
|
|
|
+ jsonObject.put("scopes",scopList);
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
+ headers.setContentType(type);
|
|
|
+ headers.add("Authorization", "Bearer " + ssoToken);
|
|
|
+ HttpEntity<String> httpEntity = new HttpEntity<String>(jsonObject.toString(), headers);
|
|
|
+ ResponseEntity<JSONObject> response = restTemplate.postForEntity(url, httpEntity, JSONObject.class);
|
|
|
+ return response.getBody();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * author: dzc
|
|
|
+ * version: 1.0
|
|
|
+ * des: 获取 clientToken
|
|
|
+ * date: 2024/8/5
|
|
|
+ */
|
|
|
+ public String getClientToken2(){
|
|
|
+ JSONObject clientInfo = postClinetInfo();
|
|
|
+ String clientId = (String) clientInfo.get("clientId");
|
|
|
+ String clientSecret = (String) clientInfo.get("clientSecret");
|
|
|
+ //String url = "http://127.0.0.1:8188/v4.0/oauth/token?grant_type=client_credentials&client_id="+clientId+"&client_secret="+clientSecret+"&duration=eternal";
|
|
|
+ String url = configService.getConfigValue(ConfigInfo.SSO_GetClientToken)+"?grant_type=client_credentials&client_id="+clientId+"&client_secret="+clientSecret+"&duration=eternal";
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
+ //headers.setContentType(type);
|
|
|
+ //headers.add("Authorization", "Bearer " + token);
|
|
|
+ HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
|
|
|
+ restTemplate.setErrorHandler(new MyResponseErrorHandler());
|
|
|
+ ResponseEntity<JSONObject> response = restTemplate.exchange(url, HttpMethod.POST,httpEntity, JSONObject.class);
|
|
|
+ String accessToken = (String) response.getBody().get("accessToken");
|
|
|
+ configService.updateClientToken2(accessToken);
|
|
|
+ configService.updateClientId(clientId);
|
|
|
+ configService.updateClientSecret(clientSecret);
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * author: dzc
|
|
|
+ * version: 1.0
|
|
|
+ * des: 新增 SSO 用户
|
|
|
+ * date: 2024/8/5
|
|
|
+ */
|
|
|
+ public JSONObject addSSOUser(String userName){
|
|
|
+ ArrayList<String> scopList = new ArrayList<>();
|
|
|
+ scopList.add("Admin");
|
|
|
+
|
|
|
+ String clientToken = configService.getConfigValue(ConfigInfo.POST_CLIENT_TOKEN);
|
|
|
+ String clientId = configService.getConfigValue(ConfigInfo.CLIENT_ID);
|
|
|
+ String clientSecret = configService.getConfigValue(ConfigInfo.CLIENT_SECRET);
|
|
|
+
|
|
|
+ if (ObjectUtil.isNull(clientToken) || "".equals(clientToken) || ObjectUtil.isNull(clientId) || "".equals(clientId) || ObjectUtil.isNull(clientSecret) || "".equals(clientSecret)){
|
|
|
+ getClientToken2();
|
|
|
+ clientToken = configService.getConfigValue(ConfigInfo.POST_CLIENT_TOKEN);
|
|
|
+ clientId = configService.getConfigValue(ConfigInfo.CLIENT_ID);
|
|
|
+ clientSecret = configService.getConfigValue(ConfigInfo.CLIENT_SECRET);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ String url = configService.getConfigValue(ConfigInfo.SSO_ADDUSER)+"/"+userName+"/scopes";
|
|
|
+
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("clientId",clientId);
|
|
|
+ jsonObject.put("clientSecret",clientSecret);
|
|
|
+ jsonObject.put("action","append");
|
|
|
+ jsonObject.put("scopes",scopList);
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
+ headers.setContentType(type);
|
|
|
+ headers.add("Authorization", "Bearer " + clientToken);
|
|
|
+ HttpEntity<String> httpEntity = new HttpEntity<String>(jsonObject.toString(), headers);
|
|
|
+
|
|
|
+ HttpClient httpClient = HttpClients.custom().build();
|
|
|
+ HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
|
|
|
+
|
|
|
+ RestTemplate restTemplate1 = new RestTemplate(factory);
|
|
|
+ restTemplate1.setErrorHandler(new MyResponseErrorHandler());
|
|
|
+ ResponseEntity<JSONObject> response = restTemplate1.exchange(url,HttpMethod.PATCH,httpEntity, JSONObject.class);
|
|
|
+ return response.getBody();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * author: dzc
|
|
|
+ * version: 1.0
|
|
|
+ * des: 调用 IoTEdge 的接口,邀请 SSO 中创建的用户
|
|
|
+ * date: 2024/8/6
|
|
|
+ */
|
|
|
+ public JSONObject invitationSSOUserInIoTEdge(String userName,String elToken){
|
|
|
+ //String url = "http://127.0.0.1:8082/v1/users/username"+"/"+userName;
|
|
|
+ String url = configService.getConfigValue(ConfigInfo.IOTEDGE_ADDUSER)+"/"+userName;
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
+ headers.setContentType(type);
|
|
|
+ headers.add("Authorization", "Bearer " + elToken);
|
|
|
+ HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
|
|
|
+ ResponseEntity<JSONObject> response = restTemplate.exchange(url, HttpMethod.POST,httpEntity, JSONObject.class);
|
|
|
+ return response.getBody();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * author: dzc
|
|
|
+ * version: 1.0
|
|
|
+ * des: 调用 IoTEdge 的接口,分配用户角色
|
|
|
+ * date: 2024/8/6
|
|
|
+ */
|
|
|
+ public JSONObject setUserRole(String userId,String groupId,String rloe,String elToken){
|
|
|
+ //String url = "http://127.0.0.1:8082/v1/users"+"/"+userId+"/rolebinding";
|
|
|
+ String url = configService.getConfigValue(ConfigInfo.IOTEDGE_SETUSERROLE)+"/"+userId+"/rolebinding";
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("groupId",groupId);
|
|
|
+ jsonObject.put("role",rloe);
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
+ headers.setContentType(type);
|
|
|
+ headers.add("Authorization", "Bearer " + elToken);
|
|
|
+ HttpEntity<String> httpEntity = new HttpEntity<String>(jsonObject.toString(), headers);
|
|
|
+ ResponseEntity<JSONObject> response = restTemplate.exchange(url, HttpMethod.POST,httpEntity, JSONObject.class);
|
|
|
+ return response.getBody();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * author: dzc
|
|
|
+ * version: 1.0
|
|
|
+ * des: 调用 IoTEdge 的接口,删除用户的权限
|
|
|
+ * date: 2024/8/7
|
|
|
+ */
|
|
|
+ public JSONObject deleteUserRole(String userId,String groupId,String rloe,String elToken){
|
|
|
+ //String url = "http://127.0.0.1:8082/v1/users"+"/"+userId+"/rolebinding";
|
|
|
+ String url = configService.getConfigValue(ConfigInfo.IOTEDGE_SETUSERROLE)+"/"+userId+"/rolebinding";
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("groupId",groupId);
|
|
|
+ jsonObject.put("role",rloe);
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
+ headers.setContentType(type);
|
|
|
+ headers.add("Authorization", "Bearer " + elToken);
|
|
|
+ HttpEntity<String> httpEntity = new HttpEntity<String>(jsonObject.toString(), headers);
|
|
|
+ ResponseEntity<JSONObject> response = restTemplate.exchange(url, HttpMethod.DELETE,httpEntity, JSONObject.class);
|
|
|
+ return response.getBody();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * author: dzc
|
|
|
+ * version: 1.0
|
|
|
+ * des: 调用 SSO 的接口,删除用户
|
|
|
+ * date: 2024/8/7
|
|
|
+ */
|
|
|
+ public JSONObject deleteSSOUser(String userName){
|
|
|
+ String ssoToken = getSSOToken();
|
|
|
+ String url = "http://127.0.0.1:8188/v4.0/users"+"/"+userName;
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
+ headers.setContentType(type);
|
|
|
+ headers.add("Authorization", "Bearer " + ssoToken);
|
|
|
+ HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
|
|
|
+ ResponseEntity<JSONObject> response = restTemplate.exchange(url, HttpMethod.DELETE,httpEntity, JSONObject.class);
|
|
|
+ return response.getBody();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * author: dzc
|
|
|
+ * version: 1.0
|
|
|
+ * des: 调用 IoTEdge 的接口,删除用户
|
|
|
+ * date: 2024/8/7
|
|
|
+ */
|
|
|
+ public JSONObject deleteIoTEdgeUser(String userId,String elToken){
|
|
|
+ String url = "http://127.0.0.1:8082/v1/user"+"/"+userId;
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
+ headers.setContentType(type);
|
|
|
+ headers.add("Authorization", "Bearer " + elToken);
|
|
|
+ HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
|
|
|
+ ResponseEntity<JSONObject> response = restTemplate.exchange(url, HttpMethod.DELETE,httpEntity, JSONObject.class);
|
|
|
+ return response.getBody();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|