lichen 3 долоо хоног өмнө
parent
commit
7d7141ab69

+ 10 - 0
springboot-ldaptive/pom.xml

@@ -28,6 +28,16 @@
             <artifactId>spring-boot-starter-amqp</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-aop</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.aspectj</groupId>
+            <artifactId>aspectjweaver</artifactId>
+        </dependency>
+
         <dependency>
             <groupId>io.springfox</groupId>
             <artifactId>springfox-swagger2</artifactId>

+ 5 - 40
springboot-ldaptive/src/main/java/org/example/lc/config/RabbitConfig.java

@@ -1,37 +1,15 @@
 package org.example.lc.config;
 
 import org.springframework.amqp.core.Queue;
-import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
-import org.springframework.amqp.rabbit.connection.ConnectionFactory;
-import org.springframework.amqp.rabbit.core.RabbitTemplate;
+import org.springframework.amqp.core.QueueBuilder;
+import org.springframework.amqp.core.TopicExchange;
 import org.springframework.beans.factory.annotation.Value;
-import org.springframework.beans.factory.config.ConfigurableBeanFactory;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
-import org.springframework.context.annotation.Scope;
 
 @Configuration
 public class RabbitConfig {
 
-
-	@Value("${rabbitmq.host}")
-	private String host;
-
-	@Value("${rabbitmq.port}")
-	private int port;
-
-	@Value("${rabbitmq.username}")
-	private String username;
-
-	@Value("${rabbitmq.password}")
-	private String password;
-
-	@Value("${rabbitmq.publisher-confirms}")
-	private boolean publisherConfirm;
-
-	@Value("${rabbitmq.virtual-host}")
-	private String virtualHost;
-
 	@Value("${queue.name.apply}")
 	private String applyQueue;
 
@@ -71,24 +49,11 @@ public class RabbitConfig {
 		return new Queue(quitQueue);
 	}
 
-	@Bean
-	public ConnectionFactory connFactory() {
-		CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
-		connectionFactory.setHost(host);
-		connectionFactory.setPort(port);
-		connectionFactory.setUsername(username);
-		connectionFactory.setPassword(password);
-		connectionFactory.setVirtualHost(virtualHost);
-		connectionFactory.setPublisherConfirms(publisherConfirm); //必须要设置
-		return connectionFactory;
-	}
 
 	@Bean
-	@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
-	//必须是prototype类型
-	public RabbitTemplate rabbitTemplate() {
-		RabbitTemplate template = new RabbitTemplate(connFactory());
-		return template;
+	TopicExchange exchange() {
+		return new TopicExchange("exchange");
 	}
 
+
 }

+ 6 - 0
springboot-ldaptive/src/main/java/org/example/lc/controller/MqController.java

@@ -83,4 +83,10 @@ public class MqController {
         sender.send(quitQueueName, JsonUtil.serialize(resource));
         return Boolean.TRUE;
     }
+
+    @ApiOperation("callback发送")
+    @PostMapping("/callback")
+    public void callback(@Valid @RequestBody QuitResource resource) {
+        sender.send2(quitQueueName, JsonUtil.serialize(resource));
+    }
 }

+ 16 - 0
springboot-ldaptive/src/main/java/org/example/lc/pojo/vo/BaseVo.java

@@ -0,0 +1,16 @@
+package org.example.lc.pojo.vo;
+
+import com.fasterxml.jackson.annotation.JsonSubTypes;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import lombok.Data;
+
+
+@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
+@JsonSubTypes({
+        @JsonSubTypes.Type(value = ModuleVo.class, name = "40"),
+        @JsonSubTypes.Type(value = PersonVo.class, name = "60")
+})
+@Data
+public abstract class BaseVo {
+    public String type;
+}

+ 9 - 0
springboot-ldaptive/src/main/java/org/example/lc/pojo/vo/Module.java

@@ -0,0 +1,9 @@
+package org.example.lc.pojo.vo;
+
+import lombok.Data;
+
+@Data
+public class Module {
+    String moduleName;
+    String org;
+}

+ 12 - 0
springboot-ldaptive/src/main/java/org/example/lc/pojo/vo/ModuleInfo.java

