Spring Expression Language(SPEL)限制对具有给定注释的Bean字段的访问

时间:2018-08-08 01:44:54

标签: java spring spring-el

我需要使用Spring表达式语言评估基于bean的动态用户生成的表达式,但是我希望限制它们可以通过注释使用的字段。例如,如果我具有下面的类,我希望能够评估表达式field1 + field2,但是如果我尝试评估field1 + field3,则将导致生成异常。

这可能吗?有没有其他方法可以限制表达式的范围?

public class Foo {

    @AllowedField
    private int field1;

    @AllowedField
    private int field2;

    private int field3;
}

1 个答案:

答案 0 :(得分:2)

基本上,这是您需要做的

扩展set.seed(123) n <- 30 m <- 9 mat <- matrix( sample(-1:1, n * m, replace = TRUE), nrow = n, dimnames = list(1:n, paste("Gene", LETTERS[1:m], sep = "_")) ) foo <- mat[, order(colSums(abs(mat)))] bar <- foo[order(rowSums(abs(foo))), ] head(bar) 以返回自己的 Gene_F Gene_D Gene_I Gene_G Gene_C Gene_A Gene_H Gene_B Gene_E 18 -1 0 0 0 0 -1 0 0 1 15 0 0 0 1 0 -1 -1 -1 0 27 0 0 0 0 1 0 -1 -1 -1 1 1 -1 0 1 0 -1 0 1 0 3 0 0 -1 1 0 0 -1 1 -1 6 0 -1 1 0 0 -1 1 0 1

StandardEvaluationContext

扩展PropertyAccessor并实现自己的public class SecureEvaluationContext extends StandardEvaluationContext { @Override public List<PropertyAccessor> getPropertyAccessors() { return Arrays.asList(new SecurePropertyAccessor()); } }

ReflectivePropertyAccessor

评估:

canRead
相关问题