SEC-1012: Adding generics and general tidying up of tests etc
This commit is contained in:
@@ -63,5 +63,5 @@ public interface AccessDecisionManager {
|
||||
*
|
||||
* @return <code>true</code> if the implementation can process the indicated class
|
||||
*/
|
||||
boolean supports(Class clazz);
|
||||
boolean supports(Class<?> clazz);
|
||||
}
|
||||
|
||||
@@ -87,5 +87,5 @@ public interface AfterInvocationManager {
|
||||
*
|
||||
* @return <code>true</code> if the implementation can process the indicated class
|
||||
*/
|
||||
boolean supports(Class clazz);
|
||||
boolean supports(Class<?> clazz);
|
||||
}
|
||||
|
||||
@@ -56,5 +56,5 @@ public interface AfterInvocationProvider {
|
||||
*
|
||||
* @return true if the implementation can process the indicated class
|
||||
*/
|
||||
boolean supports(Class<? extends Object> clazz);
|
||||
boolean supports(Class<?> clazz);
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ public class AfterInvocationProviderManager implements AfterInvocationManager, I
|
||||
* @return if the <code>AfterInvocationProviderManager</code> can support the secure object class, which requires
|
||||
* every one of its <code>AfterInvocationProvider</code>s to support the secure object class
|
||||
*/
|
||||
public boolean supports(Class clazz) {
|
||||
public boolean supports(Class<?> clazz) {
|
||||
Iterator iter = this.providers.iterator();
|
||||
|
||||
while (iter.hasNext()) {
|
||||
|
||||
@@ -2,10 +2,11 @@ package org.springframework.security.authoritymapping;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
@@ -14,7 +15,6 @@ import org.springframework.security.GrantedAuthorityImpl;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
||||
/**
|
||||
* This class implements the Attributes2GrantedAuthoritiesMapper and
|
||||
* MappableAttributesRetriever interfaces based on the supplied Map.
|
||||
@@ -27,7 +27,7 @@ import org.springframework.util.StringUtils;
|
||||
public class MapBasedAttributes2GrantedAuthoritiesMapper implements Attributes2GrantedAuthoritiesMapper, MappableAttributesRetriever, InitializingBean {
|
||||
private Map<String, Collection<GrantedAuthority>> attributes2grantedAuthoritiesMap = null;
|
||||
private String stringSeparator = ",";
|
||||
private String[] mappableAttributes = null;
|
||||
private Set<String> mappableAttributes = null;
|
||||
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
@@ -51,21 +51,17 @@ public class MapBasedAttributes2GrantedAuthoritiesMapper implements Attributes2G
|
||||
/**
|
||||
* @return Returns the attributes2grantedAuthoritiesMap.
|
||||
*/
|
||||
public Map getAttributes2grantedAuthoritiesMap() {
|
||||
public Map<String, Collection<GrantedAuthority>> getAttributes2grantedAuthoritiesMap() {
|
||||
return attributes2grantedAuthoritiesMap;
|
||||
}
|
||||
/**
|
||||
* @param attributes2grantedAuthoritiesMap The attributes2grantedAuthoritiesMap to set.
|
||||
*/
|
||||
public void setAttributes2grantedAuthoritiesMap(final Map<String, Object> attributes2grantedAuthoritiesMap) {
|
||||
public void setAttributes2grantedAuthoritiesMap(final Map attributes2grantedAuthoritiesMap) {
|
||||
Assert.notEmpty(attributes2grantedAuthoritiesMap,"A non-empty attributes2grantedAuthoritiesMap must be supplied");
|
||||
this.attributes2grantedAuthoritiesMap = preProcessMap(attributes2grantedAuthoritiesMap);
|
||||
|
||||
try {
|
||||
mappableAttributes = (String[])this.attributes2grantedAuthoritiesMap.keySet().toArray(new String[]{});
|
||||
} catch ( ArrayStoreException ase ) {
|
||||
throw new IllegalArgumentException("attributes2grantedAuthoritiesMap contains non-String objects as keys");
|
||||
}
|
||||
mappableAttributes = Collections.unmodifiableSet(this.attributes2grantedAuthoritiesMap.keySet());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,11 +70,14 @@ public class MapBasedAttributes2GrantedAuthoritiesMapper implements Attributes2G
|
||||
* @param orgMap The map to process
|
||||
* @return the processed Map
|
||||
*/
|
||||
private Map<String, Collection<GrantedAuthority>> preProcessMap(Map<String, Object> orgMap) {
|
||||
Map result = new HashMap(orgMap.size());
|
||||
private Map<String, Collection<GrantedAuthority>> preProcessMap(Map<?, ?> orgMap) {
|
||||
Map<String, Collection<GrantedAuthority>> result =
|
||||
new HashMap<String, Collection<GrantedAuthority>>(orgMap.size());
|
||||
|
||||
for(Map.Entry entry : orgMap.entrySet()) {
|
||||
result.put(entry.getKey(),getGrantedAuthorityCollection(entry.getValue()));
|
||||
for(Map.Entry<?,?> entry : orgMap.entrySet()) {
|
||||
Assert.isInstanceOf(String.class, entry.getKey(),
|
||||
"attributes2grantedAuthoritiesMap contains non-String objects as keys");
|
||||
result.put((String)entry.getKey(),getGrantedAuthorityCollection(entry.getValue()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -90,8 +89,8 @@ public class MapBasedAttributes2GrantedAuthoritiesMapper implements Attributes2G
|
||||
* The value to convert to a GrantedAuthority Collection
|
||||
* @return Collection containing the GrantedAuthority Collection
|
||||
*/
|
||||
private Collection getGrantedAuthorityCollection(Object value) {
|
||||
Collection result = new ArrayList();
|
||||
private Collection<GrantedAuthority> getGrantedAuthorityCollection(Object value) {
|
||||
Collection<GrantedAuthority> result = new ArrayList<GrantedAuthority>();
|
||||
addGrantedAuthorityCollection(result,value);
|
||||
return result;
|
||||
}
|
||||
@@ -109,7 +108,7 @@ public class MapBasedAttributes2GrantedAuthoritiesMapper implements Attributes2G
|
||||
return;
|
||||
}
|
||||
if ( value instanceof Collection ) {
|
||||
addGrantedAuthorityCollection(result,(Collection)value);
|
||||
addGrantedAuthorityCollection(result,(Collection<?>)value);
|
||||
} else if ( value instanceof Object[] ) {
|
||||
addGrantedAuthorityCollection(result,(Object[])value);
|
||||
} else if ( value instanceof String ) {
|
||||
@@ -121,10 +120,9 @@ public class MapBasedAttributes2GrantedAuthoritiesMapper implements Attributes2G
|
||||
}
|
||||
}
|
||||
|
||||
private void addGrantedAuthorityCollection(Collection<GrantedAuthority> result, Collection value) {
|
||||
Iterator it = value.iterator();
|
||||
while ( it.hasNext() ) {
|
||||
addGrantedAuthorityCollection(result,it.next());
|
||||
private void addGrantedAuthorityCollection(Collection<GrantedAuthority> result, Collection<?> value) {
|
||||
for(Object elt : value) {
|
||||
addGrantedAuthorityCollection(result, elt);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,7 +146,7 @@ public class MapBasedAttributes2GrantedAuthoritiesMapper implements Attributes2G
|
||||
*
|
||||
* @see org.springframework.security.authoritymapping.MappableAttributesRetriever#getMappableAttributes()
|
||||
*/
|
||||
public String[] getMappableAttributes() {
|
||||
public Set<String> getMappableAttributes() {
|
||||
return mappableAttributes;
|
||||
}
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.springframework.security.authoritymapping;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Interface to be implemented by classes that can retrieve a list of mappable
|
||||
* security attribute strings (for example the list of all available J2EE roles in a web or EJB
|
||||
@@ -10,10 +12,10 @@ package org.springframework.security.authoritymapping;
|
||||
*/
|
||||
public interface MappableAttributesRetriever {
|
||||
/**
|
||||
* Implementations of this method should return a list of all string attributes which
|
||||
* Implementations of this method should return a set of all string attributes which
|
||||
* can be mapped to <tt>GrantedAuthority</tt>s.
|
||||
*
|
||||
* @return list of all mappable roles
|
||||
* @return set of all mappable roles
|
||||
*/
|
||||
String[] getMappableAttributes();
|
||||
Set<String> getMappableAttributes();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package org.springframework.security.authoritymapping;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* This class implements the MappableAttributesRetriever interface by just returning
|
||||
@@ -11,23 +14,21 @@ import org.springframework.util.Assert;
|
||||
* @since 2.0
|
||||
*/
|
||||
public class SimpleMappableAttributesRetriever implements MappableAttributesRetriever {
|
||||
private String[] mappableAttributes = null;
|
||||
private Set<String> mappableAttributes = null;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.security.authoritymapping.MappableAttributesRetriever#getMappableAttributes()
|
||||
*/
|
||||
public String[] getMappableAttributes() {
|
||||
Assert.notNull(mappableAttributes, "No mappable roles have been set");
|
||||
String[] copy = new String[mappableAttributes.length];
|
||||
System.arraycopy(mappableAttributes, 0, copy, 0, copy.length);
|
||||
return copy;
|
||||
public Set<String> getMappableAttributes() {
|
||||
return mappableAttributes;
|
||||
}
|
||||
|
||||
public void setMappableAttributes(String[] aMappableRoles) {
|
||||
this.mappableAttributes = new String[aMappableRoles.length];
|
||||
System.arraycopy(aMappableRoles, 0, mappableAttributes, 0, mappableAttributes.length);
|
||||
mappableAttributes = new HashSet<String>(aMappableRoles.length);
|
||||
mappableAttributes.addAll(Arrays.asList(aMappableRoles));
|
||||
mappableAttributes = Collections.unmodifiableSet(mappableAttributes);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,7 +5,10 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.StringReader;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
@@ -41,7 +44,7 @@ import org.xml.sax.SAXException;
|
||||
public abstract class XmlMappableAttributesRetriever implements MappableAttributesRetriever, InitializingBean {
|
||||
private static final Log logger = LogFactory.getLog(XmlMappableAttributesRetriever.class);
|
||||
|
||||
private String[] mappableAttributes = null;
|
||||
private Set<String> mappableAttributes = null;
|
||||
|
||||
private InputStream xmlInputStream = null;
|
||||
|
||||
@@ -55,27 +58,25 @@ public abstract class XmlMappableAttributesRetriever implements MappableAttribut
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(xmlInputStream, "An XML InputStream must be set");
|
||||
Assert.notNull(xpathExpression, "An XPath expression must be set");
|
||||
mappableAttributes = getMappableAttributes(xmlInputStream);
|
||||
mappableAttributes = Collections.unmodifiableSet(getMappableAttributes(xmlInputStream));
|
||||
}
|
||||
|
||||
public String[] getMappableAttributes() {
|
||||
String[] copy = new String[mappableAttributes.length];
|
||||
System.arraycopy(mappableAttributes, 0, copy, 0, copy.length);
|
||||
return copy;
|
||||
public Set<String> getMappableAttributes() {
|
||||
return mappableAttributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mappable roles from the specified XML document.
|
||||
*/
|
||||
private String[] getMappableAttributes(InputStream aStream) {
|
||||
private Set<String> getMappableAttributes(InputStream aStream) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Reading mappable attributes from XML document");
|
||||
}
|
||||
try {
|
||||
Document doc = getDocument(aStream);
|
||||
String[] roles = getMappableAttributes(doc);
|
||||
Set<String> roles = getMappableAttributes(doc);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Mappable attributes from XML document: " + Arrays.asList(roles));
|
||||
logger.debug("Mappable attributes from XML document: " + roles);
|
||||
}
|
||||
return roles;
|
||||
} finally {
|
||||
@@ -118,13 +119,14 @@ public abstract class XmlMappableAttributesRetriever implements MappableAttribut
|
||||
* @return String[] the list of roles.
|
||||
* @throws JaxenException
|
||||
*/
|
||||
private String[] getMappableAttributes(Document doc) {
|
||||
private Set<String> getMappableAttributes(Document doc) {
|
||||
try {
|
||||
DOMXPath xpath = new DOMXPath(xpathExpression);
|
||||
List roleElements = xpath.selectNodes(doc);
|
||||
String[] roles = new String[roleElements.size()];
|
||||
for (int i = 0; i < roles.length; i++) {
|
||||
roles[i] = ((Node) roleElements.get(i)).getNodeValue();
|
||||
List<Node> roleElements = xpath.selectNodes(doc);
|
||||
Set<String> roles = new HashSet<String>(roleElements.size());
|
||||
|
||||
for (Node n : roleElements) {
|
||||
roles.add(n.getNodeValue());
|
||||
}
|
||||
return roles;
|
||||
} catch (JaxenException e) {
|
||||
|
||||
@@ -43,6 +43,7 @@ public class FilterChainProxyPostProcessor implements BeanPostProcessor, BeanFac
|
||||
|
||||
private ListableBeanFactory beanFactory;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
if(!BeanIds.FILTER_CHAIN_PROXY.equals(beanName)) {
|
||||
return bean;
|
||||
@@ -51,7 +52,7 @@ public class FilterChainProxyPostProcessor implements BeanPostProcessor, BeanFac
|
||||
FilterChainProxy filterChainProxy = (FilterChainProxy) bean;
|
||||
FilterChainList filterList = (FilterChainList) beanFactory.getBean(BeanIds.FILTER_LIST);
|
||||
|
||||
List filters = new ArrayList(filterList.getFilters());
|
||||
List<Filter> filters = new ArrayList<Filter>(filterList.getFilters());
|
||||
Collections.sort(filters, new OrderComparator());
|
||||
|
||||
logger.info("Checking sorted filter chain: " + filters);
|
||||
@@ -82,7 +83,7 @@ public class FilterChainProxyPostProcessor implements BeanPostProcessor, BeanFac
|
||||
checkFilterStack(filters);
|
||||
|
||||
// Note that this returns a copy
|
||||
Map filterMap = filterChainProxy.getFilterChainMap();
|
||||
Map<String, List<Filter>> filterMap = filterChainProxy.getFilterChainMap();
|
||||
filterMap.put(filterChainProxy.getMatcher().getUniversalMatchPattern(), filters);
|
||||
filterChainProxy.setFilterChainMap(filterMap);
|
||||
|
||||
|
||||
@@ -18,16 +18,16 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* Registered by the <tt>AuthenticationManagerBeanDefinitionParser</tt> if an external
|
||||
* ConcurrentSessionController is set (and hence an external SessionRegistry).
|
||||
* Registered by the <tt>AuthenticationManagerBeanDefinitionParser</tt> if an external
|
||||
* ConcurrentSessionController is set (and hence an external SessionRegistry).
|
||||
* Its responsibility is to set the SessionRegistry on namespace-registered beans which require access
|
||||
* to it.
|
||||
* <p>
|
||||
* It will attempt to read the registry directly from the registered controller. If that fails, it will look in
|
||||
* the application context for a registered SessionRegistry bean.
|
||||
*
|
||||
* See SEC-879.
|
||||
*
|
||||
*
|
||||
* See SEC-879.
|
||||
*
|
||||
* @author Luke Taylor
|
||||
* @since 2.0.3
|
||||
*/
|
||||
@@ -38,57 +38,57 @@ class SessionRegistryInjectionBeanPostProcessor implements BeanPostProcessor, Be
|
||||
private final String controllerBeanName;
|
||||
|
||||
SessionRegistryInjectionBeanPostProcessor(String controllerBeanName) {
|
||||
this.controllerBeanName = controllerBeanName;
|
||||
this.controllerBeanName = controllerBeanName;
|
||||
}
|
||||
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
if (BeanIds.FORM_LOGIN_FILTER.equals(beanName) ||
|
||||
BeanIds.OPEN_ID_FILTER.equals(beanName)) {
|
||||
((AbstractProcessingFilter) bean).setSessionRegistry(getSessionRegistry());
|
||||
} else if (BeanIds.SESSION_FIXATION_PROTECTION_FILTER.equals(beanName)) {
|
||||
((SessionFixationProtectionFilter)bean).setSessionRegistry(getSessionRegistry());
|
||||
}
|
||||
|
||||
return bean;
|
||||
}
|
||||
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
|
||||
private SessionRegistry getSessionRegistry() {
|
||||
if (sessionRegistry != null) {
|
||||
return sessionRegistry;
|
||||
}
|
||||
|
||||
logger.info("Attempting to read SessionRegistry from registered ConcurrentSessionController bean");
|
||||
|
||||
ConcurrentSessionController controller = (ConcurrentSessionController) beanFactory.getBean(controllerBeanName);
|
||||
|
||||
if (controller instanceof ConcurrentSessionControllerImpl) {
|
||||
sessionRegistry = ((ConcurrentSessionControllerImpl)controller).getSessionRegistry();
|
||||
|
||||
return sessionRegistry;
|
||||
}
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
if (BeanIds.FORM_LOGIN_FILTER.equals(beanName) ||
|
||||
BeanIds.OPEN_ID_FILTER.equals(beanName)) {
|
||||
((AbstractProcessingFilter) bean).setSessionRegistry(getSessionRegistry());
|
||||
} else if (BeanIds.SESSION_FIXATION_PROTECTION_FILTER.equals(beanName)) {
|
||||
((SessionFixationProtectionFilter)bean).setSessionRegistry(getSessionRegistry());
|
||||
}
|
||||
|
||||
logger.info("ConcurrentSessionController is not a standard implementation. SessionRegistry could not be read from it. Looking for it in the context.");
|
||||
|
||||
List sessionRegs = new ArrayList(beanFactory.getBeansOfType(SessionRegistry.class).values());
|
||||
|
||||
if (sessionRegs.size() == 0) {
|
||||
throw new SecurityConfigurationException("concurrent-session-controller-ref was set but no SessionRegistry could be obtained from the application context.");
|
||||
}
|
||||
|
||||
if (sessionRegs.size() > 1) {
|
||||
logger.warn("More than one SessionRegistry instance in application context. Possible configuration errors may result.");
|
||||
}
|
||||
|
||||
sessionRegistry = (SessionRegistry) sessionRegs.get(0);
|
||||
|
||||
return sessionRegistry;
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
this.beanFactory = (ListableBeanFactory) beanFactory;
|
||||
}
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
|
||||
private SessionRegistry getSessionRegistry() {
|
||||
if (sessionRegistry != null) {
|
||||
return sessionRegistry;
|
||||
}
|
||||
|
||||
logger.info("Attempting to read SessionRegistry from registered ConcurrentSessionController bean");
|
||||
|
||||
ConcurrentSessionController controller = (ConcurrentSessionController) beanFactory.getBean(controllerBeanName);
|
||||
|
||||
if (controller instanceof ConcurrentSessionControllerImpl) {
|
||||
sessionRegistry = ((ConcurrentSessionControllerImpl)controller).getSessionRegistry();
|
||||
|
||||
return sessionRegistry;
|
||||
}
|
||||
|
||||
logger.info("ConcurrentSessionController is not a standard implementation. SessionRegistry could not be read from it. Looking for it in the context.");
|
||||
|
||||
List<SessionRegistry> sessionRegs = new ArrayList<SessionRegistry>(beanFactory.getBeansOfType(SessionRegistry.class).values());
|
||||
|
||||
if (sessionRegs.size() == 0) {
|
||||
throw new SecurityConfigurationException("concurrent-session-controller-ref was set but no SessionRegistry could be obtained from the application context.");
|
||||
}
|
||||
|
||||
if (sessionRegs.size() > 1) {
|
||||
logger.warn("More than one SessionRegistry instance in application context. Possible configuration errors may result.");
|
||||
}
|
||||
|
||||
sessionRegistry = (SessionRegistry) sessionRegs.get(0);
|
||||
|
||||
return sessionRegistry;
|
||||
}
|
||||
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
this.beanFactory = (ListableBeanFactory) beanFactory;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ public class MethodExpressionAfterInvocationProvider implements AfterInvocationP
|
||||
return attribute instanceof PostInvocationExpressionAttribute;
|
||||
}
|
||||
|
||||
public boolean supports(Class<? extends Object> clazz) {
|
||||
public boolean supports(Class<?> clazz) {
|
||||
return clazz.isAssignableFrom(MethodInvocation.class);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,68 +14,68 @@ import org.springframework.util.ReflectionUtils;
|
||||
* Any object that accepts an <code>Object</code> as its sole constructor can
|
||||
* be used instead of this default.
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
* @author Ruud Senden
|
||||
* @since 2.0
|
||||
*/
|
||||
public class AuthenticationDetailsSourceImpl implements AuthenticationDetailsSource {
|
||||
//~ Instance fields ================================================================================================
|
||||
//~ Instance fields ================================================================================================
|
||||
|
||||
private Class clazz = AuthenticationDetails.class;
|
||||
|
||||
//~ Methods ========================================================================================================
|
||||
private Class<?> clazz = AuthenticationDetails.class;
|
||||
|
||||
public Object buildDetails(Object context) {
|
||||
try {
|
||||
Constructor constructor = getFirstMatchingConstructor(context);
|
||||
return constructor.newInstance(new Object[] { context });
|
||||
} catch (NoSuchMethodException ex) {
|
||||
ReflectionUtils.handleReflectionException(ex);
|
||||
} catch (InvocationTargetException ex) {
|
||||
ReflectionUtils.handleReflectionException(ex);
|
||||
} catch (InstantiationException ex) {
|
||||
ReflectionUtils.handleReflectionException(ex);
|
||||
} catch (IllegalAccessException ex) {
|
||||
ReflectionUtils.handleReflectionException(ex);
|
||||
}
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
return null;
|
||||
}
|
||||
public Object buildDetails(Object context) {
|
||||
try {
|
||||
Constructor<?> constructor = getFirstMatchingConstructor(context);
|
||||
return constructor.newInstance(new Object[] { context });
|
||||
} catch (NoSuchMethodException ex) {
|
||||
ReflectionUtils.handleReflectionException(ex);
|
||||
} catch (InvocationTargetException ex) {
|
||||
ReflectionUtils.handleReflectionException(ex);
|
||||
} catch (InstantiationException ex) {
|
||||
ReflectionUtils.handleReflectionException(ex);
|
||||
} catch (IllegalAccessException ex) {
|
||||
ReflectionUtils.handleReflectionException(ex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first matching constructor that can take the given object
|
||||
* as an argument. Please note that we cannot use
|
||||
* getDeclaredConstructor(new Class[]{object.getClass()})
|
||||
* as this will only match if the constructor argument type matches
|
||||
* the object type exactly (instead of checking whether it is assignable)
|
||||
*
|
||||
* @param object the object for which to find a matching constructor
|
||||
* @return a matching constructor for the given object
|
||||
* @throws NoSuchMethodException if no matching constructor can be found
|
||||
*/
|
||||
private Constructor getFirstMatchingConstructor(Object object) throws NoSuchMethodException {
|
||||
Constructor[] constructors = clazz.getDeclaredConstructors();
|
||||
Constructor constructor = null;
|
||||
for (int i = 0; i < constructors.length; i++) {
|
||||
Class[] parameterTypes = constructors[i].getParameterTypes();
|
||||
if (parameterTypes.length == 1 && (object == null || parameterTypes[0].isInstance(object))) {
|
||||
constructor = constructors[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if (constructor == null) {
|
||||
if (object == null) {
|
||||
throw new NoSuchMethodException("No constructor found that can take a single argument");
|
||||
} else {
|
||||
throw new NoSuchMethodException("No constructor found that can take a single argument of type " + object.getClass());
|
||||
}
|
||||
}
|
||||
return constructor;
|
||||
}
|
||||
/**
|
||||
* Return the first matching constructor that can take the given object
|
||||
* as an argument. Please note that we cannot use
|
||||
* getDeclaredConstructor(new Class[]{object.getClass()})
|
||||
* as this will only match if the constructor argument type matches
|
||||
* the object type exactly (instead of checking whether it is assignable)
|
||||
*
|
||||
* @param object the object for which to find a matching constructor
|
||||
* @return a matching constructor for the given object
|
||||
* @throws NoSuchMethodException if no matching constructor can be found
|
||||
*/
|
||||
private Constructor<?> getFirstMatchingConstructor(Object object) throws NoSuchMethodException {
|
||||
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
|
||||
Constructor<?> constructor = null;
|
||||
for (int i = 0; i < constructors.length; i++) {
|
||||
Class<?>[] parameterTypes = constructors[i].getParameterTypes();
|
||||
if (parameterTypes.length == 1 && (object == null || parameterTypes[0].isInstance(object))) {
|
||||
constructor = constructors[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void setClazz(Class clazz) {
|
||||
Assert.notNull(clazz, "Class required");
|
||||
this.clazz = clazz;
|
||||
}
|
||||
if (constructor == null) {
|
||||
if (object == null) {
|
||||
throw new NoSuchMethodException("No constructor found that can take a single argument");
|
||||
} else {
|
||||
throw new NoSuchMethodException("No constructor found that can take a single argument of type " + object.getClass());
|
||||
}
|
||||
}
|
||||
return constructor;
|
||||
}
|
||||
|
||||
public void setClazz(Class<?> clazz) {
|
||||
Assert.notNull(clazz, "Class required");
|
||||
this.clazz = clazz;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||
* Implementation of {@link AuthenticationDetailsSource} which builds the details object from
|
||||
* an <tt>HttpServletRequest</tt> object.
|
||||
* <p>
|
||||
* By default will create an instance of <code>WebAuthenticationDetails</code>. Any object that accepts a
|
||||
* By default will create an instance of <code>WebAuthenticationDetails</code>. Any object that accepts a
|
||||
* <code>HttpServletRequest</code> as its sole constructor can be used instead of this default.
|
||||
*
|
||||
* @author Ben Alex
|
||||
@@ -37,7 +37,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||
public class WebAuthenticationDetailsSource implements AuthenticationDetailsSource {
|
||||
//~ Instance fields ================================================================================================
|
||||
|
||||
private Class clazz = WebAuthenticationDetails.class;
|
||||
private Class<?> clazz = WebAuthenticationDetails.class;
|
||||
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
@@ -47,7 +47,7 @@ public class WebAuthenticationDetailsSource implements AuthenticationDetailsSour
|
||||
public Object buildDetails(Object context) {
|
||||
Assert.isInstanceOf(HttpServletRequest.class, context);
|
||||
try {
|
||||
Constructor constructor = clazz.getConstructor(new Class[] {HttpServletRequest.class});
|
||||
Constructor<?> constructor = clazz.getConstructor(new Class[] {HttpServletRequest.class});
|
||||
|
||||
return constructor.newInstance(new Object[] {context});
|
||||
} catch (NoSuchMethodException ex) {
|
||||
@@ -63,7 +63,7 @@ public class WebAuthenticationDetailsSource implements AuthenticationDetailsSour
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setClazz(Class clazz) {
|
||||
public void setClazz(Class<?> clazz) {
|
||||
Assert.notNull(clazz, "Class required");
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package org.springframework.security.ui.preauth.j2ee;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@@ -26,7 +26,7 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
public abstract class AbstractPreAuthenticatedAuthenticationDetailsSource extends AuthenticationDetailsSourceImpl {
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
protected String[] j2eeMappableRoles;
|
||||
protected Set<String> j2eeMappableRoles;
|
||||
protected Attributes2GrantedAuthoritiesMapper j2eeUserRoles2GrantedAuthoritiesMapper =
|
||||
new SimpleAttributes2GrantedAuthoritiesMapper();
|
||||
|
||||
@@ -72,7 +72,7 @@ public abstract class AbstractPreAuthenticatedAuthenticationDetailsSource extend
|
||||
* @param mappableRoles the possible roles as determined by the MappableAttributesRetriever
|
||||
* @return the subset of mappable roles which the current user has.
|
||||
*/
|
||||
protected abstract Collection<String> getUserRoles(Object context, String[] mappableRoles);
|
||||
protected abstract Collection<String> getUserRoles(Object context, Set<String> mappableRoles);
|
||||
|
||||
/**
|
||||
* @param aJ2eeMappableRolesRetriever
|
||||
|
||||
@@ -5,6 +5,7 @@ import org.springframework.security.authoritymapping.SimpleAttributes2GrantedAut
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@@ -36,12 +37,12 @@ public class J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource extends Abs
|
||||
* returned by the MappableAttributesRetriever.
|
||||
* @return GrantedAuthority[] mapped from the user's J2EE roles.
|
||||
*/
|
||||
protected Collection<String> getUserRoles(Object context, String[] mappableRoles) {
|
||||
protected Collection<String> getUserRoles(Object context, Set<String> mappableRoles) {
|
||||
ArrayList<String> j2eeUserRolesList = new ArrayList<String>();
|
||||
|
||||
for (int i = 0; i < mappableRoles.length; i++) {
|
||||
if (((HttpServletRequest)context).isUserInRole(mappableRoles[i])) {
|
||||
j2eeUserRolesList.add(mappableRoles[i]);
|
||||
for (String role : mappableRoles) {
|
||||
if (((HttpServletRequest)context).isUserInRole(role)) {
|
||||
j2eeUserRolesList.add(role);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,5 +28,5 @@ public interface SwitchUserAuthorityChanger {
|
||||
*
|
||||
* @return the modified list of granted authorities.
|
||||
*/
|
||||
List modifyGrantedAuthorities(UserDetails targetUser, Authentication currentAuthentication, List authoritiesToBeGranted);
|
||||
List<GrantedAuthority> modifyGrantedAuthorities(UserDetails targetUser, Authentication currentAuthentication, List<GrantedAuthority> authoritiesToBeGranted);
|
||||
}
|
||||
|
||||
@@ -33,14 +33,13 @@ import java.util.Vector;
|
||||
public class UserAttribute {
|
||||
//~ Instance fields ================================================================================================
|
||||
|
||||
private List authorities = new Vector();
|
||||
private List<GrantedAuthority> authorities = new Vector<GrantedAuthority>();
|
||||
private String password;
|
||||
private boolean enabled = true;
|
||||
|
||||
//~ Constructors ===================================================================================================
|
||||
|
||||
public UserAttribute() {
|
||||
super();
|
||||
}
|
||||
|
||||
//~ Methods ========================================================================================================
|
||||
@@ -52,7 +51,7 @@ public class UserAttribute {
|
||||
public GrantedAuthority[] getAuthorities() {
|
||||
GrantedAuthority[] toReturn = {new GrantedAuthorityImpl("demo")};
|
||||
|
||||
return (GrantedAuthority[]) this.authorities.toArray(toReturn);
|
||||
return this.authorities.toArray(toReturn);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,7 +60,7 @@ public class UserAttribute {
|
||||
* @param authorities {@link List} <{@link GrantedAuthority}>
|
||||
* @since 1.1
|
||||
*/
|
||||
public void setAuthorities(List authorities) {
|
||||
public void setAuthorities(List<GrantedAuthority> authorities) {
|
||||
this.authorities = authorities;
|
||||
}
|
||||
|
||||
@@ -69,15 +68,13 @@ public class UserAttribute {
|
||||
* Set all authorities for this user from String values.
|
||||
* It will create the necessary {@link GrantedAuthority} objects.
|
||||
*
|
||||
* @param authoritiesAsString {@link List} <{@link String}>
|
||||
* @param authoritiesAsStrings {@link List} <{@link String}>
|
||||
* @since 1.1
|
||||
*/
|
||||
public void setAuthoritiesAsString(List authoritiesAsString) {
|
||||
setAuthorities(new ArrayList(authoritiesAsString.size()));
|
||||
Iterator it = authoritiesAsString.iterator();
|
||||
while (it.hasNext()) {
|
||||
GrantedAuthority grantedAuthority = new GrantedAuthorityImpl((String) it.next());
|
||||
addAuthority(grantedAuthority);
|
||||
public void setAuthoritiesAsString(List<String> authoritiesAsStrings) {
|
||||
setAuthorities(new ArrayList<GrantedAuthority>(authoritiesAsStrings.size()));
|
||||
for(String authority : authoritiesAsStrings) {
|
||||
addAuthority(new GrantedAuthorityImpl(authority));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ public class UserAttributeEditor extends PropertyEditorSupport {
|
||||
String[] tokens = StringUtils.commaDelimitedListToStringArray(s);
|
||||
UserAttribute userAttrib = new UserAttribute();
|
||||
|
||||
List authoritiesAsString = new ArrayList();
|
||||
List<String> authoritiesAsStrings = new ArrayList<String>();
|
||||
|
||||
for (int i = 0; i < tokens.length; i++) {
|
||||
String currentToken = tokens[i].trim();
|
||||
@@ -48,11 +48,11 @@ public class UserAttributeEditor extends PropertyEditorSupport {
|
||||
} else if (currentToken.toLowerCase().equals("disabled")) {
|
||||
userAttrib.setEnabled(false);
|
||||
} else {
|
||||
authoritiesAsString.add(currentToken);
|
||||
authoritiesAsStrings.add(currentToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
userAttrib.setAuthoritiesAsString(authoritiesAsString);
|
||||
userAttrib.setAuthoritiesAsString(authoritiesAsStrings);
|
||||
|
||||
if (userAttrib.isValid()) {
|
||||
setValue(userAttrib);
|
||||
|
||||
@@ -27,7 +27,8 @@ import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Static utility methods for creating <code>MethodInvocation</code>s usable within Spring Security.
|
||||
* <p>All methods of this class return a {@link org.springframework.security.util.SimpleMethodInvocation}.</p>
|
||||
* <p>
|
||||
* All methods of this class return a {@link org.springframework.security.util.SimpleMethodInvocation}.
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
@@ -40,58 +41,46 @@ public final class MethodInvocationUtils {
|
||||
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
/**
|
||||
* Generates a <code>MethodInvocation</code> for specified <code>methodName</code> on the passed object.
|
||||
*
|
||||
* @param object the object that will be used to find the relevant <code>Method</code>
|
||||
* @param methodName the name of the method to find
|
||||
*
|
||||
* @return a <code>MethodInvocation</code>, or <code>null</code> if there was a problem
|
||||
*/
|
||||
public static MethodInvocation create(Object object, String methodName) {
|
||||
return create(object, methodName, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a <code>MethodInvocation</code> for specified <code>methodName</code> on the passed object,
|
||||
* using the <code>args</code> to locate the method.
|
||||
*
|
||||
* @param object the object that will be used to find the relevant <code>Method</code>
|
||||
* @param methodName the name of the method to find
|
||||
* @param args arguments that are required as part of the method signature
|
||||
* @param args arguments that are required as part of the method signature (can be empty)
|
||||
*
|
||||
* @return a <code>MethodInvocation</code>, or <code>null</code> if there was a problem
|
||||
*/
|
||||
public static MethodInvocation create(Object object, String methodName, Object[] args) {
|
||||
public static MethodInvocation create(Object object, String methodName, Object... args) {
|
||||
Assert.notNull(object, "Object required");
|
||||
|
||||
Class[] classArgs = null;
|
||||
Class<?>[] classArgs = null;
|
||||
|
||||
if (args != null) {
|
||||
List list = new ArrayList();
|
||||
List<Class<?>> list = new ArrayList<Class<?>>();
|
||||
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
list.add(args[i].getClass());
|
||||
}
|
||||
|
||||
classArgs = (Class[]) list.toArray(new Class[] {});
|
||||
classArgs = list.toArray(new Class[] {});
|
||||
}
|
||||
|
||||
|
||||
// Determine the type that declares the requested method, taking into account proxies
|
||||
Class target = AopUtils.getTargetClass(object);
|
||||
Class<?> target = AopUtils.getTargetClass(object);
|
||||
if (object instanceof Advised) {
|
||||
Advised a = (Advised) object;
|
||||
if (!a.isProxyTargetClass()) {
|
||||
Class[] possibleInterfaces = a.getProxiedInterfaces();
|
||||
for (int i = 0; i < possibleInterfaces.length; i++) {
|
||||
try {
|
||||
possibleInterfaces[i].getMethod(methodName, classArgs);
|
||||
// to get here means no exception happened
|
||||
target = possibleInterfaces[i];
|
||||
break;
|
||||
} catch (Exception tryTheNextOne) {}
|
||||
}
|
||||
}
|
||||
Advised a = (Advised) object;
|
||||
if (!a.isProxyTargetClass()) {
|
||||
Class<?>[] possibleInterfaces = a.getProxiedInterfaces();
|
||||
for (int i = 0; i < possibleInterfaces.length; i++) {
|
||||
try {
|
||||
possibleInterfaces[i].getMethod(methodName, classArgs);
|
||||
// to get here means no exception happened
|
||||
target = possibleInterfaces[i];
|
||||
break;
|
||||
} catch (Exception tryTheNextOne) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return createFromClass(object, target, methodName, classArgs, args);
|
||||
@@ -105,7 +94,7 @@ public final class MethodInvocationUtils {
|
||||
*
|
||||
* @return a <code>MethodInvocation</code>, or <code>null</code> if there was a problem
|
||||
*/
|
||||
public static MethodInvocation createFromClass(Class clazz, String methodName) {
|
||||
public static MethodInvocation createFromClass(Class<?> clazz, String methodName) {
|
||||
return createFromClass(null, clazz, methodName, null, null);
|
||||
}
|
||||
|
||||
@@ -120,8 +109,8 @@ public final class MethodInvocationUtils {
|
||||
* @param args the actual arguments that should be passed to SimpleMethodInvocation
|
||||
* @return a <code>MethodInvocation</code>, or <code>null</code> if there was a problem
|
||||
*/
|
||||
public static MethodInvocation createFromClass(Object targetObject, Class clazz, String methodName, Class[] classArgs, Object[] args) {
|
||||
Assert.notNull(clazz, "Class required");
|
||||
public static MethodInvocation createFromClass(Object targetObject, Class<?> clazz, String methodName, Class<?>[] classArgs, Object[] args) {
|
||||
Assert.notNull(clazz, "Class required");
|
||||
Assert.hasText(methodName, "MethodName required");
|
||||
|
||||
Method method;
|
||||
|
||||
@@ -113,7 +113,7 @@ public abstract class AbstractAccessDecisionManager implements AccessDecisionMan
|
||||
* @param clazz the type of secured object being presented
|
||||
* @return true if this type is supported
|
||||
*/
|
||||
public boolean supports(Class clazz) {
|
||||
public boolean supports(Class<?> clazz) {
|
||||
Iterator<AccessDecisionVoter> iter = this.decisionVoters.iterator();
|
||||
|
||||
while (iter.hasNext()) {
|
||||
|
||||
Reference in New Issue
Block a user