问题
We are attempting to use a combination of spring session, spring security and websockets to implement security for a websocket API without using cookies.
Ideally we would be using a authorization header or authentication using the websocket/stomp messages but this does not seem to be possible with the current spring websocket support.
We are using a pre-auth provider to validate a query parameter token and log the user in. I can see that the proper user is pulled out in the pre-auth for the handshake but the SecurityContext is not available to interceptors wired into the websocket.
Our spring security configuration is
<!-- 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>
Our websocket configuration is
@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) {
}
}
回答1:
Coworker of the accursed here. Our configuration is largely correct but our issue stemmed from a bit of a misunderstanding around the security context and its availability from the websocket side of things.
Comments gathered from various sub-issues of https://jira.spring.io/browse/SEC-2179 led us to grabbing the logged in user principal from the message in the interceptor
StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(message);
Principal userPrincipal = headerAccessor.getUser();
Rather than
SecurityContextHolder.getContext().getAuthentication().getName();
来源:https://stackoverflow.com/questions/28015429/spring-security-spring-session-web-sockets