Example usage for org.springframework.security.config Elements INTERCEPT_MESSAGE

List of usage examples for org.springframework.security.config Elements INTERCEPT_MESSAGE

Introduction

In this page you can find the example usage for org.springframework.security.config Elements INTERCEPT_MESSAGE.

Prototype

String INTERCEPT_MESSAGE

To view the source code for org.springframework.security.config Elements INTERCEPT_MESSAGE.

Click Source Link

Usage

From source file:org.springframework.security.config.message.MessageSecurityBeanDefinitionParser.java

/**
 * @param element/*  ww w.  ja  va  2 s.  c om*/
 * @param parserContext
 * @return
 */
public BeanDefinition parse(Element element, ParserContext parserContext) {
    BeanDefinitionRegistry registry = parserContext.getRegistry();
    XmlReaderContext context = parserContext.getReaderContext();

    ManagedMap<BeanDefinition, String> matcherToExpression = new ManagedMap<BeanDefinition, String>();

    String id = element.getAttribute(ID_ATTR);

    List<Element> interceptMessages = DomUtils.getChildElementsByTagName(element, Elements.INTERCEPT_MESSAGE);
    for (Element interceptMessage : interceptMessages) {
        String matcherPattern = interceptMessage.getAttribute(PATTERN_ATTR);
        String accessExpression = interceptMessage.getAttribute(ACCESS_ATTR);
        BeanDefinitionBuilder matcher = BeanDefinitionBuilder
                .rootBeanDefinition(SimpDestinationMessageMatcher.class);
        matcher.addConstructorArgValue(matcherPattern);
        matcherToExpression.put(matcher.getBeanDefinition(), accessExpression);
    }

    BeanDefinitionBuilder mds = BeanDefinitionBuilder
            .rootBeanDefinition(ExpressionBasedMessageSecurityMetadataSourceFactory.class);
    mds.setFactoryMethod("createExpressionMessageMetadataSource");
    mds.addConstructorArgValue(matcherToExpression);

    String mdsId = context.registerWithGeneratedName(mds.getBeanDefinition());

    ManagedList<BeanDefinition> voters = new ManagedList<BeanDefinition>();
    voters.add(new RootBeanDefinition(MessageExpressionVoter.class));
    BeanDefinitionBuilder adm = BeanDefinitionBuilder.rootBeanDefinition(ConsensusBased.class);
    adm.addConstructorArgValue(voters);

    BeanDefinitionBuilder inboundChannelSecurityInterceptor = BeanDefinitionBuilder
            .rootBeanDefinition(ChannelSecurityInterceptor.class);
    inboundChannelSecurityInterceptor.addConstructorArgValue(registry.getBeanDefinition(mdsId));
    inboundChannelSecurityInterceptor.addPropertyValue("accessDecisionManager", adm.getBeanDefinition());
    String inSecurityInterceptorName = context
            .registerWithGeneratedName(inboundChannelSecurityInterceptor.getBeanDefinition());

    if (StringUtils.hasText(id)) {
        registry.registerAlias(inSecurityInterceptorName, id);
    } else {
        BeanDefinitionBuilder mspp = BeanDefinitionBuilder
                .rootBeanDefinition(MessageSecurityPostProcessor.class);
        mspp.addConstructorArgValue(inSecurityInterceptorName);
        context.registerWithGeneratedName(mspp.getBeanDefinition());
    }

    return null;
}

From source file:org.springframework.security.config.websocket.WebSocketMessageBrokerSecurityBeanDefinitionParser.java

/**
 * @param element//from   w ww .ja  v  a2 s .  c  o  m
 * @param parserContext
 * @return
 */
public BeanDefinition parse(Element element, ParserContext parserContext) {
    BeanDefinitionRegistry registry = parserContext.getRegistry();
    XmlReaderContext context = parserContext.getReaderContext();

    ManagedMap<BeanDefinition, String> matcherToExpression = new ManagedMap<BeanDefinition, String>();

    String id = element.getAttribute(ID_ATTR);
    boolean sameOriginDisabled = Boolean.parseBoolean(element.getAttribute(DISABLED_ATTR));

    List<Element> interceptMessages = DomUtils.getChildElementsByTagName(element, Elements.INTERCEPT_MESSAGE);
    for (Element interceptMessage : interceptMessages) {
        String matcherPattern = interceptMessage.getAttribute(PATTERN_ATTR);
        String accessExpression = interceptMessage.getAttribute(ACCESS_ATTR);
        String messageType = interceptMessage.getAttribute(TYPE_ATTR);

        BeanDefinition matcher = createMatcher(matcherPattern, messageType, parserContext, interceptMessage);
        matcherToExpression.put(matcher, accessExpression);
    }

    BeanDefinitionBuilder mds = BeanDefinitionBuilder
            .rootBeanDefinition(ExpressionBasedMessageSecurityMetadataSourceFactory.class);
    mds.setFactoryMethod("createExpressionMessageMetadataSource");
    mds.addConstructorArgValue(matcherToExpression);

    String mdsId = context.registerWithGeneratedName(mds.getBeanDefinition());

    ManagedList<BeanDefinition> voters = new ManagedList<BeanDefinition>();
    voters.add(new RootBeanDefinition(MessageExpressionVoter.class));
    BeanDefinitionBuilder adm = BeanDefinitionBuilder.rootBeanDefinition(ConsensusBased.class);
    adm.addConstructorArgValue(voters);

    BeanDefinitionBuilder inboundChannelSecurityInterceptor = BeanDefinitionBuilder
            .rootBeanDefinition(ChannelSecurityInterceptor.class);
    inboundChannelSecurityInterceptor.addConstructorArgValue(registry.getBeanDefinition(mdsId));
    inboundChannelSecurityInterceptor.addPropertyValue("accessDecisionManager", adm.getBeanDefinition());
    String inSecurityInterceptorName = context
            .registerWithGeneratedName(inboundChannelSecurityInterceptor.getBeanDefinition());

    if (StringUtils.hasText(id)) {
        registry.registerAlias(inSecurityInterceptorName, id);

        if (!registry.containsBeanDefinition(PATH_MATCHER_BEAN_NAME)) {
            registry.registerBeanDefinition(PATH_MATCHER_BEAN_NAME,
                    new RootBeanDefinition(AntPathMatcher.class));
        }
    } else {
        BeanDefinitionBuilder mspp = BeanDefinitionBuilder
                .rootBeanDefinition(MessageSecurityPostProcessor.class);
        mspp.addConstructorArgValue(inSecurityInterceptorName);
        mspp.addConstructorArgValue(sameOriginDisabled);
        context.registerWithGeneratedName(mspp.getBeanDefinition());
    }

    return null;
}