@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2017 the original author or authors.
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -28,4 +28,5 @@ public class Application {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2017 the original author or authors.
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -24,12 +24,12 @@ import org.springframework.session.web.socket.config.annotation.AbstractSessionW
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
|
||||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
||||
|
||||
// @formatter:off
|
||||
// tag::class[]
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
@EnableWebSocketMessageBroker
|
||||
public class WebSocketConfig
|
||||
extends AbstractSessionWebSocketMessageBrokerConfigurer<Session> { // <1>
|
||||
public class WebSocketConfig extends AbstractSessionWebSocketMessageBrokerConfigurer<Session> { // <1>
|
||||
|
||||
@Override
|
||||
protected void configureStompEndpoints(StompEndpointRegistry registry) { // <2>
|
||||
@@ -41,5 +41,7 @@ public class WebSocketConfig
|
||||
registry.enableSimpleBroker("/queue/", "/topic/");
|
||||
registry.setApplicationDestinationPrefixes("/app");
|
||||
}
|
||||
|
||||
}
|
||||
// end::class[]
|
||||
// @formatter:on
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2017 the original author or authors.
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -35,16 +35,15 @@ import org.springframework.session.Session;
|
||||
public class WebSocketHandlersConfig<S extends Session> {
|
||||
|
||||
@Bean
|
||||
public WebSocketConnectHandler<S> webSocketConnectHandler(
|
||||
SimpMessageSendingOperations messagingTemplate,
|
||||
public WebSocketConnectHandler<S> webSocketConnectHandler(SimpMessageSendingOperations messagingTemplate,
|
||||
ActiveWebSocketUserRepository repository) {
|
||||
return new WebSocketConnectHandler<>(messagingTemplate, repository);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WebSocketDisconnectHandler<S> webSocketDisconnectHandler(
|
||||
SimpMessageSendingOperations messagingTemplate,
|
||||
public WebSocketDisconnectHandler<S> webSocketDisconnectHandler(SimpMessageSendingOperations messagingTemplate,
|
||||
ActiveWebSocketUserRepository repository) {
|
||||
return new WebSocketDisconnectHandler<>(messagingTemplate, repository);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -24,8 +24,7 @@ import org.springframework.security.config.annotation.web.socket.AbstractSecurit
|
||||
* @author Rob Winch
|
||||
*/
|
||||
@Configuration
|
||||
public class WebSocketSecurityConfig
|
||||
extends AbstractSecurityWebSocketMessageBrokerConfigurer {
|
||||
public class WebSocketSecurityConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
|
||||
|
||||
// @formatter:off
|
||||
@Override
|
||||
@@ -36,4 +35,5 @@ public class WebSocketSecurityConfig
|
||||
.anyMessage().authenticated();
|
||||
}
|
||||
// @formatter:on
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,6 +23,7 @@ import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class ActiveWebSocketUser {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -21,9 +21,9 @@ import java.util.List;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface ActiveWebSocketUserRepository
|
||||
extends CrudRepository<ActiveWebSocketUser, String> {
|
||||
public interface ActiveWebSocketUserRepository extends CrudRepository<ActiveWebSocketUser, String> {
|
||||
|
||||
@Query("select DISTINCT(u.username) from ActiveWebSocketUser u where u.username != ?#{principal?.username}")
|
||||
List<String> findAllActiveUsers();
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,6 +19,7 @@ package sample.data;
|
||||
import java.util.Calendar;
|
||||
|
||||
public class InstantMessage {
|
||||
|
||||
private String to;
|
||||
|
||||
private String from;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2017 the original author or authors.
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -111,4 +111,5 @@ public class User implements Serializable {
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = 2738859149330833739L;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -27,4 +27,5 @@ import org.springframework.data.repository.CrudRepository;
|
||||
public interface UserRepository extends CrudRepository<User, Long> {
|
||||
|
||||
User findByEmail(String email);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -27,4 +27,5 @@ public class CsrfController {
|
||||
public CsrfToken csrf(CsrfToken token) {
|
||||
return token;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2017 the original author or authors.
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -38,7 +38,9 @@ import org.springframework.stereotype.Controller;
|
||||
*/
|
||||
@Controller
|
||||
public class MessageController {
|
||||
|
||||
private SimpMessageSendingOperations messagingTemplate;
|
||||
|
||||
private ActiveWebSocketUserRepository activeUserRepository;
|
||||
|
||||
@Autowired
|
||||
@@ -59,4 +61,5 @@ public class MessageController {
|
||||
public List<String> subscribeMessages() throws Exception {
|
||||
return this.activeUserRepository.findAllActiveUsers();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2018 the original author or authors.
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -35,6 +35,7 @@ import org.springframework.stereotype.Service;
|
||||
*/
|
||||
@Service
|
||||
public class UserRepositoryUserDetailsService implements UserDetailsService {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
@@ -50,8 +51,7 @@ public class UserRepositoryUserDetailsService implements UserDetailsService {
|
||||
* (java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username)
|
||||
throws UsernameNotFoundException {
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
User user = this.userRepository.findByEmail(username);
|
||||
if (user == null) {
|
||||
throw new UsernameNotFoundException("Could not find user " + username);
|
||||
@@ -96,5 +96,7 @@ public class UserRepositoryUserDetailsService implements UserDetailsService {
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = 5639683223516504866L;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -29,9 +29,10 @@ import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
|
||||
import org.springframework.messaging.simp.SimpMessageSendingOperations;
|
||||
import org.springframework.web.socket.messaging.SessionConnectEvent;
|
||||
|
||||
public class WebSocketConnectHandler<S>
|
||||
implements ApplicationListener<SessionConnectEvent> {
|
||||
public class WebSocketConnectHandler<S> implements ApplicationListener<SessionConnectEvent> {
|
||||
|
||||
private ActiveWebSocketUserRepository repository;
|
||||
|
||||
private SimpMessageSendingOperations messagingTemplate;
|
||||
|
||||
public WebSocketConnectHandler(SimpMessageSendingOperations messagingTemplate,
|
||||
@@ -49,9 +50,8 @@ public class WebSocketConnectHandler<S>
|
||||
return;
|
||||
}
|
||||
String id = SimpMessageHeaderAccessor.getSessionId(headers);
|
||||
this.repository.save(
|
||||
new ActiveWebSocketUser(id, user.getName(), Calendar.getInstance()));
|
||||
this.messagingTemplate.convertAndSend("/topic/friends/signin",
|
||||
Arrays.asList(user.getName()));
|
||||
this.repository.save(new ActiveWebSocketUser(id, user.getName(), Calendar.getInstance()));
|
||||
this.messagingTemplate.convertAndSend("/topic/friends/signin", Arrays.asList(user.getName()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2018 the original author or authors.
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -24,9 +24,10 @@ import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.messaging.simp.SimpMessageSendingOperations;
|
||||
import org.springframework.web.socket.messaging.SessionDisconnectEvent;
|
||||
|
||||
public class WebSocketDisconnectHandler<S>
|
||||
implements ApplicationListener<SessionDisconnectEvent> {
|
||||
public class WebSocketDisconnectHandler<S> implements ApplicationListener<SessionDisconnectEvent> {
|
||||
|
||||
private ActiveWebSocketUserRepository repository;
|
||||
|
||||
private SimpMessageSendingOperations messagingTemplate;
|
||||
|
||||
public WebSocketDisconnectHandler(SimpMessageSendingOperations messagingTemplate,
|
||||
@@ -44,8 +45,8 @@ public class WebSocketDisconnectHandler<S>
|
||||
}
|
||||
this.repository.findById(id).ifPresent((user) -> {
|
||||
this.repository.deleteById(id);
|
||||
this.messagingTemplate.convertAndSend("/topic/friends/signout",
|
||||
Arrays.asList(user.getUsername()));
|
||||
this.messagingTemplate.convertAndSend("/topic/friends/signout", Arrays.asList(user.getUsername()));
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user