add:IDM组织、人员数据同步
This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user