SEC-1723: Use standard SpEL syntax for accessing beans in the app context by name.

This commit is contained in:
Luke Taylor
2011-04-22 13:33:56 +01:00
parent dd108041a0
commit 614d8c0321
8 changed files with 65 additions and 50 deletions

View File

@@ -45,7 +45,7 @@ public class ExpressionProtectedBusinessServiceImpl implements BusinessService {
return someArray;
}
@PreAuthorize("#x == 'x' and number.intValue() == 1294 ")
@PreAuthorize("#x == 'x' and @number.intValue() == 1294 ")
public void methodWithBeanNamePropertyAccessExpression(String x) {
}

View File

@@ -0,0 +1,53 @@
package org.springframework.security.access.expression;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import org.junit.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.expression.Expression;
import org.springframework.security.core.Authentication;
import java.util.*;
/**
* @author Luke Taylor
*/
public class AbstractSecurityExpressionHandlerTests {
private AbstractSecurityExpressionHandler<Object> handler;
@Before
public void setUp() throws Exception {
handler = new AbstractSecurityExpressionHandler<Object>() {
@Override
protected SecurityExpressionRoot createSecurityExpressionRoot(Authentication authentication, Object o) {
return new SecurityExpressionRoot(authentication) {};
}
};
}
@Test
public void beanNamesAreCorrectlyResolved() throws Exception {
handler.setApplicationContext(new AnnotationConfigApplicationContext(TestConfiguration.class));
Expression expression = handler.getExpressionParser().parseExpression("@number10.compareTo(@number20) < 0");
assertTrue((Boolean) expression.getValue(handler.createEvaluationContext(mock(Authentication.class), new Object())));
}
}
@Configuration
class TestConfiguration {
@Bean
Integer number10() {
return 10;
}
@Bean
Integer number20() {
return 20;
}
}

View File

@@ -20,7 +20,7 @@ import org.springframework.security.core.authority.AuthorityUtils;
* @since 3.0
*/
public class SecurityExpressionRootTests {
private final Authentication JOE = new TestingAuthenticationToken("joe", "pass", "A", "B");
final static Authentication JOE = new TestingAuthenticationToken("joe", "pass", "A", "B");
@Test
public void denyAllIsFalsePermitAllTrue() throws Exception {