Search in sources :

Example 1 with RoleHierarchyImpl

use of org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl in project spring-security by spring-projects.

the class DefaultMessageSecurityExpressionHandlerTests method roleHierarchy.

@Test
public void roleHierarchy() {
    authentication = new TestingAuthenticationToken("admin", "pass", "ROLE_ADMIN");
    RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
    roleHierarchy.setHierarchy("ROLE_ADMIN > ROLE_USER");
    handler.setRoleHierarchy(roleHierarchy);
    EvaluationContext context = handler.createEvaluationContext(authentication, message);
    Expression expression = handler.getExpressionParser().parseExpression("hasRole('ROLE_USER')");
    assertThat(ExpressionUtils.evaluateAsBoolean(expression, context)).isTrue();
}
Also used : Expression(org.springframework.expression.Expression) EvaluationContext(org.springframework.expression.EvaluationContext) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) RoleHierarchyImpl(org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl) Test(org.junit.Test)

Example 2 with RoleHierarchyImpl

use of org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl in project spring-security by spring-projects.

the class RoleHierarchyVoterTests method hierarchicalRoleIsIncludedInDecision.

@Test
public void hierarchicalRoleIsIncludedInDecision() {
    RoleHierarchyImpl roleHierarchyImpl = new RoleHierarchyImpl();
    roleHierarchyImpl.setHierarchy("ROLE_A > ROLE_B");
    // User has role A, role B is required
    TestingAuthenticationToken auth = new TestingAuthenticationToken("user", "password", "ROLE_A");
    RoleHierarchyVoter voter = new RoleHierarchyVoter(roleHierarchyImpl);
    assertThat(voter.vote(auth, new Object(), SecurityConfig.createList("ROLE_B"))).isEqualTo(RoleHierarchyVoter.ACCESS_GRANTED);
}
Also used : RoleHierarchyVoter(org.springframework.security.access.vote.RoleHierarchyVoter) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) RoleHierarchyImpl(org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl) Test(org.junit.Test)

Example 3 with RoleHierarchyImpl

use of org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl in project ocvn by devgateway.

the class WebSecurityConfig method roleHierarchy.

/**
     * Enable hierarchical roles. This bean can be used to extract all effective roles.
     */
@Bean
RoleHierarchy roleHierarchy() {
    RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
    roleHierarchy.setHierarchy(roleHierarchyStringRepresentation);
    return roleHierarchy;
}
Also used : RoleHierarchyImpl(org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl) Bean(org.springframework.context.annotation.Bean)

Aggregations

RoleHierarchyImpl (org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl)3 Test (org.junit.Test)2 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)2 Bean (org.springframework.context.annotation.Bean)1 EvaluationContext (org.springframework.expression.EvaluationContext)1 Expression (org.springframework.expression.Expression)1 RoleHierarchyVoter (org.springframework.security.access.vote.RoleHierarchyVoter)1