Spring security/Spring session/Web sockets

限于喜欢 提交于 2019-12-23 01:49:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!