diff --git a/messaging/src/main/java/org/springframework/security/messaging/access/expression/EvaluationContextPostProcessor.java b/messaging/src/main/java/org/springframework/security/messaging/access/expression/EvaluationContextPostProcessor.java new file mode 100644 index 00000000000..ed2ff262f72 --- /dev/null +++ b/messaging/src/main/java/org/springframework/security/messaging/access/expression/EvaluationContextPostProcessor.java @@ -0,0 +1,46 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.messaging.access.expression; + +import org.springframework.expression.EvaluationContext; + +/** + * +/** + * Allows post processing the {@link EvaluationContext} + * + *
+ * This API is intentionally kept package scope as it may evolve over time. + *
+ * + * @author Daniel Bustamante Ospina + * @since 5.1 + */ +interface EvaluationContextPostProcessor { + + /** + * Allows post processing of the {@link EvaluationContext}. Implementations + * may return a new instance of {@link EvaluationContext} or modify the + * {@link EvaluationContext} that was passed in. + * + * @param context + * the original {@link EvaluationContext} + * @param invocation + * the security invocation object (i.e. Message) + * @return the upated context. + */ + EvaluationContext postProcess(EvaluationContext context, I invocation); +} diff --git a/messaging/src/main/java/org/springframework/security/messaging/access/expression/ExpressionBasedMessageSecurityMetadataSourceFactory.java b/messaging/src/main/java/org/springframework/security/messaging/access/expression/ExpressionBasedMessageSecurityMetadataSourceFactory.java index 67c6f323156..eb952d4be15 100644 --- a/messaging/src/main/java/org/springframework/security/messaging/access/expression/ExpressionBasedMessageSecurityMetadataSourceFactory.java +++ b/messaging/src/main/java/org/springframework/security/messaging/access/expression/ExpressionBasedMessageSecurityMetadataSourceFactory.java @@ -48,6 +48,7 @@ public final class ExpressionBasedMessageSecurityMetadataSourceFactory { * LinkedHashMap<MessageMatcher<?>,String> matcherToExpression = new LinkedHashMap<MessageMatcher<Object>,String>(); * matcherToExpression.put(new SimDestinationMessageMatcher("/public/**"), "permitAll"); * matcherToExpression.put(new SimDestinationMessageMatcher("/admin/**"), "hasRole('ROLE_ADMIN')"); + * matcherToExpression.put(new SimDestinationMessageMatcher("/topics/{name}/**"), "@someBean.customLogic(authentication, #name)"); * matcherToExpression.put(new SimDestinationMessageMatcher("/**"), "authenticated"); * * MessageSecurityMetadataSource metadataSource = createExpressionMessageMetadataSource(matcherToExpression); @@ -82,6 +83,7 @@ public static MessageSecurityMetadataSource createExpressionMessageMetadataSourc * LinkedHashMap<MessageMatcher<?>,String> matcherToExpression = new LinkedHashMap<MessageMatcher<Object>,String>(); * matcherToExpression.put(new SimDestinationMessageMatcher("/public/**"), "permitAll"); * matcherToExpression.put(new SimDestinationMessageMatcher("/admin/**"), "hasRole('ROLE_ADMIN')"); + * matcherToExpression.put(new SimDestinationMessageMatcher("/topics/{name}/**"), "@someBean.customLogic(authentication, #name)"); * matcherToExpression.put(new SimDestinationMessageMatcher("/**"), "authenticated"); * * MessageSecurityMetadataSource metadataSource = createExpressionMessageMetadataSource(matcherToExpression); @@ -113,7 +115,7 @@ public static MessageSecurityMetadataSource createExpressionMessageMetadataSourc String rawExpression = entry.getValue(); Expression expression = handler.getExpressionParser().parseExpression( rawExpression); - ConfigAttribute attribute = new MessageExpressionConfigAttribute(expression); + ConfigAttribute attribute = new MessageExpressionConfigAttribute(expression, matcher); matcherToAttrs.put(matcher, Arrays.asList(attribute)); } return new DefaultMessageSecurityMetadataSource(matcherToAttrs); diff --git a/messaging/src/main/java/org/springframework/security/messaging/access/expression/MessageExpressionConfigAttribute.java b/messaging/src/main/java/org/springframework/security/messaging/access/expression/MessageExpressionConfigAttribute.java index 28e86d120b7..71376d1b219 100644 --- a/messaging/src/main/java/org/springframework/security/messaging/access/expression/MessageExpressionConfigAttribute.java +++ b/messaging/src/main/java/org/springframework/security/messaging/access/expression/MessageExpressionConfigAttribute.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,32 +15,43 @@ */ package org.springframework.security.messaging.access.expression; +import org.springframework.expression.EvaluationContext; import org.springframework.expression.Expression; import org.springframework.messaging.Message; import org.springframework.security.access.ConfigAttribute; +import org.springframework.security.messaging.util.matcher.MessageMatcher; +import org.springframework.security.messaging.util.matcher.SimpDestinationMessageMatcher; import org.springframework.util.Assert; +import java.util.Map; + /** * Simple expression configuration attribute for use in {@link Message} authorizations. * * @since 4.0 * @author Rob Winch + * @author Daniel Bustamante Ospina */ @SuppressWarnings("serial") -class MessageExpressionConfigAttribute implements ConfigAttribute { +class MessageExpressionConfigAttribute implements ConfigAttribute, EvaluationContextPostProcessor
* MessageMatcher which compares a pre-defined pattern against the destination of a
@@ -129,6 +132,14 @@ public boolean matches(Message extends Object> message) {
return destination != null && matcher.match(pattern, destination);
}
+
+ public Map