add:IDM组织、人员数据同步

This commit is contained in:
2510220824
2021-07-04 13:57:56 +08:00
parent bad0f16f90
commit 3e69a462b0
4 changed files with 229 additions and 0 deletions
+21
View File
@@ -33,6 +33,27 @@
<artifactId>adc-da-base</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
</dependencies>
<build>
@@ -0,0 +1,30 @@
package com.adc.da.sync.controller;
import com.adc.da.sync.service.SyncUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/${restPath}/sync")
@Api(tags = "数据同步管理")
public class TestSyncController {
@Autowired
private SyncUserService syncUserService;
@ApiOperation(value = "福田IDM用户数据同步")
@GetMapping("/syncUser")
public void syncUser() throws Exception {
syncUserService.syncFotonUser();
}
@ApiOperation(value = "福田IDM组织数据同步")
@GetMapping("/syncOrg")
public void syncOrg() throws Exception {
syncUserService.syncFotonOrg();
}
}
@@ -0,0 +1,100 @@
package com.adc.da.sync.service;
import com.adc.da.sync.util.AuthUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONSerializer;
import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service("syncUserService")
@Slf4j
public class SyncUserService {
/**
* 同步福田IDM数据
* @throws Exception
*/
public void syncFotonUser()throws Exception{
String appuser = "wsuser";
String appkey = "o1bnrph3pe6i63txm3z5l3cnka2a82pi";
AuthUtils util = new AuthUtils(appuser, appkey, null);
byte[] cookie = null;
do {
Map<String, Object> params = new HashMap<String, Object>();
params.put("cookie", Base64.encodeBase64String(cookie));
params.put("timestamp", "20141125065609Z");
// 用户参数
params.put("filter", "(userid=*)");
params.put("basedn", "ou=People,o=foton.com.cn,o=isp");
System.out.println("RequestBody:" + params.toString());
// 用户测试
String rs = util.getResponseFromServer("http://172.24.224.42:82/rest/users/getUserList", params);
System.err.println(rs);
JSONObject responseStr = (JSONObject) JSONSerializer.toJSON(rs);
if (responseStr.containsKey("cookie")) {
try {
JSONArray entries = responseStr.getJSONArray("cookie");
cookie = new byte[entries.size()];
for (int i = 0; i < entries.size(); i++) {
cookie[i] = (byte) entries.getByte(i);
}
} catch (Exception e) {
if (e.getMessage().contains("is not a JSONArray")) {
cookie = null;
}
}
}
} while (cookie != null);
}
public void syncFotonOrg()throws Exception{
String appuser = "wsuser";
String appkey = "o1bnrph3pe6i63txm3z5l3cnka2a82pi";
AuthUtils util = new AuthUtils(appuser, appkey, null);
byte[] cookie = null;
do {
Map<String, Object> params = new HashMap<String, Object>();
params.put("cookie", Base64.encodeBase64String(cookie));
params.put("timestamp", "20141125065609Z");
// 组织参数
params.put("filter", "(orgNumber=*)");
params.put("basedn", "ou=Organizations,o=foton.com.cn,o=isp");
// 组织测试
String rs = util.getResponseFromServer("http://172.24.224.42:82/rest/orgs/getOrgList", params);
System.err.println(rs);
JSONObject responseStr = (JSONObject) JSONSerializer.toJSON(rs);
if (responseStr.containsKey("cookie")) {
try {
JSONArray entries = responseStr.getJSONArray("cookie");
cookie = new byte[entries.size()];
for (int i = 0; i < entries.size(); i++) {
cookie[i] = (byte) entries.getByte(i);
}
} catch (Exception e) {
if (e.getMessage().contains("is not a JSONArray")) {
cookie = null;
}
}
}
} while (cookie != null);
}
}
@@ -0,0 +1,78 @@
package com.adc.da.sync.util;
import net.sf.json.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;
public class AuthUtils {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss'Z'");
private final String appuser;
private final String appkey;
private final String appinfo;
public AuthUtils(String appuser, String appkey, String appinfo) {
this.appuser = appuser;
this.appkey = appkey;
this.appinfo = appinfo;
}
public String getResponseFromServer(String url, Map<String, Object> params) throws Exception {
HttpPost httpPost = new HttpPost(url);
createSignHeader(httpPost, params);
JSONObject object = JSONObject.fromObject(params);
httpPost.setEntity(new ByteArrayEntity(object.toString().getBytes(), ContentType.APPLICATION_JSON));
return execute(httpPost);
}
private String execute(HttpUriRequest request) throws Exception {
CloseableHttpClient client = null;
CloseableHttpResponse resp = null;
try {
client = HttpClientBuilder.create().build();
resp = client.execute(request);
return EntityUtils.toString(resp.getEntity());
} finally {
HttpClientUtils.closeQuietly(resp);
HttpClientUtils.closeQuietly(client);
}
}
private void createSignHeader(HttpUriRequest request, Map<String, Object> params) throws Exception {
request.addHeader("appuser", appuser);
String randomcode = RandomStringUtils.random(10, true, true);
request.addHeader("randomcode", randomcode);
Calendar now = Calendar.getInstance();
String timestamp = sdf.format(now.getTime());
request.addHeader("timestamp", timestamp);
String data = StringUtils.join(appuser, randomcode, timestamp);
String encodekey = DigestUtils.sha256Hex(StringUtils.join(data, "{", appkey, "}"));
request.addHeader("encodekey", encodekey);
JSONObject object = JSONObject.fromObject(params);
String paramData = StringUtils.join(object.toString(), "&", appkey);
String signature = DigestUtils.md5Hex(paramData);
request.addHeader("sign", signature);
request.addHeader("appinfo", appinfo);
request.addHeader("Content-Type", "application/json");
}
}