Skip to content

Commit ff95a34

Browse files
author
Rob Winch
committed
SEC-2705: DefaultMessageSecurityExpressionHandler populates AuthenticationTrustResolver
1 parent 3b8f7fd commit ff95a34

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

messaging/src/main/java/org/springframework/security/messaging/access/expression/DefaultMessageSecurityExpressionHandler.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import org.springframework.security.access.expression.AbstractSecurityExpressionHandler;
2020
import org.springframework.security.access.expression.SecurityExpressionHandler;
2121
import org.springframework.security.access.expression.SecurityExpressionOperations;
22+
import org.springframework.security.authentication.AuthenticationTrustResolver;
23+
import org.springframework.security.authentication.AuthenticationTrustResolverImpl;
2224
import org.springframework.security.core.Authentication;
25+
import org.springframework.util.Assert;
2326

2427
/**
2528
* The default implementation of {@link SecurityExpressionHandler} which uses a {@link MessageSecurityExpressionRoot}.
@@ -31,8 +34,17 @@
3134
*/
3235
public class DefaultMessageSecurityExpressionHandler<T> extends AbstractSecurityExpressionHandler<Message<T>> {
3336

37+
private AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl();
38+
3439
@Override
3540
protected SecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication, Message<T> invocation) {
36-
return new MessageSecurityExpressionRoot(authentication,invocation);
41+
MessageSecurityExpressionRoot root = new MessageSecurityExpressionRoot(authentication,invocation);
42+
root.setTrustResolver(trustResolver);
43+
return root;
44+
}
45+
46+
public void setTrustResolver(AuthenticationTrustResolver trustResolver) {
47+
Assert.notNull(trustResolver,"trustResolver cannot be null");
48+
this.trustResolver = trustResolver;
3749
}
3850
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2002-2013 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+
* http://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.messaging.access.expression;
17+
18+
import static org.fest.assertions.Assertions.*;
19+
import static org.mockito.Mockito.*;
20+
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.mockito.Mock;
25+
import org.mockito.runners.MockitoJUnitRunner;
26+
import org.springframework.expression.EvaluationContext;
27+
import org.springframework.expression.Expression;
28+
import org.springframework.messaging.Message;
29+
import org.springframework.messaging.support.GenericMessage;
30+
import org.springframework.security.access.expression.ExpressionUtils;
31+
import org.springframework.security.authentication.AnonymousAuthenticationToken;
32+
import org.springframework.security.authentication.AuthenticationTrustResolver;
33+
import org.springframework.security.core.Authentication;
34+
import org.springframework.security.core.authority.AuthorityUtils;
35+
36+
@RunWith(MockitoJUnitRunner.class)
37+
public class DefaultMessageSecurityExpressionHandlerTests {
38+
@Mock
39+
AuthenticationTrustResolver trustResolver;
40+
41+
DefaultMessageSecurityExpressionHandler<Object> handler;
42+
43+
Message<Object> message;
44+
45+
Authentication authentication;
46+
47+
@Before
48+
public void setup() {
49+
handler = new DefaultMessageSecurityExpressionHandler<Object>();
50+
51+
message = new GenericMessage<Object>("");
52+
authentication = new AnonymousAuthenticationToken("key", "anonymous", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
53+
}
54+
55+
// SEC-2705
56+
@Test
57+
public void trustResolverPopulated() {
58+
EvaluationContext context = handler.createEvaluationContext(authentication, message);
59+
Expression expression = handler.getExpressionParser().parseExpression("authenticated");
60+
61+
assertThat(ExpressionUtils.evaluateAsBoolean(expression, context)).isFalse();
62+
}
63+
64+
@Test(expected = IllegalArgumentException.class)
65+
public void trustResolverNull() {
66+
handler.setTrustResolver(null);
67+
}
68+
69+
@Test
70+
public void trustResolverCustom() {
71+
handler.setTrustResolver(trustResolver);
72+
EvaluationContext context = handler.createEvaluationContext(authentication, message);
73+
Expression expression = handler.getExpressionParser().parseExpression("authenticated");
74+
when(trustResolver.isAnonymous(authentication)).thenReturn(false);
75+
76+
assertThat(ExpressionUtils.evaluateAsBoolean(expression, context)).isTrue();
77+
}
78+
}

0 commit comments

Comments
 (0)