HttpSession 을 WebSocket Session과 통합하는 시도중에 발생하였다.
로그인을 하고 HttpSession이 생긴 이후에 웹소켓에 연결하려고 시도하면 문제가 생겼다.
LocalSimpUser simpUser = this.users.get(name);
해당 에러가 발생하는 이유는 위 코드에서 name 이 Null 이었고
users 는 ConcurrentHashMap 이었기에 key가 Null이 될 수 없어서 발생했다.
public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicationListener
{...}
@Override
public void onApplicationEvent(ApplicationEvent event) {
{...}
else if (event instanceof SessionConnectedEvent) {
Principal user = subProtocolEvent.getUser();
if (user == null) {
return;
}
String name = user.getName();
if (user instanceof DestinationUserNameProvider destinationUserNameProvider) {
name = destinationUserNameProvider.getDestinationUserName();
}
synchronized (this.sessionLock) {
LocalSimpUser simpUser = this.users.get(name);
if (simpUser == null) {
simpUser = new LocalSimpUser(name, user);
this.users.put(name, simpUser);
}
LocalSimpSession session = new LocalSimpSession(sessionId, simpUser);
simpUser.addSession(session);
this.sessions.put(sessionId, session);
}
{...}
}
name = user.getName();
Principal user = subProtocolEvent.getUser();
Principal의 구현체 => UsernamePasswordAuthenticationToken
public abstract class AbstractAuthenticationToken implements Authentication, CredentialsContainer
{...}
public String getName() {
if (this.getPrincipal() instanceof UserDetails) {
return ((UserDetails)this.getPrincipal()).getUsername();
} else if (this.getPrincipal() instanceof AuthenticatedPrincipal) {
return ((AuthenticatedPrincipal)this.getPrincipal()).getName();
} else if (this.getPrincipal() instanceof Principal) {
return ((Principal)this.getPrincipal()).getName();
} else {
return this.getPrincipal() == null ? "" : this.getPrincipal().toString();
}
}
this.getPrincipal() = SessionUser 로 내가 만들었던 CustomClass로 UserDetails를 구현한다.
따라서 SessionUser의 getUserName()의 값이 Null이 되지 않도록 만들어야 한다.
이 문제를 해결하면서 배운점이 있다기 보다 나의 부족함을 알게 되는 계기가 되었다.
또 하나의 디테일은 SimpMessagingTemplate 의 convertAndSendToUser(String user, String destination, Object payload) 의 user 에는 여기서 UserDetails의 getUserName()이 된다.