@@ -0,0 +1,12 @@
+package org.example.lc.pojo.vo;
+
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+public class ModuleInfo {
+    String username;
+    String name;
+    List<Module> modules;
+}

+ 11 - 0
springboot-ldaptive/src/main/java/org/example/lc/pojo/vo/ModuleVo.java

@@ -0,0 +1,11 @@
+package org.example.lc.pojo.vo;
+
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+public class ModuleVo {
+    public String type;
+    public List<ModuleInfo> detail;
+}

+ 9 - 0
springboot-ldaptive/src/main/java/org/example/lc/pojo/vo/Person.java

@@ -0,0 +1,9 @@
+package org.example.lc.pojo.vo;
+
+import lombok.Data;
+
+@Data
+public class Person {
+    String name;
+    String age;
+}

+ 8 - 0
springboot-ldaptive/src/main/java/org/example/lc/pojo/vo/PersonVo.java

@@ -0,0 +1,8 @@
+package org.example.lc.pojo.vo;
+
+import lombok.Data;
+
+@Data
+public class PersonVo extends BaseVo {
+    public Person detail;
+}

+ 19 - 5
springboot-ldaptive/src/main/java/org/example/lc/service/mq/QuitReceiver.java

@@ -8,11 +8,16 @@ import org.example.lc.utlis.JsonUtil;
 import org.springframework.amqp.rabbit.annotation.RabbitListener;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.retry.annotation.Backoff;
+import org.springframework.retry.annotation.EnableRetry;
+import org.springframework.retry.annotation.Recover;
+import org.springframework.retry.annotation.Retryable;
 import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
 @Slf4j
 @Component
