DbSessionManager
DbSessionManager is a singleton that manages the in-memory map of active user sessions. Each session is represented by a string token that maps to a Spring Security Authentication object. The manager is the central point for resolving who a token belongs to, obtaining elevated system-level access, and inspecting active sessions.
DbSessionManager is a singleton ? access it via DbSessionManager.getInstance(). It does not need to be injected as a Spring bean.
Methods
getSystemToken
Description:
| Method | Return values | Description |
|---|---|---|
|
getSystemToken() |
String |
Returns the administrator token for the tenant of the currently logged-in user. Pass this token to any API method to execute the operation as the system super-user instead of the current user. |
|
There is a special super-user named system that OpenKM uses for background operations. The system token is typically used in cron tasks or automation actions that must bypass normal user permissions. |
||
Example:
package com.openkm;
import com.openkm.api.OKMDocument;
import com.openkm.module.db.stuff.DbSessionManager;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
String systemToken = DbSessionManager.getInstance().getSystemToken();
// Delete is executed as the system super-user
okmDocument.delete(systemToken, "d50133e3-dbfa-4d01-a109-28785cd48f40");
} catch (Exception e) {
e.printStackTrace();
}
}
}
getSystemToken (by tenant)
Description:
| Method | Return values | Description |
|---|---|---|
|
getSystemToken(long tenantId) |
String |
Returns the administrator token for the given tenant ID. Use this overload when the tenant cannot be resolved from the current security context (e.g. in background threads). |
Example:
package com.openkm;
import com.openkm.module.db.stuff.DbSessionManager;
public class Test {
public static void main(String[] args) {
try {
long tenantId = 1L;
String systemToken = DbSessionManager.getInstance().getSystemToken(tenantId);
System.out.println("System token for tenant " + tenantId + ": " + systemToken);
} catch (Exception e) {
e.printStackTrace();
}
}
}
getAuthentication
Description:
| Method | Return values | Description |
|---|---|---|
|
getAuthentication(String token) |
Authentication |
Returns the Spring Security |
Example:
package com.openkm;
import com.openkm.module.db.stuff.DbSessionManager;
import org.springframework.security.core.Authentication;
public class Test {
public static void main(String[] args) {
try {
String token = "some-token";
Authentication auth = DbSessionManager.getInstance().getAuthentication(token);
if (auth != null) {
System.out.println("User: " + auth.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
getUser
Description:
| Method | Return values | Description |
|---|---|---|
|
getUser(String token) |
String |
Returns the username associated with the given token, or null if the token does not exist. |
Example:
package com.openkm;
import com.openkm.module.db.stuff.DbSessionManager;
public class Test {
public static void main(String[] args) {
try {
String token = "some-token";
String user = DbSessionManager.getInstance().getUser(token);
System.out.println("Token belongs to user: " + user);
} catch (Exception e) {
e.printStackTrace();
}
}
}
getRoles
Description:
| Method | Return values | Description |
|---|---|---|
|
getRoles(String token) |
Set<String> |
Returns the set of role names granted to the user associated with the given token. Returns an empty set if the token does not exist. |
Example:
package com.openkm;
import com.openkm.module.db.stuff.DbSessionManager;
import java.util.Set;
public class Test {
public static void main(String[] args) {
try {
String token = "some-token";
Set<String> roles = DbSessionManager.getInstance().getRoles(token);
System.out.println("Roles: " + roles);
} catch (Exception e) {
e.printStackTrace();
}
}
}
getToken
Description:
| Method | Return values | Description |
|---|---|---|
|
getToken(Authentication auth) |
String |
Returns the token string associated with the given |
Example:
package com.openkm;
import com.openkm.module.db.stuff.DbSessionManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
public class Test {
public static void main(String[] args) {
try {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String token = DbSessionManager.getInstance().getToken(auth);
System.out.println("Current user token: " + token);
} catch (Exception e) {
e.printStackTrace();
}
}
}
getInfo
Description:
| Method | Return values | Description |
|---|---|---|
|
getInfo(String token) |
DbSessionInfo |
Returns the session information object for the given token. |
Example:
package com.openkm;
import com.openkm.bean.DbSessionInfo;
import com.openkm.module.db.stuff.DbSessionManager;
public class Test {
public static void main(String[] args) {
try {
String token = "some-token";
DbSessionInfo info = DbSessionManager.getInstance().getInfo(token);
if (info != null) {
System.out.println("Created: " + info.getCreation().getTime());
System.out.println("Last access: " + info.getLastAccess().getTime());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
getTokens
Description:
| Method | Return values | Description |
|---|---|---|
|
getTokens() |
List<String> |
Returns a list of all active session tokens currently registered in the session manager. |
Example:
package com.openkm;
import com.openkm.module.db.stuff.DbSessionManager;
public class Test {
public static void main(String[] args) {
try {
for (String token : DbSessionManager.getInstance().getTokens()) {
String user = DbSessionManager.getInstance().getUser(token);
System.out.println("Token: " + token + " -> User: " + user);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
add
Description:
| Method | Return values | Description |
|---|---|---|
|
add(String token, Authentication auth) |
void |
Registers a new session by associating the given token with an |
Example:
package com.openkm;
import com.openkm.module.db.stuff.DbSessionManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import java.util.UUID;
public class Test {
public static void main(String[] args) {
try {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String token = UUID.randomUUID().toString();
DbSessionManager.getInstance().add(token, auth);
System.out.println("Session registered with token: " + token);
} catch (Exception e) {
e.printStackTrace();
}
}
}
remove
Description:
| Method | Return values | Description |
|---|---|---|
|
remove(String token) |
void |
Removes the session associated with the given token from the session manager. |
Example:
package com.openkm;
import com.openkm.module.db.stuff.DbSessionManager;
public class Test {
public static void main(String[] args) {
try {
String token = "some-token";
DbSessionManager.getInstance().remove(token);
System.out.println("Session removed");
} catch (Exception e) {
e.printStackTrace();
}
}
}