Example usage for org.springframework.security.access.annotation Jsr250MethodSecurityMetadataSource setDefaultRolePrefix

List of usage examples for org.springframework.security.access.annotation Jsr250MethodSecurityMetadataSource setDefaultRolePrefix

Introduction

In this page you can find the example usage for org.springframework.security.access.annotation Jsr250MethodSecurityMetadataSource setDefaultRolePrefix.

Prototype

public void setDefaultRolePrefix(String defaultRolePrefix) 

Source Link

Document

Sets the default prefix to be added to RolesAllowed .

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}.//ww w .j av a 2s . 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);
}