Search in sources :

Example 1 with MessageMatcher

use of org.springframework.security.messaging.util.matcher.MessageMatcher in project spring-security by spring-projects.

the class ExpressionBasedMessageSecurityMetadataSourceFactory method createExpressionMessageMetadataSource.

/**
	 * Create a {@link MessageSecurityMetadataSource} that uses {@link MessageMatcher}
	 * mapped to Spring Expressions. Each entry is considered in order and only the first
	 * match is used.
	 *
	 * For example:
	 *
	 * <pre>
	 *     LinkedHashMap&lt;MessageMatcher&lt;?&gt;,String&gt; matcherToExpression = new LinkedHashMap&lt;MessageMatcher&lt;Object&gt;,String&gt;();
	 *     matcherToExpression.put(new SimDestinationMessageMatcher("/public/**"), "permitAll");
	 *     matcherToExpression.put(new SimDestinationMessageMatcher("/admin/**"), "hasRole('ROLE_ADMIN')");
	 *     matcherToExpression.put(new SimDestinationMessageMatcher("/**"), "authenticated");
	 *
	 *     MessageSecurityMetadataSource metadataSource = createExpressionMessageMetadataSource(matcherToExpression);
	 * </pre>
	 *
	 * <p>
	 * If our destination is "/public/hello", it would match on "/public/**" and on "/**".
	 * However, only "/public/**" would be used since it is the first entry. That means
	 * that a destination of "/public/hello" will be mapped to "permitAll".
	 * </p>
	 *
	 * <p>
	 * For a complete listing of expressions see {@link MessageSecurityExpressionRoot}
	 * </p>
	 *
	 * @param matcherToExpression an ordered mapping of {@link MessageMatcher} to Strings
	 * that are turned into an Expression using
	 * {@link DefaultMessageSecurityExpressionHandler#getExpressionParser()}
	 * @param handler the {@link SecurityExpressionHandler} to use
	 * @return the {@link MessageSecurityMetadataSource} to use. Cannot be null.
	 */
public static MessageSecurityMetadataSource createExpressionMessageMetadataSource(LinkedHashMap<MessageMatcher<?>, String> matcherToExpression, SecurityExpressionHandler<Message<Object>> handler) {
    LinkedHashMap<MessageMatcher<?>, Collection<ConfigAttribute>> matcherToAttrs = new LinkedHashMap<MessageMatcher<?>, Collection<ConfigAttribute>>();
    for (Map.Entry<MessageMatcher<?>, String> entry : matcherToExpression.entrySet()) {
        MessageMatcher<?> matcher = entry.getKey();
        String rawExpression = entry.getValue();
        Expression expression = handler.getExpressionParser().parseExpression(rawExpression);
        ConfigAttribute attribute = new MessageExpressionConfigAttribute(expression);
        matcherToAttrs.put(matcher, Arrays.asList(attribute));
    }
    return new DefaultMessageSecurityMetadataSource(matcherToAttrs);
}
Also used : MessageMatcher(org.springframework.security.messaging.util.matcher.MessageMatcher) ConfigAttribute(org.springframework.security.access.ConfigAttribute) Expression(org.springframework.expression.Expression) Collection(java.util.Collection) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) DefaultMessageSecurityMetadataSource(org.springframework.security.messaging.access.intercept.DefaultMessageSecurityMetadataSource) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with MessageMatcher

use of org.springframework.security.messaging.util.matcher.MessageMatcher in project spring-security by spring-projects.

the class MessageSecurityMetadataSourceRegistry method simpTypeMatchers.

/**
	 * Maps a {@link List} of {@link SimpDestinationMessageMatcher} instances.
	 *
	 * @param typesToMatch the {@link SimpMessageType} instance to match on
	 * @return the {@link Constraint} associated to the matchers.
	 */
public Constraint simpTypeMatchers(SimpMessageType... typesToMatch) {
    MessageMatcher<?>[] typeMatchers = new MessageMatcher<?>[typesToMatch.length];
    for (int i = 0; i < typesToMatch.length; i++) {
        SimpMessageType typeToMatch = typesToMatch[i];
        typeMatchers[i] = new SimpMessageTypeMatcher(typeToMatch);
    }
    return matchers(typeMatchers);
}
Also used : MessageMatcher(org.springframework.security.messaging.util.matcher.MessageMatcher) SimpDestinationMessageMatcher(org.springframework.security.messaging.util.matcher.SimpDestinationMessageMatcher) SimpMessageType(org.springframework.messaging.simp.SimpMessageType) SimpMessageTypeMatcher(org.springframework.security.messaging.util.matcher.SimpMessageTypeMatcher)

Aggregations

MessageMatcher (org.springframework.security.messaging.util.matcher.MessageMatcher)2 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Expression (org.springframework.expression.Expression)1 SimpMessageType (org.springframework.messaging.simp.SimpMessageType)1 ConfigAttribute (org.springframework.security.access.ConfigAttribute)1 DefaultMessageSecurityMetadataSource (org.springframework.security.messaging.access.intercept.DefaultMessageSecurityMetadataSource)1 SimpDestinationMessageMatcher (org.springframework.security.messaging.util.matcher.SimpDestinationMessageMatcher)1 SimpMessageTypeMatcher (org.springframework.security.messaging.util.matcher.SimpMessageTypeMatcher)1