SEC-2781: Remove deprecations
This commit is contained in:
@@ -16,11 +16,7 @@
|
||||
package org.springframework.security.taglibs.authz;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletContext;
|
||||
@@ -37,7 +33,6 @@ import org.springframework.expression.ParseException;
|
||||
import org.springframework.security.access.expression.ExpressionUtils;
|
||||
import org.springframework.security.access.expression.SecurityExpressionHandler;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.web.FilterInvocation;
|
||||
import org.springframework.security.web.WebAttributes;
|
||||
@@ -63,10 +58,6 @@ public abstract class AbstractAuthorizeTag {
|
||||
private String access;
|
||||
private String url;
|
||||
private String method = "GET";
|
||||
private String ifAllGranted;
|
||||
private String ifAnyGranted;
|
||||
private String ifNotGranted;
|
||||
|
||||
/**
|
||||
* This method allows subclasses to provide a way to access the ServletRequest according to the rendering
|
||||
* technology.
|
||||
@@ -91,7 +82,6 @@ public abstract class AbstractAuthorizeTag {
|
||||
* <ul>
|
||||
* <li>access</li>
|
||||
* <li>url, method</li>
|
||||
* <li>ifAllGranted, ifAnyGranted, ifNotGranted</li>
|
||||
* </ul>
|
||||
* The above combinations are mutually exclusive and evaluated in the given order.
|
||||
*
|
||||
@@ -108,55 +98,13 @@ public abstract class AbstractAuthorizeTag {
|
||||
isAuthorized = authorizeUsingUrlCheck();
|
||||
|
||||
} else {
|
||||
isAuthorized = authorizeUsingGrantedAuthorities();
|
||||
isAuthorized = false;
|
||||
|
||||
}
|
||||
|
||||
return isAuthorized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make an authorization decision by considering ifAllGranted, ifAnyGranted, and ifNotGranted. All 3 or any
|
||||
* combination can be provided. All provided attributes must evaluate to true.
|
||||
*
|
||||
* @return the result of the authorization decision
|
||||
*/
|
||||
public boolean authorizeUsingGrantedAuthorities() {
|
||||
boolean hasTextAllGranted = StringUtils.hasText(getIfAllGranted());
|
||||
boolean hasTextAnyGranted = StringUtils.hasText(getIfAnyGranted());
|
||||
boolean hasTextNotGranted = StringUtils.hasText(getIfNotGranted());
|
||||
|
||||
if ((!hasTextAllGranted) && (!hasTextAnyGranted) && (!hasTextNotGranted)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final Collection<? extends GrantedAuthority> granted = getPrincipalAuthorities();
|
||||
final Set<String> grantedRoles = authoritiesToRoles(granted);
|
||||
|
||||
if (hasTextAllGranted) {
|
||||
final Set<String> requiredRoles = splitRoles(getIfAllGranted());
|
||||
if (!grantedRoles.containsAll(requiredRoles)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (hasTextAnyGranted) {
|
||||
final Set<String> expectOneOfRoles = splitRoles(getIfAnyGranted());
|
||||
if (!containsAnyValue(grantedRoles, expectOneOfRoles)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (hasTextNotGranted) {
|
||||
final Set<String> expectNoneOfRoles = splitRoles(getIfNotGranted());
|
||||
if (containsAnyValue(expectNoneOfRoles, grantedRoles)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make an authorization decision based on a Spring EL expression. See the "Expression-Based Access Control" chapter
|
||||
* in Spring Security for details on what expressions can be used.
|
||||
@@ -234,82 +182,8 @@ public abstract class AbstractAuthorizeTag {
|
||||
this.method = (method != null) ? method.toUpperCase() : null;
|
||||
}
|
||||
|
||||
public String getIfAllGranted() {
|
||||
return ifAllGranted;
|
||||
}
|
||||
|
||||
public void setIfAllGranted(String ifAllGranted) {
|
||||
this.ifAllGranted = ifAllGranted;
|
||||
}
|
||||
|
||||
public String getIfAnyGranted() {
|
||||
return ifAnyGranted;
|
||||
}
|
||||
|
||||
public void setIfAnyGranted(String ifAnyGranted) {
|
||||
this.ifAnyGranted = ifAnyGranted;
|
||||
}
|
||||
|
||||
public String getIfNotGranted() {
|
||||
return ifNotGranted;
|
||||
}
|
||||
|
||||
public void setIfNotGranted(String ifNotGranted) {
|
||||
this.ifNotGranted = ifNotGranted;
|
||||
}
|
||||
|
||||
/*------------- Private helper methods -----------------*/
|
||||
|
||||
private Collection<? extends GrantedAuthority> getPrincipalAuthorities() {
|
||||
Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (null == currentUser) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return currentUser.getAuthorities();
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits the authorityString using "," as a delimiter into a Set.
|
||||
* @param authorityString
|
||||
* @return
|
||||
*/
|
||||
private Set<String> splitRoles(String authorityString) {
|
||||
String[] rolesArray = StringUtils.tokenizeToStringArray(authorityString, ",");
|
||||
Set<String> roles = new HashSet<String>(rolesArray.length);
|
||||
for(String role : rolesArray) {
|
||||
roles.add(role);
|
||||
}
|
||||
return roles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if any of the values are contained in toTest. Otherwise, false.
|
||||
* @param toTest Check this Set to see if any of the values are contained in it.
|
||||
* @param values The values to check if they are in toTest.
|
||||
* @return
|
||||
*/
|
||||
private boolean containsAnyValue(Set<String> toTest, Collection<String> values) {
|
||||
for(String value : values) {
|
||||
if(toTest.contains(value)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private Set<String> authoritiesToRoles(Collection<? extends GrantedAuthority> c) {
|
||||
Set<String> target = new HashSet<String>();
|
||||
for (GrantedAuthority authority : c) {
|
||||
if (null == authority.getAuthority()) {
|
||||
throw new IllegalArgumentException(
|
||||
"Cannot process GrantedAuthority objects which return null from getAuthority() - attempting to process "
|
||||
+ authority.toString());
|
||||
}
|
||||
target.add(authority.getAuthority());
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private SecurityExpressionHandler<FilterInvocation> getExpressionHandler() throws IOException {
|
||||
ApplicationContext appContext = WebApplicationContextUtils
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.security.taglibs.velocity;
|
||||
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
||||
|
||||
/**
|
||||
* @author Wang Qi
|
||||
*/
|
||||
public interface Authz {
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
/**
|
||||
* all the listed roles must be granted to return true, otherwise false;
|
||||
*
|
||||
* @param roles - comma separate GrantedAuthoritys
|
||||
*
|
||||
* @return granted (true|false)
|
||||
*/
|
||||
boolean allGranted(String roles);
|
||||
|
||||
/**
|
||||
* any the listed roles must be granted to return true, otherwise false;
|
||||
*
|
||||
* @param roles - comma separate GrantedAuthoritys
|
||||
*
|
||||
* @return granted (true|false)
|
||||
*/
|
||||
boolean anyGranted(String roles);
|
||||
|
||||
/**
|
||||
* get Spring application context which contains
|
||||
*
|
||||
*/
|
||||
ApplicationContext getAppCtx();
|
||||
|
||||
/**
|
||||
* return the principal's name, supports the various type of principals that can exist in the {@link
|
||||
* Authentication} object, such as a String or {@link UserDetails} instance
|
||||
*
|
||||
* @return string representation of principal's name
|
||||
*/
|
||||
String getPrincipal();
|
||||
|
||||
/**
|
||||
* none the listed roles must be granted to return true, otherwise false;
|
||||
*
|
||||
* @param roles - comma separate GrantedAuthoritys
|
||||
*
|
||||
* @return granted (true|false)
|
||||
*/
|
||||
boolean noneGranted(String roles);
|
||||
|
||||
/**
|
||||
* set Spring application context which contains Acegi related bean
|
||||
*
|
||||
*/
|
||||
void setAppCtx(ApplicationContext appCtx);
|
||||
}
|
||||
@@ -1,140 +0,0 @@
|
||||
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.security.taglibs.velocity;
|
||||
|
||||
import javax.servlet.jsp.JspException;
|
||||
import javax.servlet.jsp.tagext.Tag;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.security.taglibs.authz.AuthenticationTag;
|
||||
import org.springframework.security.taglibs.authz.JspAuthorizeTag;
|
||||
|
||||
|
||||
/**
|
||||
* I decided to wrap several JSP tag in one class, so I have to using inner class to wrap these JSP tag. To using
|
||||
* this class, you need to inject Spring Context via SetAppCtx() method. AclTag need Spring Context to get AclManger
|
||||
* bean.
|
||||
*/
|
||||
public class AuthzImpl implements Authz {
|
||||
//~ Static fields/initializers =====================================================================================
|
||||
|
||||
static final int ALL_GRANTED = 1;
|
||||
static final int ANY_GRANTED = 2;
|
||||
static final int NONE_GRANTED = 3;
|
||||
|
||||
//~ Instance fields ================================================================================================
|
||||
|
||||
private ApplicationContext appCtx;
|
||||
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
public boolean allGranted(String roles) {
|
||||
return ifGranted(roles, ALL_GRANTED);
|
||||
}
|
||||
|
||||
public boolean anyGranted(String roles) {
|
||||
return ifGranted(roles, ANY_GRANTED);
|
||||
}
|
||||
|
||||
public ApplicationContext getAppCtx() {
|
||||
return appCtx;
|
||||
}
|
||||
|
||||
/**
|
||||
* implementation of AuthenticationTag
|
||||
*/
|
||||
public String getPrincipal() {
|
||||
MyAuthenticationTag authenticationTag = new MyAuthenticationTag();
|
||||
|
||||
authenticationTag.setProperty("name");
|
||||
|
||||
try {
|
||||
authenticationTag.doEndTag();
|
||||
} catch (JspException je) {
|
||||
je.printStackTrace();
|
||||
throw new IllegalArgumentException(je.getMessage());
|
||||
}
|
||||
|
||||
return authenticationTag.getLastMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* implementation of JspAuthorizeTag
|
||||
*/
|
||||
private boolean ifGranted(String roles, int grantType) {
|
||||
JspAuthorizeTag authorizeTag = new JspAuthorizeTag();
|
||||
|
||||
int result;
|
||||
|
||||
try {
|
||||
switch (grantType) {
|
||||
case ALL_GRANTED:
|
||||
authorizeTag.setIfAllGranted(roles);
|
||||
|
||||
break;
|
||||
|
||||
case ANY_GRANTED:
|
||||
authorizeTag.setIfAnyGranted(roles);
|
||||
|
||||
break;
|
||||
|
||||
case NONE_GRANTED:
|
||||
authorizeTag.setIfNotGranted(roles);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new IllegalArgumentException("invalid granted type : " + grantType + " role=" + roles);
|
||||
}
|
||||
|
||||
result = authorizeTag.doStartTag();
|
||||
} catch (JspException je) {
|
||||
throw new IllegalArgumentException(je.getMessage());
|
||||
}
|
||||
|
||||
return Tag.EVAL_BODY_INCLUDE == result;
|
||||
}
|
||||
|
||||
public boolean noneGranted(String roles) {
|
||||
return ifGranted(roles, NONE_GRANTED);
|
||||
}
|
||||
|
||||
/**
|
||||
* test case can use this class to mock application context with aclManager bean in it.
|
||||
*/
|
||||
public void setAppCtx(ApplicationContext appCtx) {
|
||||
this.appCtx = appCtx;
|
||||
}
|
||||
|
||||
//~ Inner Classes ==================================================================================================
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
private class MyAuthenticationTag extends AuthenticationTag {
|
||||
private static final long serialVersionUID = -1094246833893599161L;
|
||||
String lastMessage = null;
|
||||
|
||||
public String getLastMessage() {
|
||||
return lastMessage;
|
||||
}
|
||||
|
||||
protected void writeMessage(String msg) throws JspException {
|
||||
lastMessage = msg;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
package org.springframework.security.taglibs.velocity;
|
||||
|
||||
@@ -74,36 +74,6 @@
|
||||
<required>false</required>
|
||||
<rtexprvalue>false</rtexprvalue>
|
||||
</attribute>
|
||||
|
||||
<attribute>
|
||||
<description>
|
||||
A comma separated list of roles which the user must not have
|
||||
for the body to be output. Deprecated in favour of the access expression.
|
||||
</description>
|
||||
<name>ifNotGranted</name>
|
||||
<required>false</required>
|
||||
<rtexprvalue>true</rtexprvalue>
|
||||
</attribute>
|
||||
|
||||
<attribute>
|
||||
<description>
|
||||
A comma separated list of roles which the user must all
|
||||
possess for the body to be output. Deprecated in favour of the access expression.
|
||||
</description>
|
||||
<name>ifAllGranted</name>
|
||||
<required>false</required>
|
||||
<rtexprvalue>true</rtexprvalue>
|
||||
</attribute>
|
||||
|
||||
<attribute>
|
||||
<description>
|
||||
A comma separated list of roles, one of which the user must
|
||||
possess for the body to be output. Deprecated in favour of the access expression.
|
||||
</description>
|
||||
<name>ifAnyGranted</name>
|
||||
<required>false</required>
|
||||
<rtexprvalue>true</rtexprvalue>
|
||||
</attribute>
|
||||
</tag>
|
||||
|
||||
<tag>
|
||||
|
||||
Reference in New Issue
Block a user