SimpUser exposes Principal

Closes gh-25191
This commit is contained in:
Rossen Stoyanchev
2020-10-19 21:39:48 +01:00
parent 76eb5e6e2c
commit bfb2ce6e2a
4 changed files with 40 additions and 5 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.messaging.simp.user;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.Principal;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@@ -310,6 +311,12 @@ public class MultiServerUserRegistry implements SimpUserRegistry, SmartApplicati
return this.name;
}
@Nullable
@Override
public Principal getPrincipal() {
return null;
}
@Override
public boolean hasSessions() {
if (this.sessionLookup != null) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2020 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.
@@ -16,6 +16,7 @@
package org.springframework.messaging.simp.user;
import java.security.Principal;
import java.util.Set;
import org.springframework.lang.Nullable;
@@ -33,6 +34,15 @@ public interface SimpUser {
*/
String getName();
/**
* Return the user associated with the session, if available. Typically, the
* user information is available unless the user is connected to a different
* server in a multi-server user registry scenario.
* @since 5.3
*/
@Nullable
Principal getPrincipal();
/**
* Whether the user has any sessions.
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2020 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.
@@ -16,11 +16,14 @@
package org.springframework.messaging.simp.user;
import java.security.Principal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.springframework.lang.Nullable;
/**
* @author Rossen Stoyanchev
*/
@@ -41,6 +44,12 @@ public class TestSimpUser implements SimpUser {
return name;
}
@Nullable
@Override
public Principal getPrincipal() {
return null;
}
@Override
public Set<SimpSession> getSessions() {
return new HashSet<>(this.sessions.values());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@@ -114,7 +114,7 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati
synchronized (this.sessionLock) {
LocalSimpUser simpUser = this.users.get(name);
if (simpUser == null) {
simpUser = new LocalSimpUser(name);
simpUser = new LocalSimpUser(name, user);
this.users.put(name, simpUser);
}
LocalSimpSession session = new LocalSimpSession(sessionId, simpUser);
@@ -193,11 +193,14 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati
private final String name;
private final Principal user;
private final Map<String, SimpSession> userSessions = new ConcurrentHashMap<>(1);
public LocalSimpUser(String userName) {
public LocalSimpUser(String userName, Principal user) {
Assert.notNull(userName, "User name must not be null");
this.name = userName;
this.user = user;
}
@Override
@@ -205,6 +208,12 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati
return this.name;
}
@Nullable
@Override
public Principal getPrincipal() {
return this.user;
}
@Override
public boolean hasSessions() {
return !this.userSessions.isEmpty();