Spring Boot 3配置WebSocket安全时找不到MessageMatcherDelegatingAuthorizationManager类
问题排查与解决
1. 类的正确位置
在Spring Security 6.x(对应Spring Boot 3.x)版本中,MessageMatcherDelegatingAuthorizationManager的全类名已更新为:
org.springframework.security.messaging.access.intercept.MessageMatcherDelegatingAuthorizationManager
若文档标注的是旧包路径(比如org.springframework.security.messaging.access),属于文档滞后问题,请使用上述正确路径尝试导入。
2. 缺失必要依赖
spring-boot-starter-security默认不包含WebSocket消息安全所需的spring-security-messaging模块,你需要在pom.xml中手动添加该依赖:
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-messaging</artifactId> </dependency>
由于你使用spring-boot-starter-parent:3.0.4做依赖版本管理,无需指定版本号,会自动匹配兼容的Spring Security 6.0.3版本。
3. 验证配置示例
添加依赖后,可参考以下示例配置WebSocket安全:
@Configuration @EnableWebSocketSecurity public class WebSocketSecurityConfig { @Bean public MessageMatcherDelegatingAuthorizationManager messageAuthorizationManager() { MessageMatcherDelegatingAuthorizationManager.Builder builder = MessageMatcherDelegatingAuthorizationManager.builder(); // 配置消息权限规则,例如允许认证用户订阅指定目的地 builder.simpDestMatchers("/topic/**").authenticated(); // 其他规则配置... return builder.build(); } }
内容的提问来源于stack exchange,提问作者Szőke Attila
相关产品推荐
相关产品推荐

