diff --git a/adc-da-sys/pom.xml b/adc-da-sys/pom.xml index 66956665..d199fc22 100644 --- a/adc-da-sys/pom.xml +++ b/adc-da-sys/pom.xml @@ -33,6 +33,27 @@ adc-da-base 3.0.0 + + org.apache.httpcomponents + httpclient + 4.5.2 + + + commons-codec + commons-codec + 1.10 + + + org.apache.commons + commons-lang3 + 3.4 + + + net.sf.json-lib + json-lib + 2.4 + jdk15 + diff --git a/adc-da-sys/src/main/java/com/adc/da/sync/controller/TestSyncController.java b/adc-da-sys/src/main/java/com/adc/da/sync/controller/TestSyncController.java new file mode 100644 index 00000000..07dcc3bd --- /dev/null +++ b/adc-da-sys/src/main/java/com/adc/da/sync/controller/TestSyncController.java @@ -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(); + } +} diff --git a/adc-da-sys/src/main/java/com/adc/da/sync/service/SyncUserService.java b/adc-da-sys/src/main/java/com/adc/da/sync/service/SyncUserService.java new file mode 100644 index 00000000..defd0c5c --- /dev/null +++ b/adc-da-sys/src/main/java/com/adc/da/sync/service/SyncUserService.java @@ -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 params = new HashMap(); + 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 params = new HashMap(); + 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); + } +} diff --git a/adc-da-sys/src/main/java/com/adc/da/sync/util/AuthUtils.java b/adc-da-sys/src/main/java/com/adc/da/sync/util/AuthUtils.java new file mode 100644 index 00000000..3a085b1f --- /dev/null +++ b/adc-da-sys/src/main/java/com/adc/da/sync/util/AuthUtils.java @@ -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 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 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"); + } +}