SEC-1012: Refactoring of use of GrantedAuthority[] to generified collections
This commit is contained in:
@@ -46,16 +46,19 @@ import javax.servlet.jsp.tagext.TagSupport;
|
||||
|
||||
/**
|
||||
* An implementation of {@link javax.servlet.jsp.tagext.Tag} that allows its body through if some authorizations
|
||||
* are granted to the request's principal.<P>Only works with permissions that are subclasses of {@link
|
||||
* org.springframework.security.acl.basic.BasicAclEntry}.</p>
|
||||
* <p>One or more comma separate integer permissions are specified via the <code>hasPermission</code> attribute.
|
||||
* are granted to the request's principal.
|
||||
* <p>
|
||||
* Only works with permissions that are subclasses of {@link org.springframework.security.acl.basic.BasicAclEntry}.
|
||||
* <p>
|
||||
* One or more comma separate integer permissions are specified via the <code>hasPermission</code> attribute.
|
||||
* The tag will include its body if <b>any</b> of the integer permissions have been granted to the current
|
||||
* <code>Authentication</code> (obtained from the <code>SecurityContextHolder</code>).</p>
|
||||
* <p>For this class to operate it must be able to access the application context via the
|
||||
* <code>Authentication</code> (obtained from the <code>SecurityContextHolder</code>).
|
||||
* <p>
|
||||
* For this class to operate it must be able to access the application context via the
|
||||
* <code>WebApplicationContextUtils</code> and locate an {@link AclManager}. Application contexts have no need to have
|
||||
* more than one <code>AclManager</code> (as a provider-based implementation can be used so that it locates a provider
|
||||
* that is authoritative for the given domain object instance), so the first <code>AclManager</code> located will be
|
||||
* used.</p>
|
||||
* used.
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
|
||||
@@ -15,17 +15,6 @@
|
||||
|
||||
package org.springframework.security.taglibs.authz;
|
||||
|
||||
import org.springframework.security.Authentication;
|
||||
import org.springframework.security.GrantedAuthority;
|
||||
import org.springframework.security.GrantedAuthorityImpl;
|
||||
|
||||
import org.springframework.security.context.SecurityContextHolder;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import org.springframework.web.util.ExpressionEvaluationUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
@@ -36,6 +25,13 @@ import javax.servlet.jsp.JspException;
|
||||
import javax.servlet.jsp.tagext.Tag;
|
||||
import javax.servlet.jsp.tagext.TagSupport;
|
||||
|
||||
import org.springframework.security.Authentication;
|
||||
import org.springframework.security.GrantedAuthority;
|
||||
import org.springframework.security.GrantedAuthorityImpl;
|
||||
import org.springframework.security.context.SecurityContextHolder;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.util.ExpressionEvaluationUtils;
|
||||
|
||||
|
||||
/**
|
||||
* An implementation of {@link javax.servlet.jsp.tagext.Tag} that allows it's body through if some authorizations
|
||||
@@ -125,20 +121,18 @@ public class AuthorizeTag extends TagSupport {
|
||||
return ifNotGranted;
|
||||
}
|
||||
|
||||
private Collection getPrincipalAuthorities() {
|
||||
private Collection<GrantedAuthority> getPrincipalAuthorities() {
|
||||
Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();
|
||||
|
||||
if (null == currentUser) {
|
||||
return Collections.EMPTY_LIST;
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
if ((null == currentUser.getAuthorities()) || (currentUser.getAuthorities().length < 1)) {
|
||||
return Collections.EMPTY_LIST;
|
||||
if ((null == currentUser.getAuthorities())) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
Collection granted = Arrays.asList(currentUser.getAuthorities());
|
||||
|
||||
return granted;
|
||||
return currentUser.getAuthorities();
|
||||
}
|
||||
|
||||
private Set parseAuthoritiesString(String authorizationsString) {
|
||||
|
||||
@@ -17,8 +17,6 @@ package org.springframework.security.taglibs.velocity;
|
||||
|
||||
import org.springframework.security.Authentication;
|
||||
|
||||
import org.springframework.security.acl.AclManager;
|
||||
|
||||
import org.springframework.security.taglibs.authz.AclTag;
|
||||
import org.springframework.security.taglibs.authz.AuthenticationTag;
|
||||
import org.springframework.security.taglibs.authz.AuthorizeTag;
|
||||
@@ -39,7 +37,7 @@ public interface Authz {
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
/**
|
||||
* all the listed roles must be granted to return true, otherwise fasle;
|
||||
* all the listed roles must be granted to return true, otherwise false;
|
||||
*
|
||||
* @param roles - comma separate GrantedAuthoritys
|
||||
*
|
||||
@@ -48,7 +46,7 @@ public interface Authz {
|
||||
boolean allGranted(String roles);
|
||||
|
||||
/**
|
||||
* any the listed roles must be granted to return true, otherwise fasle;
|
||||
* any the listed roles must be granted to return true, otherwise false;
|
||||
*
|
||||
* @param roles - comma separate GrantedAuthoritys
|
||||
*
|
||||
@@ -57,9 +55,8 @@ public interface Authz {
|
||||
boolean anyGranted(String roles);
|
||||
|
||||
/**
|
||||
* set Spring application context which contains acegi related bean
|
||||
* get Spring application context which contains
|
||||
*
|
||||
* @return DOCUMENT ME!
|
||||
*/
|
||||
ApplicationContext getAppCtx();
|
||||
|
||||
@@ -72,20 +69,7 @@ public interface Authz {
|
||||
String getPrincipal();
|
||||
|
||||
/**
|
||||
* return true if the principal holds either permission specified for the provided domain object<P>Only
|
||||
* works with permissions that are subclasses of {@link org.springframework.security.acl.basic.AbstractBasicAclEntry}.</p>
|
||||
* <p>For this class to operate it must be able to access the application context via the
|
||||
* <code>WebApplicationContextUtils</code> and locate an {@link AclManager}.</p>
|
||||
*
|
||||
* @param domainObject - domain object need acl control
|
||||
* @param permissions - comma separate integer permissions
|
||||
*
|
||||
* @return got acl permission (true|false)
|
||||
*/
|
||||
boolean hasPermission(Object domainObject, String permissions);
|
||||
|
||||
/**
|
||||
* none the listed roles must be granted to return true, otherwise fasle;
|
||||
* none the listed roles must be granted to return true, otherwise false;
|
||||
*
|
||||
* @param roles - comma separate GrantedAuthoritys
|
||||
*
|
||||
@@ -94,9 +78,8 @@ public interface Authz {
|
||||
boolean noneGranted(String roles);
|
||||
|
||||
/**
|
||||
* get Spring application context which contains acegi related bean
|
||||
* set Spring application context which contains Acegi related bean
|
||||
*
|
||||
* @param appCtx DOCUMENT ME!
|
||||
*/
|
||||
void setAppCtx(ApplicationContext appCtx);
|
||||
}
|
||||
|
||||
@@ -15,17 +15,12 @@
|
||||
|
||||
package org.springframework.security.taglibs.velocity;
|
||||
|
||||
import org.springframework.security.acl.AclManager;
|
||||
|
||||
import org.springframework.security.taglibs.authz.AclTag;
|
||||
import org.springframework.security.taglibs.authz.AuthenticationTag;
|
||||
import org.springframework.security.taglibs.authz.AuthorizeTag;
|
||||
import javax.servlet.jsp.JspException;
|
||||
import javax.servlet.jsp.tagext.Tag;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import javax.servlet.jsp.JspException;
|
||||
import javax.servlet.jsp.PageContext;
|
||||
import javax.servlet.jsp.tagext.Tag;
|
||||
import org.springframework.security.taglibs.authz.AuthenticationTag;
|
||||
import org.springframework.security.taglibs.authz.AuthorizeTag;
|
||||
|
||||
|
||||
/**
|
||||
@@ -60,18 +55,14 @@ public class AuthzImpl implements Authz {
|
||||
|
||||
/**
|
||||
* implementation of AuthenticationTag
|
||||
*
|
||||
* @return DOCUMENT ME!
|
||||
*
|
||||
* @throws IllegalArgumentException DOCUMENT ME!
|
||||
*/
|
||||
public String getPrincipal() {
|
||||
MyAuthenticationTag authenticationTag = new MyAuthenticationTag();
|
||||
|
||||
authenticationTag.setProperty("username");
|
||||
authenticationTag.setProperty("name");
|
||||
|
||||
try {
|
||||
authenticationTag.doStartTag();
|
||||
authenticationTag.doEndTag();
|
||||
} catch (JspException je) {
|
||||
je.printStackTrace();
|
||||
throw new IllegalArgumentException(je.getMessage());
|
||||
@@ -80,38 +71,6 @@ public class AuthzImpl implements Authz {
|
||||
return authenticationTag.getLastMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* implementation of AclTag
|
||||
*
|
||||
* @param domainObject DOCUMENT ME!
|
||||
* @param permissions DOCUMENT ME!
|
||||
*
|
||||
* @return DOCUMENT ME!
|
||||
*
|
||||
* @throws IllegalArgumentException DOCUMENT ME!
|
||||
*/
|
||||
public boolean hasPermission(Object domainObject, String permissions) {
|
||||
MyAclTag aclTag = new MyAclTag();
|
||||
aclTag.setPageContext(null);
|
||||
aclTag.setContext(getAppCtx());
|
||||
aclTag.setDomainObject(domainObject);
|
||||
aclTag.setHasPermission(permissions);
|
||||
|
||||
int result = -1;
|
||||
|
||||
try {
|
||||
result = aclTag.doStartTag();
|
||||
} catch (JspException je) {
|
||||
throw new IllegalArgumentException(je.getMessage());
|
||||
}
|
||||
|
||||
if (Tag.EVAL_BODY_INCLUDE == result) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* implementation of AuthorizeTag
|
||||
*
|
||||
@@ -166,8 +125,6 @@ public class AuthzImpl implements Authz {
|
||||
|
||||
/**
|
||||
* test case can use this class to mock application context with aclManager bean in it.
|
||||
*
|
||||
* @param appCtx DOCUMENT ME!
|
||||
*/
|
||||
public void setAppCtx(ApplicationContext appCtx) {
|
||||
this.appCtx = appCtx;
|
||||
@@ -175,24 +132,6 @@ public class AuthzImpl implements Authz {
|
||||
|
||||
//~ Inner Classes ==================================================================================================
|
||||
|
||||
/**
|
||||
* AclTag need to access the application context via the <code> WebApplicationContextUtils</code> and
|
||||
* locate an {@link AclManager}. WebApplicationContextUtils get application context via ServletContext. I decided
|
||||
* to let the Authz provide the Spring application context.
|
||||
*/
|
||||
private class MyAclTag extends AclTag {
|
||||
private static final long serialVersionUID = 6752340622125924108L;
|
||||
ApplicationContext context;
|
||||
|
||||
protected ApplicationContext getContext(PageContext pageContext) {
|
||||
return context;
|
||||
}
|
||||
|
||||
protected void setContext(ApplicationContext context) {
|
||||
this.context = context;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* it must output somthing to JSP page, so have to override the writeMessage method to avoid JSP related
|
||||
* operation. Get Idea from Acegi Test class.
|
||||
|
||||
Reference in New Issue
Block a user