Spring security Spring安全性:如何使用zoneinfo验证WebSocket

Spring security Spring安全性:如何使用zoneinfo验证WebSocket,spring-security,jwt,Spring Security,Jwt,有人能建议如何使用Spring安全框架保护web套接字端点吗 我有一个用Spring Security保护的应用程序。 其中一个端点是web套接字。 在web套接字处理程序中,我需要对连接到web套接字的用户进行身份验证。 具体来说,我需要获取userid和区域/租户id 如果我在spring-security.xml(请参阅下面的文件)中为websocket端点使用标记sec:http,这确实会触发登录,然后在websocket的@onOpen(会话会话)处理程序中触发 调用session.g

有人能建议如何使用Spring安全框架保护web套接字端点吗

我有一个用Spring Security保护的应用程序。 其中一个端点是web套接字。 在web套接字处理程序中,我需要对连接到web套接字的用户进行身份验证。 具体来说,我需要获取userid和区域/租户id

如果我在spring-security.xml(请参阅下面的文件)中为websocket端点使用标记sec:http,这确实会触发登录,然后在websocket的@onOpen(会话会话)处理程序中触发 调用session.getUserPrincipal()时,返回的主体内部有一个正确的用户名

但是,我还需要区域/租户id信息

我试图使用应该包含它的SecurityContextHolder.getContext().getAuthentication(),但调用返回null。 显然,sec:http不会导致为web套接字请求创建身份验证对象

有人提到过我

这似乎表明,要为websocket端点获取包含区域信息的验证器,我需要在spring-security.xml中添加以下部分:

<sec:websocket-message-broker>
    <sec:intercept-message pattern="/WebSocket.svc" access="isAuthenticated()" />
</sec:websocket-message-broker>
<bean id="springSecurityMessagePathMatcher"    
class="org.springframework.util.AntPathMatcher"/>
我使用SpringSecurity 4.1.2,MasterSpring使用4.3.1

从Spring文档中还不清楚sec:websocket messagebrokersec:http是否应该与web套接字端点一起使用,或者是相互排斥的

谢谢你的建议

谢尔盖

另外,我的spring-security.xml如下所示:

<?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:oauth="http://www.springframework.org/schema/security/oauth2"
    xmlns:sec="http://www.springframework.org/schema/security"
    xmlns:websocket="http://www.springframework.org/schema/websocket"
    xsi:schemaLocation="http://www.springframework.org/schema/security/oauth2
        http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security-4.1.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">

    <sec:http pattern="/Consumer.svc/**" create-session="never"
        entry-point-ref="oauthAuthenticationEntryPoint"
        access-decision-manager-ref="accessDecisionManager"
        authentication-manager-ref="authenticationManager"
        use-expressions="true">
        <sec:anonymous enabled="false" />
        <sec:intercept-url pattern="/Consumer.svc/**" access="isAuthenticated()" />
        <sec:custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" />
        <sec:access-denied-handler ref="oauthAccessDeniedHandler" />
    </sec:http>

    <sec:http pattern="/WebSocket.svc" create-session="never"
        entry-point-ref="oauthAuthenticationEntryPoint"
        access-decision-manager-ref="accessDecisionManager"
        authentication-manager-ref="authenticationManager"
        use-expressions="true">
        <sec:anonymous enabled="false" />
        <sec:intercept-url pattern="/WebSocket.svc" access="isAuthenticated()"  />
        <sec:custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" />
        <sec:access-denied-handler ref="oauthAccessDeniedHandler" />
    </sec:http>

    <sec:websocket-message-broker>
        <sec:intercept-message pattern="/WebSocket.svc" access="isAuthenticated()" />
    </sec:websocket-message-broker>

    <bean id="oauthAuthenticationEntryPoint"
        class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
    </bean>

    <bean id="oauthWebExpressionHandler"
        class="org.springframework.security.oauth2.provider.expression.OAuth2WebSecurityExpressionHandler">
    </bean>

    <bean id="accessDecisionManager"
        class="org.springframework.security.access.vote.UnanimousBased">
        <constructor-arg>
            <list>
                <bean class="org.springframework.security.web.access.expression.WebExpressionVoter">
                    <property name="expressionHandler" ref="oauthWebExpressionHandler" />
                </bean>
                <bean class="org.springframework.security.access.vote.AuthenticatedVoter"  />
            </list>
        </constructor-arg>
    </bean>

    <sec:authentication-manager alias="authenticationManager"/>

    <oauth:resource-server id="resourceServerFilter"
        resource-id="springsec" token-services-ref="offlineTokenServices" />

    <bean id="oauthAccessDeniedHandler"
        class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler" />

    <!-- ... also some other elements here ... -->

</beans>

有必要将以下内容添加到pom.xml中:

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-messaging</artifactId>
        <version>4.1.2.RELEASE</version>
    </dependency>

     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-messaging</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-websocket</artifactId>
        <version>${spring.version}</version>
    </dependency>

org.springframework.security
spring安全消息
4.1.2.1发布
org.springframework
spring消息
${spring.version}
org.springframework
弹簧网袋
${spring.version}
下面是spring-security.xml:

<sec:websocket-message-broker>
    <sec:intercept-message pattern="/WebSocket.svc" access="isAuthenticated()" />
</sec:websocket-message-broker>
<bean id="springSecurityMessagePathMatcher"    
class="org.springframework.util.AntPathMatcher"/>

现在,Spring不会在启动时抛出异常,并且可以很好地解析Spring-security.xml,但它也不起作用SecurityContextHolder.getContext().getAuthentication()仍然返回null