refactor: 添加 websocket 连接认证拦截器实现点对点指定用户发送消息;移除 easy-captcha 替换为 hutool-captcha验证码实现代码简化;重构认证接口控制层代码。

This commit is contained in:
haoxr
2023-09-12 18:25:16 +08:00
parent 87fcf022ba
commit 9453600715
37 changed files with 290 additions and 358 deletions

View File

@@ -1,62 +0,0 @@
package com.youlai.system.config;
import com.youlai.system.common.enums.CaptchaTypeEnum;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.awt.*;
/**
* EasyCaptcha 配置类
*
* @author haoxr
* @since 2023/03/24
*/
@ConfigurationProperties(prefix = "easy-captcha")
@Configuration
@Data
public class CaptchaConfig {
/**
* 验证码类型
*/
private CaptchaTypeEnum type = CaptchaTypeEnum.ARITHMETIC;
/**
* 验证码缓存过期时间(单位:秒)
*/
private long ttl = 120l;
/**
* 验证码内容长度
*/
private int length = 4;
/**
* 验证码宽度
*/
private int width = 120;
/**
* 验证码高度
*/
private int height = 36;
/**
* 验证码字体
*/
private String fontName = "Verdana";
/**
* 字体风格
*/
private Integer fontStyle = Font.PLAIN;
/**
* 字体大小
*/
private int fontSize = 20;
}

View File

@@ -33,8 +33,6 @@ public class WebMvcConfig implements WebMvcConfigurer {
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = jackson2HttpMessageConverter.getObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
// 后台Long值传递给前端精度丢失问题JS最大精度整数是Math.pow(2,53)

View File

@@ -1,28 +1,32 @@
package com.youlai.system.config;
import com.youlai.system.interceptor.AuthChannelInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
/**
* WebSocket 配置
* <p>
* STOMP 不是一个传输协议,而是一个简单的文本消息协议,定义消息格式和交换规则
* WebSocket 配置
*
* @author haoxr
* @since 2.3.0
* @since 2.4.0
*/
@Configuration
@ConditionalOnProperty(name = "system.config.websocket-enabled")// system.config.websocket-enabled = true 才会自动装配
@EnableWebSocketMessageBroker // 启用WebSocket消息代理功能和配置STOMP协议实现实时双向通信和消息传递
@RequiredArgsConstructor
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
private final AuthChannelInterceptor authChannelInterceptor;
/**
* 配置和注册WebSocket端点(endpoints)
* 注册一个端点,客户端通过这个端点进行连接
*/
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
@@ -35,9 +39,7 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
/**
* 配置消息代理(Message Broker)
* <p>
* 设置消息传输的规则和前缀
* 配置消息代理
*/
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
@@ -51,4 +53,14 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
registry.setUserDestinationPrefix("/user");
}
/**
* 配置客户端入站通道拦截器
*
* @param registration 通道注册器
*/
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.interceptors(authChannelInterceptor);
}
}