mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-06-24 04:52:16 +00:00
SEC-1583: Added hasAuthority and hasAnyAuthority imlementations to SecurityExpressionRoot.
This commit is contained in:
parent
1739628e6a
commit
d6f408e8bf
@ -36,6 +36,14 @@ public abstract class SecurityExpressionRoot {
|
|||||||
this.authentication = a;
|
this.authentication = a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public final boolean hasAuthority(String authority) {
|
||||||
|
return hasRole(authority);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final boolean hasAnyAuthority(String... authorities) {
|
||||||
|
return hasAnyRole(authorities);
|
||||||
|
}
|
||||||
|
|
||||||
public final boolean hasRole(String role) {
|
public final boolean hasRole(String role) {
|
||||||
return getAuthoritySet().contains(role);
|
return getAuthoritySet().contains(role);
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,16 @@
|
|||||||
package org.springframework.security.access.expression;
|
package org.springframework.security.access.expression;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
|
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
|
||||||
|
import org.springframework.security.authentication.AuthenticationTrustResolver;
|
||||||
import org.springframework.security.authentication.TestingAuthenticationToken;
|
import org.springframework.security.authentication.TestingAuthenticationToken;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.GrantedAuthority;
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
import org.springframework.security.core.authority.AuthorityUtils;
|
import org.springframework.security.core.authority.AuthorityUtils;
|
||||||
|
|
||||||
@ -16,11 +20,30 @@ import org.springframework.security.core.authority.AuthorityUtils;
|
|||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public class SecurityExpressionRootTests {
|
public class SecurityExpressionRootTests {
|
||||||
|
private final Authentication JOE = new TestingAuthenticationToken("joe", "pass", "A", "B");
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void denyAllIsFalsePermitAllTrue() throws Exception {
|
||||||
|
SecurityExpressionRoot root = new SecurityExpressionRoot(JOE) {};
|
||||||
|
assertFalse(root.denyAll());
|
||||||
|
assertFalse(root.denyAll);
|
||||||
|
assertTrue(root.permitAll());
|
||||||
|
assertTrue(root.permitAll);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void rememberMeIsCorrectlyDetected() throws Exception {
|
||||||
|
SecurityExpressionRoot root = new SecurityExpressionRoot(JOE) {};
|
||||||
|
AuthenticationTrustResolver atr = mock(AuthenticationTrustResolver.class);
|
||||||
|
root.setTrustResolver(atr);
|
||||||
|
when(atr.isRememberMe(JOE)).thenReturn(true);
|
||||||
|
assertTrue(root.isRememberMe());
|
||||||
|
assertFalse(root.isFullyAuthenticated());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void roleHierarchySupportIsCorrectlyUsedInEvaluatingRoles() throws Exception {
|
public void roleHierarchySupportIsCorrectlyUsedInEvaluatingRoles() throws Exception {
|
||||||
SecurityExpressionRoot root =
|
SecurityExpressionRoot root = new SecurityExpressionRoot(JOE) {};
|
||||||
new SecurityExpressionRoot(new TestingAuthenticationToken("joe", "pass", "A", "B")) {};
|
|
||||||
|
|
||||||
root.setRoleHierarchy(new RoleHierarchy() {
|
root.setRoleHierarchy(new RoleHierarchy() {
|
||||||
public Collection<GrantedAuthority> getReachableGrantedAuthorities(Collection<GrantedAuthority> authorities) {
|
public Collection<GrantedAuthority> getReachableGrantedAuthorities(Collection<GrantedAuthority> authorities) {
|
||||||
@ -29,9 +52,11 @@ public class SecurityExpressionRootTests {
|
|||||||
});
|
});
|
||||||
|
|
||||||
assertTrue(root.hasRole("C"));
|
assertTrue(root.hasRole("C"));
|
||||||
|
assertTrue(root.hasAuthority("C"));
|
||||||
assertFalse(root.hasRole("A"));
|
assertFalse(root.hasRole("A"));
|
||||||
assertFalse(root.hasRole("B"));
|
assertFalse(root.hasRole("B"));
|
||||||
assertTrue(root.hasAnyRole("C", "A", "B"));
|
assertTrue(root.hasAnyRole("C", "A", "B"));
|
||||||
|
assertTrue(root.hasAnyAuthority("C", "A", "B"));
|
||||||
assertFalse(root.hasAnyRole("A", "B"));
|
assertFalse(root.hasAnyRole("A", "B"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user