feat: 旋转图片验证使用多线程

This commit is contained in:
landaiqing
2024-05-12 15:21:51 +08:00
parent 1683fdaacf
commit c84289fe0a
22 changed files with 1082 additions and 28 deletions

View File

@@ -16,7 +16,7 @@ import javax.annotation.Resource;
@Slf4j
public class SmsInitConfig implements SmartLifecycle {
@Resource
SmsConfig smsConfig;
SmsReadConfig smsReadConfig;
@Resource
SmsConfigRpc smsConfigRpc;
@@ -33,7 +33,7 @@ public class SmsInitConfig implements SmartLifecycle {
log.info("短信配置获取失败!");
}
// 初始化短信配置
SmsFactory.createSmsBlend(smsConfig, configInfo.getConfigValue());
SmsFactory.createSmsBlend(smsReadConfig, configInfo.getConfigValue());
}
@Override

View File

@@ -2,7 +2,6 @@ package com.schisandra.auth.application.config;
import com.schisandra.auth.application.factory.SmsTypeHandlerFactory;
import com.schisandra.auth.application.handler.SchisandraSmsTypeHandler;
import org.dromara.sms4j.core.datainterface.SmsReadConfig;
import org.dromara.sms4j.provider.config.BaseConfig;
import org.springframework.stereotype.Component;
@@ -19,7 +18,7 @@ import java.util.List;
* @Version: 1.0
*/
@Component
public class SmsConfig implements SmsReadConfig {
public class SmsReadConfig implements org.dromara.sms4j.core.datainterface.SmsReadConfig {
@Resource

View File

@@ -0,0 +1,28 @@
package com.schisandra.auth.application.config;
import com.schisandra.auth.application.factory.CustomNameThreadFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 线程池的config管理
*
* @author: landaiqing
* @date: 2024/2/18
*/
@Configuration
public class ThreadPoolConfig {
@Bean(name = "rotateCaptchaThreadPool")
public ThreadPoolExecutor getLabelThreadPool() {
return new ThreadPoolExecutor(20, 100, 5,
TimeUnit.SECONDS, new LinkedBlockingDeque<>(40),
new CustomNameThreadFactory("rotateCaptcha"),
new ThreadPoolExecutor.CallerRunsPolicy());
}
}

View File

@@ -4,7 +4,9 @@ import com.schisandra.auth.application.dto.SchisandraCaptchaDTO;
import com.schisandra.auth.common.entity.CaptchaResult;
import com.schisandra.auth.common.redis.RedisUtil;
import com.schisandra.auth.common.utils.AESUtils;
import com.schisandra.auth.common.utils.MD5Util;
import com.schisandra.auth.common.utils.RotateImageUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@@ -15,9 +17,11 @@ import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
@@ -40,25 +44,28 @@ public class ReactRotateCaptchaController {
*/
@PostMapping("get")
public CaptchaResult get() throws Exception {
HashMap<String,String> map = new HashMap<>();
RotateImageUtils rotateImageUtils = new RotateImageUtils();
InputStream inputStream= ReactRotateCaptchaController.class.getClassLoader().getResourceAsStream("image/test1.jpg");
BufferedImage image = ImageIO.read(inputStream);
Random random = new Random();
double randomNumber = random.nextInt(280) + 40;
String key = AESUtils.getKey();
String token = AESUtils.encrypt(String.valueOf(randomNumber), key);
String prefix = redisUtil.buildKey(authRotateCaptchaPrefix, token);
redisUtil.setNx(prefix, String.valueOf(randomNumber), 60L, TimeUnit.SECONDS);
try {
BufferedImage image1 = rotateImageUtils.rotateImage(image, randomNumber, Color.black);
map.put("token",token);
map.put("str", RotateImageUtils.BufferedImageToBase64(image1));
return CaptchaResult.ok(map);
} catch (Exception e) {
return CaptchaResult.fail();
}
CompletableFuture<HashMap> futurePrice = CompletableFuture.supplyAsync(() -> {
HashMap<String, String> map = new HashMap<>();
try {
Random random = new Random();
double randomNumber = random.nextInt(280) + 40;
InputStream inputStream = ReactRotateCaptchaController.class.getClassLoader().getResourceAsStream("image/test1.jpg");
BufferedImage image = ImageIO.read(inputStream);
String key = AESUtils.getKey();
String token = AESUtils.encrypt(String.valueOf(randomNumber), key);
String prefix = redisUtil.buildKey(authRotateCaptchaPrefix, token);
redisUtil.setNx(prefix, String.valueOf(randomNumber), 60L, TimeUnit.SECONDS);
BufferedImage image1 = rotateImageUtils.rotateImage(image, randomNumber, Color.black);
String img = RotateImageUtils.BufferedImageToBase64(image1);
map.put("token", token);
map.put("str", img);
} catch (Exception e) {
log.error("ReactRotateCaptchaController.get.error:{}", e.getMessage(), e);
}
return map;
});
return CaptchaResult.ok(futurePrice.join());
}

View File

@@ -0,0 +1,49 @@
package com.schisandra.auth.application.factory;
import org.apache.commons.lang3.StringUtils;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 自定义名称的线程工厂
*
* @author: landaiqing
* @date: 2024/2/18
*/
public class CustomNameThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
public CustomNameThreadFactory(String name) {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
if (StringUtils.isBlank(name)) {
name = "pool";
}
namePrefix = name + "-" +
poolNumber.getAndIncrement() +
"-thread-";
}
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
// 设置线程为非守护线程
if (t.isDaemon()){
t.setDaemon(false);
}
// 设置线程优先级为正常优先级
if (t.getPriority() != Thread.NORM_PRIORITY){
t.setPriority(Thread.NORM_PRIORITY);
}
return t;
}
}

View File

@@ -0,0 +1,51 @@
package com.schisandra.auth.common.utils;
import com.alibaba.fastjson.JSON;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
/**
* @Classname CacheUtil
* @Author: landaiqing
* @CreateTime: 2024-03-03 21:31
* @Description: 缓存工具类
* @Version: 1.0
*/
@Component
public class CacheUtil<K, V> {
private Cache<String, String> localCache =
CacheBuilder.newBuilder()
.maximumSize(5000)
.expireAfterWrite(10, TimeUnit.SECONDS)
.build();
public List<V> getResult(String cacheKey, Class<V> clazz,
Function<String, List<V>> function) {
List<V> resultList = new ArrayList<>();
String content = localCache.getIfPresent(cacheKey);
if (StringUtils.isNotBlank(content)) {
resultList = JSON.parseArray(content, clazz);
} else {
resultList = function.apply(cacheKey);
if (!CollectionUtils.isEmpty(resultList)) {
localCache.put(cacheKey, JSON.toJSONString(resultList));
}
}
return resultList;
}
public Map<K, V> getMapResult(String cacheKey, Class<V> clazz,
Function<String, Map<K, V>> function) {
return new HashMap<>();
}
}