Selaa lähdekoodia

钉钉通讯录回调接口注册

fancy 5 vuotta sitten
vanhempi
commit
9bbd5fce08

+ 4 - 6
o2server/x_program_center/src/main/java/com/x/program/center/jaxrs/dingding/ActionSyncOrganizationCallbackPost.java

@@ -10,6 +10,7 @@ import com.x.base.core.project.logger.Logger;
 import com.x.base.core.project.logger.LoggerFactory;
 import com.x.program.center.ThisApplication;
 import com.x.program.center.jaxrs.dingding.encrypt.DingTalkEncryptor;
+import com.x.program.center.jaxrs.dingding.encrypt.Utils;
 
 import java.util.*;
 
@@ -24,7 +25,7 @@ public class ActionSyncOrganizationCallbackPost extends BaseAction {
 
     private List<String> tags = new ArrayList<>(Arrays.asList("user_add_org", "user_modify_org", "user_leave_org", "user_active_org", "org_dept_create", "org_dept_modify", "org_dept_remove"));
 
-    String execute(EffectivePerson effectivePerson, String signature, String timestamp, String nonce, JsonElement body) throws Exception {
+    Map<String, String> execute(EffectivePerson effectivePerson, String signature, String timestamp, String nonce, JsonElement body) throws Exception {
         String params = "signature:" + signature + " timestamp:" + timestamp + " nonce:" + nonce + " body:" + body;
         logger.info(params);
         if (Config.dingding().getEnable()) {
@@ -42,11 +43,8 @@ public class ActionSyncOrganizationCallbackPost extends BaseAction {
             } else {
                 logger.info("忽略的。。。。。。。。。。");
             }
-            Long time = new Date().getTime();
-            Map<String, String> result = dingTalkEncryptor.getEncryptedMap("success", time, nonce);
-            String json = gson.toJson(result);
-            logger.info("json: {}", json);
-            return json;
+            Map<String, String> result = dingTalkEncryptor.getEncryptedMap("success", System.currentTimeMillis(), Utils.getRandomStr(8));
+            return result;
         }else {
             throw new ExceptionNotPullSync();
         }

+ 1 - 1
o2server/x_program_center/src/main/java/com/x/program/center/jaxrs/dingding/ActionSyncOrganizationCallbackUrlRegister.java

@@ -1,7 +1,6 @@
 package com.x.program.center.jaxrs.dingding;
 
 
-import com.x.base.core.project.config.ApplicationServer;
 import com.x.base.core.project.config.CenterServer;
 import com.x.base.core.project.config.Config;
 import com.x.base.core.project.connection.HttpConnection;
@@ -47,6 +46,7 @@ public class ActionSyncOrganizationCallbackUrlRegister extends BaseAction {
             logger.info("注册回调地址 post对象:{}", registerObject.toString());
             //钉钉回调地址注册 url post
             String address = Config.dingding().getOapiAddress() + "/call_back/register_call_back?access_token=" + Config.dingding().corpAccessToken();
+            logger.info("register url :" + address);
             DingdingMessageResp resp = HttpConnection.postAsObject(address, null, registerObject.toString(), DingdingMessageResp.class);
             if (resp.getErrcode() != 0) {
                 throw  new ExceptionRegisterCallbackMessage(resp.getErrcode(), resp.getErrmsg());

+ 7 - 6
o2server/x_program_center/src/main/java/com/x/program/center/jaxrs/dingding/DingdingAction.java

@@ -8,6 +8,8 @@ import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
 import com.google.gson.JsonElement;
 import com.x.base.core.project.annotation.JaxrsDescribe;
 import com.x.base.core.project.annotation.JaxrsMethodDescribe;
@@ -19,7 +21,6 @@ import com.x.base.core.project.jaxrs.StandardJaxrsAction;
 import com.x.base.core.project.logger.Logger;
 import com.x.base.core.project.logger.LoggerFactory;
 
-import java.util.HashMap;
 import java.util.Map;
 
 @Path("dingding")
@@ -114,13 +115,13 @@ public class DingdingAction extends StandardJaxrsAction {
 	@Produces(HttpMediaType.APPLICATION_JSON_UTF_8)
 	@Consumes(MediaType.APPLICATION_JSON)
 	public void syncOrganizationCallback(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request,
-										 @QueryParam("signature") String signature,
-										 @QueryParam("timestamp") String timestamp, @QueryParam("nonce") String nonce,
-										 JsonElement jsonElement) {
+										 @QueryParam("signature") String signature, @QueryParam("timestamp") String timestamp,
+										 @QueryParam("nonce") String nonce, JsonElement jsonElement) {
 		EffectivePerson effectivePerson = this.effectivePerson(request);
 		try {
-			String json = new ActionSyncOrganizationCallbackPost().execute(effectivePerson, signature, timestamp, nonce, jsonElement);
-			asyncResponse.resume(Response.ok(json).build());
+			Map<String, String> json = new ActionSyncOrganizationCallbackPost().execute(effectivePerson, signature, timestamp, nonce, jsonElement);
+			Gson gson = new GsonBuilder().disableHtmlEscaping().create();
+			asyncResponse.resume(Response.ok(gson.toJson(json)).build());
 		} catch (Exception e) {
 			logger.error(e, effectivePerson, request, null);
 			asyncResponse.resume(Response.serverError().entity("fail").build());

+ 8 - 5
o2server/x_program_center/src/main/java/com/x/program/center/jaxrs/dingding/TestMain.java

@@ -2,7 +2,6 @@ package com.x.program.center.jaxrs.dingding;
 
 
 import com.google.gson.Gson;
-import com.x.program.center.jaxrs.dingding.encrypt.DingTalkEncryptException;
 import com.x.program.center.jaxrs.dingding.encrypt.DingTalkEncryptor;
 
 import java.util.Date;
@@ -34,11 +33,15 @@ public class TestMain {
             //    "nonce": "2dffdfdfdf2222"
             //}
 
-            String j = dingTalkEncryptor.getDecryptMsg("77d1d1214d0bb07e91be9b231c802cfd575d8a5f", "1603697312745", "2dffdfdfdf2222",
-                    "Z129Xx+g/4RNjuplftqRsS24/FbMqS8Zy56JGpUQtL1zpVAoqhGS1PHiF5QdxQKRwzUgNg06tDylA02Lyy/W9Q\\u003d\\u003d");
-            System.out.println(j);
-        } catch (DingTalkEncryptException e) {
+//            String j = dingTalkEncryptor.getDecryptMsg("111108bb8e6dbce3c9671d6fdb69d15066227608", "1783610513", "123456",
+//                    "1ojQf0NSvw2WPvW7LijxS8UvISr8pdDP+rXpPbcLGOmIBNbWetRg7IP0vdhVgkVwSoZBJeQwY2zhROsJq/HJ+q6tp1qhl9L1+ccC9ZjKs1wV5bmA9NoAWQiZ+7MpzQVq+j74rJQljdVyBdI/dGOvsnBSCxCVW0ISWX0vn9lYTuuHSoaxwCGylH9xRhYHL9bRDskBc7bO0FseHQQasdfghjkl");
+//            System.out.println(j+"点点滴滴");
+
+
+        } catch (Exception e) {
             e.printStackTrace();
         }
     }
+
+
 }

+ 3 - 1
o2server/x_program_center/src/main/java/com/x/program/center/jaxrs/dingding/encrypt/DingTalkEncryptor.java

@@ -1,5 +1,6 @@
 package com.x.program.center.jaxrs.dingding.encrypt;
 
+
 import org.apache.commons.codec.binary.Base64;
 
 import javax.crypto.Cipher;
@@ -86,10 +87,11 @@ public class DingTalkEncryptor {
             Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
             SecretKeySpec keySpec = new SecretKeySpec(this.aesKey, "AES");
             IvParameterSpec iv = new IvParameterSpec(this.aesKey, 0, 16);
-            cipher.init(1, keySpec, iv);
+            cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
             byte[] encrypted = cipher.doFinal(unencrypted);
             String result = base64.encodeToString(encrypted);
             return result;
+
         } catch (Exception var15) {
             throw new DingTalkEncryptException(900007);
         }