首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Spring Security with WebSockets -Formated403

Spring Security with WebSockets -Formated403
EN

Stack Overflow用户
提问于 2015-06-02 20:06:23
回答 1查看 3.9K关注 0票数 17

我已经在Spring中实现了WebSocket。一切都很好,但最近我决定实现Spring Security。

我的MessageBroker看起来像这样:

代码语言:javascript
复制
@Configuration
@EnableWebSocketMessageBroker
@Component("messageBroker")
public class MessageBroker implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
        stompEndpointRegistry.addEndpoint("/graphs").withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry messageBrokerRegistry) {
    }

    @Override
    public void configureClientInboundChannel(ChannelRegistration channelRegistration) {
    }

    @Override
    public void configureClientOutboundChannel(ChannelRegistration channelRegistration) {
    }

    @Override
    public boolean configureMessageConverters(List<MessageConverter> messageConverters) {
        messageConverters.add(new MappingJackson2MessageConverter());
        return false;
    }

}

我的JS客户端看起来像这样:

代码语言:javascript
复制
var socket = new SockJS('/server/graphs');
var client = Stomp.over(socket);

client.connect({}, function (frame) {
    client.subscribe("/data", function (message) {
        console.log('GET MESSAGE :' + message.body);
        var test = JSON.parse(message.body);
        var point = [ (new Date()).getTime(), parseInt(25) ];
        var shift = randomData.data.length > 60;
        randomData.addPoint(point, true, shift);
    });

});

Spring安全配置:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/security
       http://www.springframework.org/schema/security/spring-security.xsd">

    <security:http auto-config='true' use-expressions="true" disable-url-rewriting="true">
        <security:intercept-url pattern="/login/**" access="isAnonymous()"/>
        <security:intercept-url pattern="/index/**" access="isAuthenticated()" />
        <security:intercept-url pattern="/volumegraph/**" access="isAuthenticated()" />
        <security:intercept-url pattern="/graphs/**" access="permitAll()" />
        <security:intercept-url pattern="/graphs/**/**" access="permitAll()" />
        <security:form-login login-page="/" login-processing-url="/" authentication-failure-url="/" always-use-default-target="true"/>
        <security:csrf/>
        <security:logout logout-success-url="/login"/>
        <security:headers>
            <security:frame-options></security:frame-options>
        </security:headers>
    </security:http>

    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="user" password="password" authorities="ROLE_USER"/>
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>

</beans>

通过我的JS客户端订阅后,我收到:

代码语言:javascript
复制
Opening Web Socket...
sockjs.js:1213 WebSocket connection to 'ws://localhost:8080/server/graphs/651/kyzdihld/websocket' failed: Error during WebSocket handshake: Unexpected response code: 404
sockjs.js:807 POST http://localhost:8080/server/graphs/651/zx7zdre7/xhr_streaming 403 (Forbidden)AbstractXHRObject._start @ sockjs.js:807(anonymous function) @ sockjs.js:834
sockjs.js:807 POST http://localhost:8080/server/graphs/651/o6eg5ikc/xhr 403 (Forbidden)AbstractXHRObject._start @ sockjs.js:807(anonymous function) @ sockjs.js:834
stomp.js:122 Whoops! Lost connection to undefined

因此,我决定在Security Config中添加以下代码:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/security
       http://www.springframework.org/schema/security/spring-security.xsd">

<security:websocket-message-broker>
    <!--<security:intercept-message pattern="/graphs/**" access="permitAll()"/>-->
    <security:intercept-message pattern="/**" access="permitAll()"/>
    <security:intercept-message type="SUBSCRIBE" access="permitAll()"/>
    <security:intercept-message type="CONNECT" access="permitAll()"/>
</security:websocket-message-broker>

</beans>

但在那之后,我收到了这样的错误:

代码语言:javascript
复制
NoSuchBeanDefinitionException: No bean named 'springSecurityMessagePathMatcher' is defined

我不知道如何定义这样的bean,所以我在下面创建了这个类:

代码语言:javascript
复制
@Configuration
public class WebSocketSecurityConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {

    protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
        messages.simpDestMatchers("/graphs/*").permitAll();
        messages.simpDestMatchers("/**").permitAll();
    }
}

但是在编译过程中,我收到了这样的错误:

代码语言:javascript
复制
java: cannot access org.springframework.beans.factory.SmartInitializingSingleton
  class file for org.springframework.beans.factory.SmartInitializingSingleton not found

我真的不知道如何解决这个问题:(我必须补充说,我使用的是Spring Core 4.0.1.RELEASE和Spring Messaging 4.0.1.RELEASE。所有与Spring Security相关的库都是4.0.1.RELEASE版本。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30595631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档