|
1 | 1 | package com.example.spelattack;
|
2 | 2 |
|
| 3 | +import org.springframework.context.expression.MethodBasedEvaluationContext; |
| 4 | +import org.springframework.expression.EvaluationContext; |
| 5 | +import org.springframework.expression.Expression; |
3 | 6 | import org.springframework.expression.spel.standard.SpelExpressionParser;
|
| 7 | +import org.springframework.expression.spel.support.SimpleEvaluationContext; |
| 8 | +import org.springframework.expression.spel.support.StandardEvaluationContext; |
4 | 9 |
|
5 | 10 | /**
|
6 | 11 | * @author Whoopsunix
|
@@ -29,11 +34,45 @@ public static void main(String[] args) {
|
29 | 34 | */
|
30 | 35 |
|
31 | 36 |
|
32 |
| - Object obj = spel(sleep); |
| 37 | + |
| 38 | + Object obj = spel(runtime); |
33 | 39 | System.out.println(obj);
|
34 | 40 | }
|
35 | 41 |
|
36 | 42 | public static Object spel(String payload) {
|
37 | 43 | return new SpelExpressionParser().parseExpression(payload).getValue();
|
38 | 44 | }
|
| 45 | + |
| 46 | + /** |
| 47 | + * 默认也是用的 StandardEvaluationContext |
| 48 | + */ |
| 49 | + public static Object spelStandardEvaluationContext(String payload) { |
| 50 | + EvaluationContext evaluationContext = new StandardEvaluationContext(); |
| 51 | + return new SpelExpressionParser().parseExpression(payload).getValue(evaluationContext); |
| 52 | + } |
| 53 | + |
| 54 | + public static Object spelMethodBasedEvaluationContext(String payload) { |
| 55 | + |
| 56 | + EvaluationContext evaluationContext = new MethodBasedEvaluationContext(new User(), null, null, null); |
| 57 | + return new SpelExpressionParser().parseExpression(payload).getValue(evaluationContext); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * safe |
| 62 | + */ |
| 63 | + |
| 64 | + /** |
| 65 | + * SimpleEvaluationContext |
| 66 | + */ |
| 67 | + public static Object spelSimpleEvaluationContext(String payload) { |
| 68 | + EvaluationContext evaluationContext = SimpleEvaluationContext.forReadOnlyDataBinding().build(); |
| 69 | + return new SpelExpressionParser().parseExpression(payload).getValue(evaluationContext); |
| 70 | + } |
| 71 | + |
| 72 | + public static Object spelSafe(String payload) { |
| 73 | + StandardEvaluationContext context = new StandardEvaluationContext(); |
| 74 | + context.setVariable("payload", payload); |
| 75 | + Expression expression = new SpelExpressionParser().parseExpression("#payload"); |
| 76 | + return expression.getValue(context); |
| 77 | + } |
39 | 78 | }
|
0 commit comments