Record Class UserContext
java.lang.Object
java.lang.Record
cloud.opencode.base.web.context.UserContext
- Record Components:
userId- the user ID | 用户IDusername- the username | 用户名roles- the user roles | 用户角色permissions- the user permissions | 用户权限attributes- additional attributes | 附加属性
public record UserContext(String userId, String username, Set<String> roles, Set<String> permissions, Map<String,Object> attributes)
extends Record
User Context
用户上下文
Holds current user information for request processing.
保存当前用户信息用于请求处理。
Features | 主要功能:
- Immutable user information record - 不可变用户信息记录
- Role and permission checking - 角色和权限检查
- Extensible attributes map - 可扩展的属性映射
- Built-in anonymous and system user support - 内置匿名和系统用户支持
Usage Examples | 使用示例:
// Create user context
UserContext user = UserContext.of("123", "john");
// Check roles
boolean isAdmin = user.hasRole("ADMIN");
boolean hasAccess = user.hasAnyPermission("read", "write");
// System and anonymous users
UserContext system = UserContext.system();
UserContext anon = UserContext.anonymous();
Security | 安全性:
- Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
- Null-safe: Partial (userId and username can be null for anonymous) - 空值安全: 部分(匿名用户的 userId 和 username 可为 null)
- Since:
- JDK 25, opencode-base-web V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic UserContextGet anonymous user context 获取匿名用户上下文Returns the value of theattributesrecord component.final booleanIndicates whether some other object is "equal to" this one.<T> TgetAttribute(String key) Get attribute 获取属性<T> TgetAttribute(String key, T defaultValue) Get attribute with default value 获取属性(带默认值)booleanhasAnyPermission(String... checkPermissions) Check if user has any of the permissions 检查用户是否拥有任一权限booleanhasAnyRole(String... checkRoles) Check if user has any of the roles 检查用户是否拥有任一角色final inthashCode()Returns a hash code value for this object.booleanhasPermission(String permission) Check if user has permission 检查用户是否拥有权限booleanCheck if user has role 检查用户是否拥有角色booleanCheck if anonymous user 检查是否匿名用户booleanCheck if authenticated 检查是否已认证booleanCheck if user is super admin 检查是否超级管理员static UserContextCreate user context 创建用户上下文static UserContextCreate user context with roles 创建带角色的用户上下文Returns the value of thepermissionsrecord component.roles()Returns the value of therolesrecord component.static UserContextsystem()Get system user context 获取系统用户上下文final StringtoString()Returns a string representation of this record class.userId()Returns the value of theuserIdrecord component.username()Returns the value of theusernamerecord component.
-
Field Details
-
ANONYMOUS
Anonymous user constant 匿名用户常量
-
-
Constructor Details
-
UserContext
-
-
Method Details
-
of
Create user context 创建用户上下文- Parameters:
userId- the user ID | 用户IDusername- the username | 用户名- Returns:
- the user context | 用户上下文
-
of
Create user context with roles 创建带角色的用户上下文- Parameters:
userId- the user ID | 用户IDusername- the username | 用户名roles- the roles | 角色- Returns:
- the user context | 用户上下文
-
hasRole
Check if user has role 检查用户是否拥有角色- Parameters:
role- the role to check | 要检查的角色- Returns:
- true if has role | 如果拥有角色返回true
-
hasAnyRole
Check if user has any of the roles 检查用户是否拥有任一角色- Parameters:
checkRoles- the roles to check | 要检查的角色- Returns:
- true if has any role | 如果拥有任一角色返回true
-
hasPermission
Check if user has permission 检查用户是否拥有权限- Parameters:
permission- the permission to check | 要检查的权限- Returns:
- true if has permission | 如果拥有权限返回true
-
hasAnyPermission
Check if user has any of the permissions 检查用户是否拥有任一权限- Parameters:
checkPermissions- the permissions to check | 要检查的权限- Returns:
- true if has any permission | 如果拥有任一权限返回true
-
getAttribute
Get attribute 获取属性- Type Parameters:
T- the attribute type | 属性类型- Parameters:
key- the attribute key | 属性键- Returns:
- the attribute value or null | 属性值或null
-
getAttribute
Get attribute with default value 获取属性(带默认值)- Type Parameters:
T- the attribute type | 属性类型- Parameters:
key- the attribute key | 属性键defaultValue- the default value | 默认值- Returns:
- the attribute value or default | 属性值或默认值
-
isAnonymous
public boolean isAnonymous()Check if anonymous user 检查是否匿名用户- Returns:
- true if anonymous | 如果是匿名用户返回true
-
isAuthenticated
public boolean isAuthenticated()Check if authenticated 检查是否已认证- Returns:
- true if authenticated | 如果已认证返回true
-
anonymous
Get anonymous user context 获取匿名用户上下文- Returns:
- the anonymous user context | 匿名用户上下文
-
system
Get system user context 获取系统用户上下文- Returns:
- the system user context | 系统用户上下文
-
isSuperAdmin
public boolean isSuperAdmin()Check if user is super admin 检查是否超级管理员- Returns:
- true if super admin | 如果是超级管理员返回true
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared withObjects::equals(Object,Object). -
userId
-
username
-
roles
-
permissions
Returns the value of thepermissionsrecord component.- Returns:
- the value of the
permissionsrecord component
-
attributes
Returns the value of theattributesrecord component.- Returns:
- the value of the
attributesrecord component
-