1
0
mirror of synced 2026-08-05 01:36:56 +00:00

SEC-1900: AbstractAuthorizeTag now compares using getAuthority()

This avoids backwards compatibility issues with other GrantedAuthority
implementations.
This commit is contained in:
Christian Hilmersson
2012-02-22 01:46:33 +01:00
committed by Rob Winch
parent c446697de3
commit d57f1d56d5
2 changed files with 86 additions and 30 deletions
@@ -20,6 +20,7 @@ import static org.junit.Assert.*;
import org.junit.*;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.context.SecurityContextHolder;
import javax.servlet.jsp.JspException;
@@ -73,4 +74,56 @@ public class AuthorizeTagCustomGrantedAuthorityTests {
assertTrue("expected", true);
}
}
@Test
@SuppressWarnings("serial")
public void testAuthorizeCustomGrantedAuthority() throws JspException {
authorizeTag.setIfAnyGranted(null);
authorizeTag.setIfNotGranted(null);
authorizeTag.setIfAllGranted("ROLE_TEST");
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
authorities.add(new GrantedAuthority() {
public String getAuthority() {
return "ROLE_TEST";
}
});
SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken("abc", "123", authorities));
assertEquals("Expected to be authorized", Tag.EVAL_BODY_INCLUDE, authorizeTag.doStartTag());
}
@Test
@SuppressWarnings("serial")
public void testAuthorizeExtendsGrantedAuthorityImpl() throws JspException {
authorizeTag.setIfAnyGranted(null);
authorizeTag.setIfNotGranted(null);
authorizeTag.setIfAllGranted("ROLE_TEST");
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
authorities.add(new GrantedAuthorityImpl("ROLE_TEST") {});
SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken("abc", "123", authorities));
assertEquals("Expected to be authorized", Tag.EVAL_BODY_INCLUDE, authorizeTag.doStartTag());
}
// SEC-1900
@Test
public void testAuthorizeUsingGrantedAuthorityImpl() throws JspException {
authorizeTag.setIfAnyGranted(null);
authorizeTag.setIfNotGranted(null);
authorizeTag.setIfAllGranted("ROLE_TEST");
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
authorities.add(new GrantedAuthorityImpl("ROLE_TEST"));
SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken("abc", "123", authorities));
assertEquals("Expected to be authorized", Tag.EVAL_BODY_INCLUDE, authorizeTag.doStartTag());
}
// SEC-1900
@Test
public void testNotAuthorizeUsingGrantedAuthorityImpl() throws JspException {
authorizeTag.setIfAnyGranted(null);
authorizeTag.setIfNotGranted(null);
authorizeTag.setIfAllGranted("ROLE_ADMIN");
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
authorities.add(new GrantedAuthorityImpl("ROLE_TEST"));
SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken("abc", "123", authorities));
assertEquals("Expected to not be authorized", Tag.SKIP_BODY, authorizeTag.doStartTag());
}
}