View Javadoc
1   /*
2    * Copyright 2002-2011 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.springframework.security.oauth2.provider.expression;
17  
18  import org.springframework.expression.Expression;
19  import org.springframework.expression.ExpressionParser;
20  import org.springframework.expression.ParseException;
21  import org.springframework.expression.ParserContext;
22  import org.springframework.util.Assert;
23  
24  /**
25   * <p>
26   * A custom {@link ExpressionParser} that automatically wraps SpEL expression with
27   * {@link OAuth2SecurityExpressionMethods#throwOnError(boolean)}. This makes it simple for users to specify an
28   * expression and then have it verified (providing errors) after the result of the expression is known.
29   * </p>
30   * <p>
31   * Note: The implication is that all expressions that are parsed must return a boolean result. This expectation is
32   * already true since Spring Security expects the result to be a boolean.
33   * </p>
34   * 
35   * @author Rob Winch
36   * 
37   */
38  public class OAuth2ExpressionParser implements ExpressionParser {
39  
40  	private final ExpressionParser delegate;
41  
42  	public OAuth2ExpressionParser(ExpressionParser delegate) {
43  		Assert.notNull(delegate, "delegate cannot be null");
44  		this.delegate = delegate;
45  	}
46  
47  	public Expression parseExpression(String expressionString) throws ParseException {
48  		return delegate.parseExpression(wrapExpression(expressionString));
49  	}
50  
51  	public Expression parseExpression(String expressionString, ParserContext context) throws ParseException {
52  		return delegate.parseExpression(wrapExpression(expressionString), context);
53  	}
54  
55  	private String wrapExpression(String expressionString) {
56  		return "#oauth2.throwOnError(" + expressionString + ")";
57  	}
58  }