2014-09-03 477 views
1

Spring Security是非常好的框架,广泛用于身份验证&授权。安全Spring-Webscoket使用spring-security并从websocket访问主体消息

我有一个需要使用j_spring_security_check进行身份验证的应用程序,并且只有授权用户才能向websocket处理程序发出请求。

我已经配置的Spring Security按照http://malalanayake.wordpress.com/2014/06/27/spring-security-on-rest-api/

而且我已经配置的WebSocket按http://syntx.io/using-websockets-in-java-using-spring-4/

我想MyPrincipal主要目的,是要访问从handleTextMessage处理程序按照以下:

@Override 
    protected void handleTextMessage(WebSocketSession session, 
      TextMessage message) throws Exception { 
     System.out.println("Protocol: "+session.getAcceptedProtocol()); 
     TextMessage returnMessage = new TextMessage(message.getPayload() 
       + " received at server"); 
     System.out.println("myAttrib=" 
       + session.getAttributes().get("myAttrib")); 
     MyPrincipal user = (MyPrincipal) ((Authentication) session 
       .getPrincipal()).getPrincipal(); 
     System.out.println("User: " + user.getUserId()); 
     session.sendMessage(returnMessage); 
    } 

请尽快重播。

回答

1

在WebSocket的配置添加HttpSessionHandshakeInterceptor允许弹簧安全主体对象传递从SpringSecurityContextWebsocketSession

编辑: HandshakeInterceptor.java

public class HandshakeInterceptor extends HttpSessionHandshakeInterceptor{ 

    @Override 
    public boolean beforeHandshake(ServerHttpRequest request, 
      ServerHttpResponse response, WebSocketHandler wsHandler, 
      Map<String, Object> attributes) throws Exception { 
     System.out.println("Before Handshake"); 
     return super.beforeHandshake(request, response, wsHandler, attributes); 
    } 

    @Override 
    public void afterHandshake(ServerHttpRequest request, 
      ServerHttpResponse response, WebSocketHandler wsHandler, 
      Exception ex) { 
     System.out.println("After Handshake"); 
     super.afterHandshake(request, response, wsHandler, ex); 
    } 

} 

websocket.xml

<bean id="websocket" class="co.syntx.example.websocket.handler.WebsocketEndPoint"/> 

<websocket:handlers> 
    <websocket:mapping path="/websocket" handler="websocket"/> 
    <websocket:handshake-interceptors> 
    <bean class="co.syntx.example.websocket.HandshakeInterceptor"/> 
    </websocket:handshake-interceptors> 
</websocket:handlers> 
+0

我想将Spring安全主体传递给WebSocket,我在代码中添加了HandShakeInterceptor,但它仍然抛出了401错误。我没有websocket.xml文件,如何配置它或可以在代码中执行? – 2017-02-15 07:48:27

+0

将@Component注释添加到类中。假设你没有xml - 你有一些弹簧配置注释或者smth的组件扫描;如果你谈论如何配置websocket - 有特殊的教程(有整个类的行为) – Sarief 2017-04-07 10:24:04

1

确保您使用Spring Security来保护您的WebSocket Endpoint并完成登录。 (401如果没有这样做。)

Testet与3.2.7和4.0.2.RELEASE

两个版本有:

  • session.getPrincipal() < - 一个在这里
  • SecurityContextHolder.getContext().getAuthentication() <价值 - - null此处

    @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
    protected void configure(HttpSecurity http) throws Exception { 
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED) 
         .and() 
         .httpBasic().and() 
         .authorizeRequests() 
    
+0

当websocket客户端连接到服务器与弹簧安全,如何保护端点时,我得到了401错误。你能详细描述一下吗? – 2017-02-15 07:44:19