Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Websocket连接链接到Spring Security中登录的用户_Spring_Spring Mvc_Session_Spring Security_Websocket - Fatal编程技术网

将Websocket连接链接到Spring Security中登录的用户

将Websocket连接链接到Spring Security中登录的用户,spring,spring-mvc,session,spring-security,websocket,Spring,Spring Mvc,Session,Spring Security,Websocket,服务器端: 我有一个webSocket配置,如下所示: @Configuration @EnableWebSocketMessageBroker @ComponentScan(basePackages = "gestionprojet.java") public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { @Override public void configureMessageBr

服务器端: 我有一个webSocket配置,如下所示:

@Configuration
@EnableWebSocketMessageBroker
@ComponentScan(basePackages = "gestionprojet.java")
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

  @Override
  public void configureMessageBroker(MessageBrokerRegistry config) {
    config.enableSimpleBroker("/notification");
    config.setApplicationDestinationPrefixes("/app");
  }

  @Override
  public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/stomp").withSockJS();
  }

  protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
      messages
              .simpDestMatchers("/user/*").authenticated() ;
  }
}
我还准备了一个spring安全配置,并使用自定义UserDetails接口与数据库集成进行用户管理:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

@Autowired
@Qualifier("customUserDetailsService")
private UserDetailsService userDetailsService;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  auth.userDetailsService(userDetailsService);

}

@Override
protected void configure(HttpSecurity http) throws Exception {

  http.csrf().disable()
      .authorizeRequests()
        .antMatchers("/login", "/logout").permitAll()
        .anyRequest().authenticated()
        .and().formLogin()
        .and()
        .sessionManagement().maximumSessions(1).sessionRegistry(sessionRegistry());

}

@Bean
public SessionRegistry sessionRegistry() {

    return new SessionRegistryImpl();
}
}

正如您所看到的,我已经实现了一个“SessionRegistry”,这样我就可以获得登录的用户

在客户端:

    var startListener = function() {
    socket.stomp.subscribe(service.NOTIFICATION_TOPIC, function(data) {
        listener.notify(getMessage(data.body));
    });
};

var initialize = function() {
    socket.client = new SockJS(service.SOCKET_URL);
    socket.stomp = Stomp.over(socket.client);
    socket.stomp.connect({}, startListener);
    socket.stomp.onclose = reconnect;
};

initialize();
return service;
我的问题是:我想将WebSocket连接链接到他们各自的登录用户(会话),这样我就可以通过WebSocket向特定用户发送删除消息,其中包括:

webSocketMessageTemplate.convertAndSend("/notification/message", new WebSocketMessage(n));
欢迎提出任何建议。多谢各位