我们正在尝试结合使用 spring session、spring security 和 websockets 来实现 websocket API 的安全性,而不使用 cookie。
理想情况下,我们将使用授权标头或使用 websocket/stomp 消息进行身份验证,但这似乎不适用于当前的 spring websocket 支持。
我们正在使用预身份验证提供程序来验证查询参数令牌并登录用户。我可以看到正确的用户在预身份验证中被拉出以进行握手,但 SecurityContext 对连接到 websocket 的拦截器不可用.
我们的spring安全配置是
<!-- API security -->
<security:http use-expressions="false" realm="api" authentication-manager-ref="apiAuthenticationManager" entry-point-ref="accessDeniedAuthEntryPoint" pattern="/api/**" create-session="never">
<security:custom-filter position="FIRST" ref="sessionRepositoryFilter" />
<security:custom-filter position="PRE_AUTH_FILTER" ref="headerTokenAuthFilter" />
<security:intercept-url pattern="/api/**" access="ROLE_USER" />
<security:access-denied-handler ref="accessDeniedHandler" />
</security:http>
<security:authentication-manager id="apiAuthenticationManager">
<security:authentication-provider ref="preauthAuthProvider" />
</security:authentication-manager>
<bean id="headerTokenAuthFilter" class="com.example.server.security.HeaderTokenAuthFilter" >
<property name="authenticationManager" ref="apiAuthenticationManager"/>
<property name="continueFilterChainOnUnsuccessfulAuthentication" value="false"/>
<property name="checkForPrincipalChanges" value="true"/>
<property name="sessionRepository" ref="sessionRepository" />
</bean>
<bean id="accessDeniedHandler" class="org.springframework.security.web.access.AccessDeniedHandlerImpl" />
<bean id="accessDeniedAuthEntryPoint" class="org.springframework.security.web.authentication.Http403ForbiddenEntryPoint" />
<bean id="sessionRepository" class="org.springframework.session.data.redis.RedisOperationsSessionRepository">
<constructor-arg ref="jedisConnectionFactory"/>
</bean>
<bean id="sessionRepositoryFilter" class="org.springframework.session.web.http.SessionRepositoryFilter">
<constructor-arg ref="sessionRepository"/>
</bean>
我们的 websocket 配置是
@Configuration
@EnableScheduling
@EnableWebSocketMessageBroker
public class WebsocketConfiguration extends AbstractSessionWebSocketMessageBrokerConfigurer<ExpiringSession> {
@Inject
private AuthenticationValidationInterceptor authenticationValidationInterceptor;
@Inject
private SelectorQuotingInterceptor selectorQuotingInterceptor;
@Inject
private SelectorValidationInterceptor selectorValidationInterceptor;
@Override
protected void configureStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/stomp")
.withSockJS().setSessionCookieNeeded(false);
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableStompBrokerRelay("/topic")
.setRelayHost("localhost")
.setRelayPort(7672);
registry.setApplicationDestinationPrefixes("/api/data/streaming");
}
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.setInterceptors(
authenticationValidationInterceptor,
selectorValidationInterceptor,
selectorQuotingInterceptor);
}
@Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
}
}