PrincipalUtils
Utility class with static helper methods for querying the current user's identity, roles, tenant, and authentication context from the Spring Security context. All methods are static.
Methods that access the current security context (e.g. getUser(), getRoles()) require a valid authenticated security context. In cron tasks or background threads use the token-based overloads (getUserByToken, getTenantIdByToken) or DbSessionManager to obtain authentication for a known token.
Current user
getUser
Description:
| Method | Return values | Description |
|---|---|---|
|
getUser() |
String |
Returns the username of the currently authenticated user from the Spring Security context. |
Example:
System.out.println(PrincipalUtils.getUser()); // "jsmith"
isUser
Description:
| Method | Return values | Description |
|---|---|---|
|
isUser(String userId) |
boolean |
Returns |
getUserByToken
Description:
| Method | Return values | Description |
|---|---|---|
|
getUserByToken(String token) |
String |
Returns the username associated with the given session token. Throws |
Tenant
getTenantId
Description:
| Method | Return values | Description |
|---|---|---|
|
getTenantId() |
long |
Returns the tenant ID of the currently authenticated user. Falls back to |
Example:
long tenantId = PrincipalUtils.getTenantId();
System.out.println("Tenant: " + tenantId);
getTenantIdByToken
Description:
| Method | Return values | Description |
|---|---|---|
|
getTenantIdByToken(String token) |
long |
Returns the tenant ID associated with the given session token. Throws |
Roles
getRoles
Description:
| Method | Return values | Description |
|---|---|---|
|
getRoles() |
Set<String> |
Returns the set of role names granted to the currently authenticated user. Returns an empty set if not authenticated. |
hasRole
Description:
| Method | Return values | Description |
|---|---|---|
|
hasRole(String role) |
boolean |
Returns |
Example:
if (PrincipalUtils.hasRole("ROLE_ADMIN")) {
System.out.println("User is admin");
}
Set<String> roles = PrincipalUtils.getRoles();
System.out.println("Roles: " + roles);
User type checks
isRegularUser
Description:
| Method | Return values | Description |
|---|---|---|
|
isRegularUser() |
boolean |
Returns |
isAdminUser
Description:
| Method | Return values | Description |
|---|---|---|
|
isAdminUser() |
boolean |
Returns |
isSystemUser
Description:
| Method | Return values | Description |
|---|---|---|
|
isSystemUser() |
boolean |
Returns |
isSuperUser
Description:
| Method | Return values | Description |
|---|---|---|
|
isSuperUser() |
boolean |
Returns |
hasFullAccess
Description:
| Method | Return values | Description |
|---|---|---|
|
hasFullAccess() |
boolean |
Returns |
Example:
package com.openkm;
import com.openkm.principal.PrincipalUtils;
public class Test {
public static void main(String[] args) {
try {
System.out.println("User: " + PrincipalUtils.getUser());
System.out.println("TenantId: " + PrincipalUtils.getTenantId());
System.out.println("IsAdmin: " + PrincipalUtils.isAdminUser());
System.out.println("HasFullAccess: " + PrincipalUtils.hasFullAccess());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Authentication access
getAuthentication
Description:
| Method | Return values | Description |
|---|---|---|
|
getAuthentication() |
Authentication |
Returns the Spring Security |
getAuthenticationByToken
Description:
| Method | Return values | Description |
|---|---|---|
|
getAuthenticationByToken(String token) |
Authentication |
Returns the Spring Security |
getRemoteAddress
Description:
| Method | Return values | Description |
|---|---|---|
|
getRemoteAddress() |
String |
Returns the remote IP address of the currently authenticated user, or |