本文整理汇总了Java中org.springframework.expression.spel.support.StandardEvaluationContext.setVariables方法的典型用法代码示例。如果您正苦于以下问题:Java StandardEvaluationContext.setVariables方法的具体用法?Java StandardEvaluationContext.setVariables怎么用?Java StandardEvaluationContext.setVariables使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.expression.spel.support.StandardEvaluationContext
的用法示例。
在下文中一共展示了StandardEvaluationContext.setVariables方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: evaluateTemplate
import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
@Override
public String evaluateTemplate(String expressionTemplate, Map<String, Object> evaluationContext) {
Optional.ofNullable(expressionTemplate)
.filter(template -> !template.isEmpty())
.orElseThrow(() -> new IllegalArgumentException("Expression template cannot be null or empty"));
Optional.ofNullable(evaluationContext)
.orElseThrow(() -> new IllegalArgumentException("Evaluation context cannot be null"));
Expression expression = new SpelExpressionParser().parseExpression(expressionTemplate
,new TemplateParserContext("@{","}"));
StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext();
try {
standardEvaluationContext.registerFunction("urlEncode",
Functions.class.getDeclaredMethod("urlEncode", new Class[] {String.class}));
} catch (NoSuchMethodException e) {
throw new EvaluationException("Fail to register function to evaluation context", e);
}
standardEvaluationContext.addPropertyAccessor(new MapAccessor());
standardEvaluationContext.setVariables(evaluationContext);
return expression.getValue(standardEvaluationContext,String.class);
}
示例2: evaluateConditional
import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
@Override
public boolean evaluateConditional(String conditionalExpression, Map<String, Object> evaluationContext) {
Optional.ofNullable(conditionalExpression)
.filter(template -> !template.isEmpty())
.orElseThrow(() -> new IllegalArgumentException("Conditional expression cannot be null or empty"));
Optional.ofNullable(evaluationContext)
.orElseThrow(() -> new IllegalArgumentException("Evaluation context cannot be null"));
Expression expression = new SpelExpressionParser().parseExpression(conditionalExpression);
StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext();
standardEvaluationContext.addPropertyAccessor(new MapAccessor());
standardEvaluationContext.setVariables(evaluationContext);
return expression.getValue(standardEvaluationContext,Boolean.class);
}
示例3: isValid
import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
public boolean isValid(Object[] values, ConstraintValidatorContext context) {
StandardEvaluationContext spelContext = new StandardEvaluationContext(values);
Map<String, Object> spelVars = IntStream.range(0, values.length).boxed()
.collect(Collectors.toMap(i -> "arg" + i, i -> values[i]));
spelContext.setVariables(spelVars);
return (Boolean) parsedExpression.getValue(spelContext);
}
示例4: checkPermission
import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@SneakyThrows
private boolean checkPermission(Authentication authentication,
Object resource,
Object privilegeKey,
boolean checkCondition,
boolean logPermission) {
String roleKey = getRoleKey(authentication);
Map<String, Object> resources = new HashMap<>();
if (resource != null) {
resources.putAll((Map<String, Object>) resource);
}
resources.put("subject", getSubject(roleKey));
Map<String, String> env = new HashMap<>();
env.put(EnvironmentVariable.IP.getName(), xmAuthenticationContextHolder
.getContext().getRemoteAddress().orElse(null));
resources.put("env", env);//put some env variables in this map
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariables(resources);
Permission permission = getPermission(roleKey, privilegeKey);
if (!isPermissionValid(permission)) {
log(logPermission, Level.ERROR,
"access denied: privilege={}, role={}, userKey={} due to privilege is not permitted",
privilegeKey, roleKey, getUserKey());
return false;
}
boolean validCondition = true;
if (!isConditionValid(permission.getEnvCondition(), context)) {
log(logPermission, Level.ERROR,
"access denied: privilege={}, role={}, userKey={} due to env condition: [{}] with context [{}]",
privilegeKey, roleKey, getUserKey(), permission.getEnvCondition().getExpressionString(), resources);
validCondition = false;
}
if (checkCondition && !isConditionValid(permission.getResourceCondition(), context)) {
log(logPermission, Level.ERROR,
"access denied: privilege={}, role={}, userKey={} due to env condition: [{}] with context [{}] "
+ "with context [{}]",
privilegeKey, roleKey, getUserKey(), permission.getResourceCondition().getExpressionString(),
resources);
validCondition = false;
}
if (!validCondition && ReactionStrategy.SKIP.equals(permission.getReactionStrategy())) {
throw new SkipPermissionException("Skip permission", permission.getRoleKey() + ":"
+ permission.getPrivilegeKey());
} else if (!validCondition) {
return false;
}
log(logPermission, Level.INFO,
"access granted: privilege={}, role={}, userKey={}",
privilegeKey, roleKey, getUserKey());
return true;
}