Search in sources :

Example 11 with AffirmativeBased

use of org.springframework.security.access.vote.AffirmativeBased in project spring-integration by spring-projects.

the class ChannelSecurityInterceptorTests method createInterceptor.

@SuppressWarnings("rawtypes")
private static ChannelSecurityInterceptor createInterceptor(String role) throws Exception {
    ChannelSecurityMetadataSource securityMetadataSource = new ChannelSecurityMetadataSource();
    securityMetadataSource.addPatternMapping(Pattern.compile("secured.*"), new DefaultChannelAccessPolicy(role, null));
    ChannelSecurityInterceptor interceptor = new ChannelSecurityInterceptor(securityMetadataSource);
    AffirmativeBased accessDecisionManager = AffirmativeBased.class.getConstructor(List.class).newInstance(Collections.singletonList(new RoleVoter()));
    accessDecisionManager.afterPropertiesSet();
    interceptor.setAccessDecisionManager(accessDecisionManager);
    interceptor.setAuthenticationManager(new MockAuthenticationManager(true));
    interceptor.afterPropertiesSet();
    return interceptor;
}
Also used : AffirmativeBased(org.springframework.security.access.vote.AffirmativeBased) MockAuthenticationManager(org.springframework.integration.security.MockAuthenticationManager) List(java.util.List) RoleVoter(org.springframework.security.access.vote.RoleVoter)

Example 12 with AffirmativeBased

use of org.springframework.security.access.vote.AffirmativeBased in project spring-security by spring-projects.

the class AbstractSecurityWebSocketMessageBrokerConfigurer method inboundChannelSecurity.

@Bean
public ChannelSecurityInterceptor inboundChannelSecurity(MessageSecurityMetadataSource messageSecurityMetadataSource) {
    ChannelSecurityInterceptor channelSecurityInterceptor = new ChannelSecurityInterceptor(messageSecurityMetadataSource);
    MessageExpressionVoter<Object> voter = new MessageExpressionVoter<>();
    voter.setExpressionHandler(getMessageExpressionHandler());
    List<AccessDecisionVoter<?>> voters = new ArrayList<>();
    voters.add(voter);
    AffirmativeBased manager = new AffirmativeBased(voters);
    channelSecurityInterceptor.setAccessDecisionManager(manager);
    return channelSecurityInterceptor;
}
Also used : MessageExpressionVoter(org.springframework.security.messaging.access.expression.MessageExpressionVoter) AffirmativeBased(org.springframework.security.access.vote.AffirmativeBased) ArrayList(java.util.ArrayList) AccessDecisionVoter(org.springframework.security.access.AccessDecisionVoter) ChannelSecurityInterceptor(org.springframework.security.messaging.access.intercept.ChannelSecurityInterceptor) Bean(org.springframework.context.annotation.Bean)

Example 13 with AffirmativeBased

use of org.springframework.security.access.vote.AffirmativeBased in project spring-security by spring-projects.

the class GlobalMethodSecurityConfiguration method accessDecisionManager.

/**
 * Allows subclasses to provide a custom {@link AccessDecisionManager}. The default is
 * a {@link AffirmativeBased} with the following voters:
 *
 * <ul>
 * <li>{@link PreInvocationAuthorizationAdviceVoter}</li>
 * <li>{@link RoleVoter}</li>
 * <li>{@link AuthenticatedVoter}</li>
 * </ul>
 * @return the {@link AccessDecisionManager} to use
 */
protected AccessDecisionManager accessDecisionManager() {
    List<AccessDecisionVoter<?>> decisionVoters = new ArrayList<>();
    if (prePostEnabled()) {
        ExpressionBasedPreInvocationAdvice expressionAdvice = new ExpressionBasedPreInvocationAdvice();
        expressionAdvice.setExpressionHandler(getExpressionHandler());
        decisionVoters.add(new PreInvocationAuthorizationAdviceVoter(expressionAdvice));
    }
    if (jsr250Enabled()) {
        decisionVoters.add(new Jsr250Voter());
    }
    RoleVoter roleVoter = new RoleVoter();
    GrantedAuthorityDefaults grantedAuthorityDefaults = getSingleBeanOrNull(GrantedAuthorityDefaults.class);
    if (grantedAuthorityDefaults != null) {
        roleVoter.setRolePrefix(grantedAuthorityDefaults.getRolePrefix());
    }
    decisionVoters.add(roleVoter);
    decisionVoters.add(new AuthenticatedVoter());
    return new AffirmativeBased(decisionVoters);
}
Also used : AuthenticatedVoter(org.springframework.security.access.vote.AuthenticatedVoter) Jsr250Voter(org.springframework.security.access.annotation.Jsr250Voter) GrantedAuthorityDefaults(org.springframework.security.config.core.GrantedAuthorityDefaults) AffirmativeBased(org.springframework.security.access.vote.AffirmativeBased) ArrayList(java.util.ArrayList) RoleVoter(org.springframework.security.access.vote.RoleVoter) AccessDecisionVoter(org.springframework.security.access.AccessDecisionVoter) ExpressionBasedPreInvocationAdvice(org.springframework.security.access.expression.method.ExpressionBasedPreInvocationAdvice) PreInvocationAuthorizationAdviceVoter(org.springframework.security.access.prepost.PreInvocationAuthorizationAdviceVoter)