+@EnableRetry
 public class QuitReceiver {
     @Autowired
     private MainService mainService;
@@ -20,16 +25,25 @@ public class QuitReceiver {
     @Value("${queue.name.quit}")
     private String queueName;
 
+    @Retryable(
+            value = Exception.class,
+            maxAttempts = 3, // 设置最大重试次数
+            backoff = @Backoff(delay = 2000)
+    )
     @RabbitListener(queues = "#{@quitQueueName}")
-    public void process(String msg) {
+    public void process(String msg) throws Exception{
         log.info("{} receive msg:{}", queueName, msg);
         QuitResource resource = JsonUtil.deserialize(msg, QuitResource.class);
         if (resource == null || CollectionUtils.isEmpty(resource.getSystemCodes())) {
             log.error("报文有误");
-            return;
+            throw new Exception("模拟异常");
         }
-        resource.getSystemCodes().stream().forEach(item -> {
-            mainService.quit(item, resource.getInfo());
-        });
+    }
+
+    @Recover
+    public void recover(Exception e, String message) {
+        // 这里处理超过最大重试次数的消息
+        System.out.println("Message exceeded max retry attempts: " + message);
+//        saveMessageToDatabase(message);
     }
 }

+ 24 - 3
springboot-ldaptive/src/main/java/org/example/lc/service/mq/Sender.java

@@ -2,17 +2,38 @@ package org.example.lc.service.mq;
 
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.amqp.core.AmqpTemplate;
+import org.springframework.amqp.rabbit.connection.CorrelationData;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.util.UUID;
+
 @Slf4j
 @Component
-public class Sender {
+public class Sender implements RabbitTemplate.ConfirmCallback {
+    @Autowired
+    private AmqpTemplate amqpTemplate;
+
     @Autowired
-    private AmqpTemplate rabbitTemplate;
+    private RabbitTemplate rabbitTemplate;
 
     public void send(String topic, String msg) {
         log.info("MqSender.send topic:{}, msg:{}", topic, msg);
-        this.rabbitTemplate.convertAndSend(topic, msg);
+        this.amqpTemplate.convertAndSend(topic, msg);
+    }
+
+    public void send2(String topic, String msg){
+        rabbitTemplate.setConfirmCallback(this);
+
+        System.out.println("callbackSender : i am callback sender");
+        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
+        System.out.println("callbackSender UUID: " + correlationData.getId());
+        this.rabbitTemplate.convertAndSend("exchange", "topic.message", msg, correlationData);
+    }
+
+    @Override
+    public void confirm(CorrelationData correlationData, boolean ack, String errorMsg) {
+        System.out.println("callbackSender.confirm");
     }
 }

+ 9 - 7
springboot-ldaptive/src/main/resources/application.properties

@@ -4,12 +4,14 @@ ldap.user-dn=cn=admin,dc=northking,dc=net
 ldap.password=123456
 
 
-rabbitmq.host=localhost
-rabbitmq.port=5672
-rabbitmq.username=guest
-rabbitmq.password=guest
-rabbitmq.publisher-confirms=true
-rabbitmq.virtual-host=/
+spring.rabbitmq.host=localhost
+spring.rabbitmq.port=5672
+spring.rabbitmq.username=guest
+spring.rabbitmq.password=guest
+spring.rabbitmq.publisher-confirm-type=correlated
+spring.rabbitmq.virtual-host=/
 queue.name.apply=applyResourceQueue
 queue.name.recovery=recoveryResourceQueue
-queue.name.quit=quitQueue
+queue.name.quit=quitQueue
+
+exchanges=

+ 32 - 0
springboot-ldaptive/src/test/java/org/example/lc/PBKDF2_SM3_Example.java

@@ -0,0 +1,32 @@
+package org.example.lc;
+
+import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator;
+import org.bouncycastle.crypto.params.KeyParameter;
+import org.bouncycastle.crypto.digests.SM3Digest;
+import java.security.SecureRandom;
+
+public class PBKDF2_SM3_Example {
+    public static void main(String[] args) {
+        String password = "123456";
+        byte[] salt = new byte[16];
+        SecureRandom random = new SecureRandom();
+        random.nextBytes(salt);
+        int iterations = 100000;
+        int keyLength = 256; // 32 bytes * 8 bits
+
+        PKCS5S2ParametersGenerator generator = new PKCS5S2ParametersGenerator(new SM3Digest());
+        generator.init(password.getBytes(), salt, iterations);
+        KeyParameter key = (KeyParameter) generator.generateDerivedMacParameters(keyLength);
+
+        System.out.println("Salt: " + bytesToHex(salt));
+        System.out.println("Derived key: " + bytesToHex(key.getKey()));
+    }
+
+    public static String bytesToHex(byte[] bytes) {
+        StringBuilder sb = new StringBuilder();
+        for (byte b : bytes) {
+            sb.append(String.format("%02x", b));
+        }
+        return sb.toString();
+    }
+}

+ 40 - 0
springboot-ldaptive/src/test/java/org/example/lc/VoTest.java

@@ -0,0 +1,40 @@
+package org.example.lc;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.example.lc.pojo.vo.BaseVo;
+import org.example.lc.pojo.vo.ModuleVo;
+import org.example.lc.pojo.vo.PersonVo;
+import org.example.lc.utlis.JsonUtil;
+
+public class VoTest {
+    private static final ObjectMapper objectMapper = new ObjectMapper();
+
+    public static void main(String[] args) {
+        String moduleVoJson = "{\"type\":\"40\",\"detail\":[{\"username\":\"san.zhang001\",\"name\":\"张三\",\"modules\":[{\"moduleName\":\"nlpmp-dcp\",\"org\":\"rdc\"},{\"moduleName\":\"nlpmp-asf-gpt\",\"org\":\"rdc\"}]},{\"username\":\"si.li002\",\"name\":\"李四\",\"modules\":[{\"moduleName\":\"decp-permission\",\"org\":\"dept1\"}]}]}";
+        String personJson = "{\"type\":\"60\",\"detail\":{\"name\":\"张三\",\"age\":\"20\"}}";
+
+        try {
+            ModuleVo obj1 = objectMapper.readValue(moduleVoJson, ModuleVo.class);
+//            handleObject(obj1);
+
+            BaseVo obj2 = objectMapper.readValue(personJson, BaseVo.class);
+            handleObject(obj2);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    private static void handleObject(BaseVo baseType) {
+//        if (baseType instanceof ModuleVo) {
+//            System.out.println(JsonUtil.serialize((ModuleVo) baseType));
+//        } else
+            if (baseType instanceof PersonVo) {
+            System.out.println(JsonUtil.serialize((PersonVo) baseType));
+        } else {
+            throw new IllegalArgumentException("Unknown type: " + baseType.getClass().getName());
+        }
+    }
+
+
+}