Example usage for org.springframework.security.access.method DelegatingMethodSecurityMetadataSource DelegatingMethodSecurityMetadataSource

List of usage examples for org.springframework.security.access.method DelegatingMethodSecurityMetadataSource DelegatingMethodSecurityMetadataSource

Introduction

In this page you can find the example usage for org.springframework.security.access.method DelegatingMethodSecurityMetadataSource DelegatingMethodSecurityMetadataSource.

Prototype

public DelegatingMethodSecurityMetadataSource(
            List<MethodSecurityMetadataSource> methodSecurityMetadataSources) 

Source Link

Usage

From source file:org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration.java

/**
 * Provides the default {@link MethodSecurityMetadataSource} that will be used. It
 * creates a {@link DelegatingMethodSecurityMetadataSource} based upon
 * {@link #customMethodSecurityMetadataSource()} and the attributes on
 * {@link EnableGlobalMethodSecurity}./*w ww .  j a v  a  2  s  .c  o  m*/
 *
 * @return the {@link MethodSecurityMetadataSource}
 */
@Bean
public MethodSecurityMetadataSource methodSecurityMetadataSource() {
    List<MethodSecurityMetadataSource> sources = new ArrayList<>();
    ExpressionBasedAnnotationAttributeFactory attributeFactory = new ExpressionBasedAnnotationAttributeFactory(
            getExpressionHandler());
    MethodSecurityMetadataSource customMethodSecurityMetadataSource = customMethodSecurityMetadataSource();
    if (customMethodSecurityMetadataSource != null) {
        sources.add(customMethodSecurityMetadataSource);
    }

    boolean hasCustom = customMethodSecurityMetadataSource != null;
    boolean isPrePostEnabled = prePostEnabled();
    boolean isSecuredEnabled = securedEnabled();
    boolean isJsr250Enabled = jsr250Enabled();

    if (!isPrePostEnabled && !isSecuredEnabled && !isJsr250Enabled && !hasCustom) {
        throw new IllegalStateException("In the composition of all global method configuration, "
                + "no annotation support was actually activated");
    }

    if (isPrePostEnabled) {
        sources.add(new PrePostAnnotationSecurityMetadataSource(attributeFactory));
    }
    if (isSecuredEnabled) {
        sources.add(new SecuredAnnotationSecurityMetadataSource());
    }
    if (isJsr250Enabled) {
        GrantedAuthorityDefaults grantedAuthorityDefaults = getSingleBeanOrNull(GrantedAuthorityDefaults.class);
        Jsr250MethodSecurityMetadataSource jsr250MethodSecurityMetadataSource = this.context
                .getBean(Jsr250MethodSecurityMetadataSource.class);
        if (grantedAuthorityDefaults != null) {
            jsr250MethodSecurityMetadataSource.setDefaultRolePrefix(grantedAuthorityDefaults.getRolePrefix());
        }
        sources.add(jsr250MethodSecurityMetadataSource);
    }
    return new DelegatingMethodSecurityMetadataSource(sources);
}