refactor: 验证码重构

This commit is contained in:
haoxr
2023-12-16 14:10:42 +08:00
parent 31a2fbeb42
commit a1a1a64f73
8 changed files with 103 additions and 191 deletions

View File

@@ -0,0 +1,55 @@
package com.youlai.system.plugin.captcha;
import cn.hutool.captcha.*;
import cn.hutool.captcha.generator.CodeGenerator;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.captcha.generator.RandomGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.awt.*;
/**
* 验证码自动装配配置
*
* @author haoxr
* @since 2023/11/24
*/
@Configuration
public class CaptchaConfig {
@Autowired
private CaptchaProperties captchaProperties;
/**
* 验证码文字生成器
*
* @return CodeGenerator
*/
@Bean
public CodeGenerator codeGenerator() {
String codeType = captchaProperties.getCode().getType();
int codeLength = captchaProperties.getCode().getLength();
if ("math".equalsIgnoreCase(codeType)) {
return new MathGenerator(codeLength);
} else if ("random".equalsIgnoreCase(codeType)) {
return new RandomGenerator(codeLength);
} else {
throw new IllegalArgumentException("Invalid captcha generator type: " + codeType);
}
}
/**
* 验证码字体
*/
@Bean
public Font captchaFont() {
String fontName = captchaProperties.getFont().getName();
int fontSize = captchaProperties.getFont().getSize();
int fontWight = captchaProperties.getFont().getWeight();
return new Font(fontName, fontWight, fontSize);
}
}

View File

@@ -1,87 +0,0 @@
package com.youlai.system.plugin.captcha;
import cn.hutool.captcha.AbstractCaptcha;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.generator.CodeGenerator;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.captcha.generator.RandomGenerator;
import com.youlai.system.model.dto.CaptchaResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 验证码自动装配配置
*
* @author haoxr
* @since 2023/11/24
*/
@Configuration
public class CaptchaGenerator {
@Autowired
private CaptchaProperties captchaProperties;
/**
* 验证码文字生成器
*
* @return CodeGenerator
*/
@Bean
public CodeGenerator codeGenerator() {
String codeType = captchaProperties.getCode().getType();
int codeLength = captchaProperties.getCode().getLength();
if ("math".equalsIgnoreCase(codeType)) {
return new MathGenerator(codeLength);
} else if ("random".equalsIgnoreCase(codeType)) {
return new RandomGenerator(codeLength);
} else {
throw new IllegalArgumentException("Invalid captcha generator type: " + codeType);
}
}
/**
* 生成验证码
*
* @return CaptchaModel 验证码
*/
public CaptchaModel generate() {
AbstractCaptcha captcha = getCaptcha();
captcha.createCode();
return new CaptchaModel(captcha.getCode(), captcha.getImageBase64Data());
}
/**
* 验证码类
*
* @return AbstractCaptcha
*/
public AbstractCaptcha getCaptcha() {
AbstractCaptcha captcha = null;
String type = captchaProperties.getType();
int width = captchaProperties.getWidth();
int height = captchaProperties.getHeight();
int interfereCount = captchaProperties.getInterfereCount();
int codeLength = captchaProperties.getCode().getLength();
if ("circle".equalsIgnoreCase(type)) {
captcha = new CircleCaptcha(width, height, codeLength, interfereCount);
} else if ("gif".equalsIgnoreCase(type)) {
return null;
} else if ("line".equalsIgnoreCase(type)) {
return null;
} else if ("shear".equalsIgnoreCase(type)) {
return null;
} else {
throw new IllegalArgumentException("Invalid captcha type: " + type);
}
captcha.setGenerator(codeGenerator());
return captcha;
}
}

View File

@@ -1,24 +0,0 @@
package com.youlai.system.plugin.captcha;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 验证码对象
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CaptchaModel {
/**
* 验证码编码
*/
private String code;
/**
* 验证码图片Base64
*/
private String base64;
}

View File

@@ -34,6 +34,11 @@ public class CaptchaProperties {
*/
private int interfereCount;
/**
* 文本透明度
*/
private Float textAlpha;
/**
* 验证码过期时间,单位:秒
*/

View File

@@ -1,68 +0,0 @@
package com.youlai.system.plugin.websocket;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.messaging.SessionConnectedEvent;
import org.springframework.web.socket.messaging.SessionDisconnectEvent;
import org.springframework.web.socket.messaging.SessionSubscribeEvent;
import org.springframework.web.socket.messaging.SessionUnsubscribeEvent;
import java.security.Principal;
/**
* Websocket 客户端事件监听器
*
* @author haoxr
* @since 2023/10/10
*/
@Component
@Slf4j
public class WebSocketEventListener {
/**
* 监听客户端连接事件
*
* @param event 连接事件对象
*/
@EventListener
public void handleWebSocketConnectListener(SessionConnectedEvent event) {
Principal user = event.getUser();
log.info("客户端连接成功");
}
/**
* 监听客户端断开连接事件
*
* @param event 断开连接事件对象
*/
@EventListener
public void handleWebSocketDisconnectListener(SessionDisconnectEvent event) {
log.info("客户端断开连接");
}
/**
* 监听客户端订阅事件
*
* @param event 订阅事件对象
*/
@EventListener
public void handleSubscription(SessionSubscribeEvent event) {
log.info("客户端订阅:{}", JSONUtil.toJsonStr(event.getMessage()));
}
/**
* 监听客户端取消订阅事件
*
* @param event 取消订阅事件对象
*/
@EventListener
public void handleUnSubscription(SessionUnsubscribeEvent event) {
log.info("客户端取消订阅:{}", JSONUtil.toJsonStr(event.getMessage()));
}
}