Record Class SecurityPolicy
java.lang.Object
java.lang.Record
cloud.opencode.base.expression.sandbox.SecurityPolicy
- Record Components:
allowedClasses- the set of allowed class names | 允许的类名集合deniedClasses- the set of denied class names | 拒绝的类名集合allowedMethods- the set of allowed method names | 允许的方法名集合deniedMethods- the set of denied method names | 拒绝的方法名集合allowedFunctions- the set of allowed function names | 允许的函数名集合deniedFunctions- the set of denied function names | 拒绝的函数名集合timeoutMillis- the maximum execution time in milliseconds | 最大执行时间(毫秒)maxIterations- the maximum number of iterations | 最大迭代次数maxExpressionLength- the maximum expression length | 最大表达式长度
public record SecurityPolicy(Set<Class<?>> allowedClasses, Set<String> deniedClasses, Set<String> allowedMethods, Set<String> deniedMethods, Set<String> allowedFunctions, Set<String> deniedFunctions, long timeoutMillis, int maxIterations, int maxExpressionLength)
extends Record
Security Policy
安全策略
Defines security constraints for expression evaluation including allowed classes, methods, timeout limits, and iteration limits.
定义表达式求值的安全约束,包括允许的类、方法、超时限制和迭代限制。
Features | 主要功能:
- Class, method, and function allow/deny lists - 类、方法和函数的允许/拒绝列表
- Timeout, iteration, and expression length limits - 超时、迭代和表达式长度限制
- Preset policies: strict and lenient - 预设策略: 严格和宽松
- Builder pattern for custom policies - 构建器模式用于自定义策略
Usage Examples | 使用示例:
// Use strict policy
SecurityPolicy policy = SecurityPolicy.strict();
boolean allowed = policy.isClassAllowed(String.class); // true
boolean denied = policy.isMethodAllowed("getClass"); // false
// Custom policy
SecurityPolicy custom = SecurityPolicy.builder()
.allowClass(String.class, Integer.class)
.denyMethod("getClass", "forName")
.timeout(10000)
.build();
Security | 安全性:
- Thread-safe: Yes, immutable record - 线程安全: 是,不可变记录
- Null-safe: Yes, null class returns false - 空值安全: 是,null类返回false
- Deny takes priority over allow - 拒绝优先于允许
- Since:
- JDK 25, opencode-base-expression V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for SecurityPolicy SecurityPolicy 构建器 -
Constructor Summary
ConstructorsConstructorDescriptionSecurityPolicy(Set<Class<?>> allowedClasses, Set<String> deniedClasses, Set<String> allowedMethods, Set<String> deniedMethods, Set<String> allowedFunctions, Set<String> deniedFunctions, long timeoutMillis, int maxIterations, int maxExpressionLength) Creates an instance of aSecurityPolicyrecord class. -
Method Summary
Modifier and TypeMethodDescriptionReturns the value of theallowedClassesrecord component.Returns the value of theallowedFunctionsrecord component.Returns the value of theallowedMethodsrecord component.static SecurityPolicy.Builderbuilder()Create a custom security policy using builder 使用构建器创建自定义安全策略Returns the value of thedeniedClassesrecord component.Returns the value of thedeniedFunctionsrecord component.Returns the value of thedeniedMethodsrecord component.final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.booleanisClassAllowed(Class<?> clazz) Check if a class is allowed 检查类是否被允许booleanisFunctionAllowed(String functionName) Check if a function is allowed 检查函数是否被允许booleanisMethodAllowed(String methodName) Check if a method is allowed 检查方法是否被允许static SecurityPolicylenient()Create a lenient security policy 创建宽松安全策略intReturns the value of themaxExpressionLengthrecord component.intReturns the value of themaxIterationsrecord component.static SecurityPolicystrict()Create a strict security policy 创建严格安全策略longReturns the value of thetimeoutMillisrecord component.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
SecurityPolicy
public SecurityPolicy(Set<Class<?>> allowedClasses, Set<String> deniedClasses, Set<String> allowedMethods, Set<String> deniedMethods, Set<String> allowedFunctions, Set<String> deniedFunctions, long timeoutMillis, int maxIterations, int maxExpressionLength) Creates an instance of aSecurityPolicyrecord class.- Parameters:
allowedClasses- the value for theallowedClassesrecord componentdeniedClasses- the value for thedeniedClassesrecord componentallowedMethods- the value for theallowedMethodsrecord componentdeniedMethods- the value for thedeniedMethodsrecord componentallowedFunctions- the value for theallowedFunctionsrecord componentdeniedFunctions- the value for thedeniedFunctionsrecord componenttimeoutMillis- the value for thetimeoutMillisrecord componentmaxIterations- the value for themaxIterationsrecord componentmaxExpressionLength- the value for themaxExpressionLengthrecord component
-
-
Method Details
-
strict
Create a strict security policy 创建严格安全策略Only allows basic types and common operations.
仅允许基本类型和常见操作。
- Returns:
- the strict policy | 严格策略
-
lenient
Create a lenient security policy 创建宽松安全策略Allows most operations with minimal restrictions.
允许大多数操作,限制最小。
- Returns:
- the lenient policy | 宽松策略
-
builder
Create a custom security policy using builder 使用构建器创建自定义安全策略- Returns:
- the builder | 构建器
-
isClassAllowed
Check if a class is allowed 检查类是否被允许- Parameters:
clazz- the class | 类- Returns:
- true if allowed | 如果允许返回 true
-
isMethodAllowed
Check if a method is allowed 检查方法是否被允许- Parameters:
methodName- the method name | 方法名- Returns:
- true if allowed | 如果允许返回 true
-
isFunctionAllowed
Check if a function is allowed 检查函数是否被允许- Parameters:
functionName- the function name | 函数名- Returns:
- true if allowed | 如果允许返回 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. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with thecomparemethod from their corresponding wrapper classes. -
allowedClasses
Returns the value of theallowedClassesrecord component.- Returns:
- the value of the
allowedClassesrecord component
-
deniedClasses
Returns the value of thedeniedClassesrecord component.- Returns:
- the value of the
deniedClassesrecord component
-
allowedMethods
Returns the value of theallowedMethodsrecord component.- Returns:
- the value of the
allowedMethodsrecord component
-
deniedMethods
Returns the value of thedeniedMethodsrecord component.- Returns:
- the value of the
deniedMethodsrecord component
-
allowedFunctions
Returns the value of theallowedFunctionsrecord component.- Returns:
- the value of the
allowedFunctionsrecord component
-
deniedFunctions
Returns the value of thedeniedFunctionsrecord component.- Returns:
- the value of the
deniedFunctionsrecord component
-
timeoutMillis
public long timeoutMillis()Returns the value of thetimeoutMillisrecord component.- Returns:
- the value of the
timeoutMillisrecord component
-
maxIterations
public int maxIterations()Returns the value of themaxIterationsrecord component.- Returns:
- the value of the
maxIterationsrecord component
-
maxExpressionLength
public int maxExpressionLength()Returns the value of themaxExpressionLengthrecord component.- Returns:
- the value of the
maxExpressionLengthrecord component
-