Example 14 with AffirmativeBased

use of org.springframework.security.access.vote.AffirmativeBased in project spring-security by spring-projects.

the class GlobalMethodSecurityBeanDefinitionParserTests method expressionVoterAndAfterInvocationProviderUseSameExpressionHandlerInstance.

// Expression configuration tests
@SuppressWarnings("unchecked")
@Test
public void expressionVoterAndAfterInvocationProviderUseSameExpressionHandlerInstance() throws Exception {
    setContext("<global-method-security pre-post-annotations='enabled'/>" + ConfigTestUtils.AUTH_PROVIDER_XML);
    AffirmativeBased adm = (AffirmativeBased) this.appContext.getBeansOfType(AffirmativeBased.class).values().toArray()[0];
    List voters = (List) FieldUtils.getFieldValue(adm, "decisionVoters");
    PreInvocationAuthorizationAdviceVoter mev = (PreInvocationAuthorizationAdviceVoter) voters.get(0);
    MethodSecurityMetadataSourceAdvisor msi = (MethodSecurityMetadataSourceAdvisor) this.appContext.getBeansOfType(MethodSecurityMetadataSourceAdvisor.class).values().toArray()[0];
    AfterInvocationProviderManager pm = (AfterInvocationProviderManager) ((MethodSecurityInterceptor) msi.getAdvice()).getAfterInvocationManager();
    PostInvocationAdviceProvider aip = (PostInvocationAdviceProvider) pm.getProviders().get(0);
    assertThat(FieldUtils.getFieldValue(mev, "preAdvice.expressionHandler")).isSameAs(FieldUtils.getFieldValue(aip, "postAdvice.expressionHandler"));
}
Also used : PostInvocationAdviceProvider(org.springframework.security.access.prepost.PostInvocationAdviceProvider) MethodSecurityMetadataSourceAdvisor(org.springframework.security.access.intercept.aopalliance.MethodSecurityMetadataSourceAdvisor) AfterInvocationProviderManager(org.springframework.security.access.intercept.AfterInvocationProviderManager) AffirmativeBased(org.springframework.security.access.vote.AffirmativeBased) ArrayList(java.util.ArrayList) List(java.util.List) PreInvocationAuthorizationAdviceVoter(org.springframework.security.access.prepost.PreInvocationAuthorizationAdviceVoter) Test(org.junit.jupiter.api.Test)

Aggregations

AffirmativeBased (org.springframework.security.access.vote.AffirmativeBased)14 ArrayList (java.util.ArrayList)9 AccessDecisionVoter (org.springframework.security.access.AccessDecisionVoter)9 RoleVoter (org.springframework.security.access.vote.RoleVoter)8 Bean (org.springframework.context.annotation.Bean)4 PreInvocationAuthorizationAdviceVoter (org.springframework.security.access.prepost.PreInvocationAuthorizationAdviceVoter)4 ExpressionBasedPreInvocationAdvice (org.springframework.security.access.expression.method.ExpressionBasedPreInvocationAdvice)3 List (java.util.List)2 AccessDecisionManager (org.springframework.security.access.AccessDecisionManager)2 ConfigAttribute (org.springframework.security.access.ConfigAttribute)2 SecurityConfig (org.springframework.security.access.SecurityConfig)2 SecuredAnnotationSecurityMetadataSource (org.springframework.security.access.annotation.SecuredAnnotationSecurityMetadataSource)2 AspectJMethodSecurityInterceptor (org.springframework.security.access.intercept.aspectj.AspectJMethodSecurityInterceptor)2 AuthenticatedVoter (org.springframework.security.access.vote.AuthenticatedVoter)2 MessageExpressionVoter (org.springframework.security.messaging.access.expression.MessageExpressionVoter)2 ChannelSecurityInterceptor (org.springframework.security.messaging.access.intercept.ChannelSecurityInterceptor)2 WebExpressionVoter (org.springframework.security.web.access.expression.WebExpressionVoter)2 InetAddress (java.net.InetAddress)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1