小编Tre*_*Bye的帖子

Spring Websocket Security 抛出 AccessDeniedException

让我先说应该拒绝访问我的场景。使用 Spring Security 4 内置 Spring Boot。我允许任何人连接到 websocket 并订阅主题,但我使用以下 web 套接字安全配置来确保向主题发送消息的能力:

@Configuration
public class WebSocketSecurityConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {

    @Override
    protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
        messages
                .simpSubscribeDestMatchers("/main-page-feed/thought-queue/**").permitAll()
                .simpDestMatchers("/thought-bubble/push-to-queue/**").authenticated();


    }

    @Override
    protected boolean sameOriginDisabled() {
        return true;
    }

}
Run Code Online (Sandbox Code Playgroud)

因此,当您尝试/thought-bubble/push-to-queue在未经身份验证的情况下向其发送消息时,它会拒绝您(这是正确的,我想强调这一点,因为我能找到的唯一其他问题是当异常被错误抛出时)并抛出一个AccessDeniedException. 我不明白的是,与 websocket 安全性相反,Spring HTTP 安全性何时锁定某些内容并拒绝访问,它不会抛出异常,它只是发送一个 HTTP 状态。我试过使用@ExceptionHandler, AccessDenied 处理程序,但我尝试过的任何东西都无法捕获和处理此异常。下面是堆栈跟踪和其他相关文件,任何想法都值得赞赏,因为我很困惑。我试过在调试中单步调试源代码,但我并没有真正看到问题是什么。

堆栈跟踪:

org.springframework.messaging.MessageDeliveryException: Failed to send message to ExecutorSubscribableChannel[clientInboundChannel]; nested exception is org.springframework.security.access.AccessDeniedException: Access is denied
    at org.springframework.messaging.support.AbstractMessageChannel.send(AbstractMessageChannel.java:127) ~[spring-messaging-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.messaging.support.AbstractMessageChannel.send(AbstractMessageChannel.java:104) ~[spring-messaging-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.web.socket.messaging.StompSubProtocolHandler.handleMessageFromClient(StompSubProtocolHandler.java:298) …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security spring-boot spring-websocket

5
推荐指数
1
解决办法
7577
查看次数