Significantly refactor "well-known location model" to authentication processing mechanism and HttpSessionContextIntegrationFilter model.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -29,7 +29,8 @@ import java.security.Principal;
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Stored in a request {@link net.sf.acegisecurity.context.SecureContext}.
|
||||
* Stored in a request {@link
|
||||
* net.sf.acegisecurity.context.security.SecureContext}.
|
||||
* </p>
|
||||
*
|
||||
* @author Ben Alex
|
||||
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -18,7 +18,7 @@ package net.sf.acegisecurity;
|
||||
/**
|
||||
* Thrown if an authentication request is rejected because there is no {@link
|
||||
* Authentication} object in the {@link
|
||||
* net.sf.acegisecurity.context.SecureContext}.
|
||||
* net.sf.acegisecurity.context.security.SecureContext}.
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -25,8 +25,8 @@ package net.sf.acegisecurity;
|
||||
* object invocation only. The {@link
|
||||
* net.sf.acegisecurity.intercept.AbstractSecurityInterceptor} will replace
|
||||
* the <code>Authentication</code> object held in the {@link
|
||||
* net.sf.acegisecurity.context.SecureContext} for the duration of the secure
|
||||
* object callback only, returning it to the original
|
||||
* net.sf.acegisecurity.context.security.SecureContext} for the duration of
|
||||
* the secure object callback only, returning it to the original
|
||||
* <code>Authentication</code> object when the callback ends.
|
||||
* </p>
|
||||
*
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -16,27 +16,47 @@
|
||||
package net.sf.acegisecurity.adapters;
|
||||
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.ui.AbstractIntegrationFilter;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContextUtils;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import java.security.Principal;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
|
||||
/**
|
||||
* Populates a {@link net.sf.acegisecurity.context.SecureContext} from the
|
||||
* container's <code>HttpServletRequest.getUserPrincipal()</code>.
|
||||
* Populates <code>ContextHolder</code> with the <code>Authentication</code>
|
||||
* obtained from the container's
|
||||
* <code>HttpServletRequest.getUserPrincipal()</code>.
|
||||
*
|
||||
* <p>
|
||||
* See {@link AbstractIntegrationFilter} for further information.
|
||||
* Used this filter with container adapters only.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* This filter <b>never</b> preserves the <code>Authentication</code> on the
|
||||
* <code>ContextHolder</code> - it is replaced every request.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* See {@link HttpSessionContextIntegrationFilter} for further information.
|
||||
* </p>
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
*/
|
||||
public class HttpRequestIntegrationFilter extends AbstractIntegrationFilter {
|
||||
public class HttpRequestIntegrationFilter implements Filter {
|
||||
//~ Static fields/initializers =============================================
|
||||
|
||||
private static final Log logger = LogFactory.getLog(HttpRequestIntegrationFilter.class);
|
||||
@@ -44,19 +64,47 @@ public class HttpRequestIntegrationFilter extends AbstractIntegrationFilter {
|
||||
//~ Methods ================================================================
|
||||
|
||||
/**
|
||||
* Not supported for this type of well-known location.
|
||||
*
|
||||
* @param request DOCUMENT ME!
|
||||
* @param authentication DOCUMENT ME!
|
||||
* Does nothing. We use IoC container lifecycle services instead.
|
||||
*/
|
||||
public void commitToContainer(ServletRequest request,
|
||||
Authentication authentication) {}
|
||||
public void destroy() {}
|
||||
|
||||
public void doFilter(ServletRequest request, ServletResponse response,
|
||||
FilterChain chain) throws IOException, ServletException {
|
||||
SecureContext sc = SecureContextUtils.getSecureContext();
|
||||
|
||||
public Object extractFromContainer(ServletRequest request) {
|
||||
if (request instanceof HttpServletRequest) {
|
||||
return ((HttpServletRequest) request).getUserPrincipal();
|
||||
Principal principal = ((HttpServletRequest) request)
|
||||
.getUserPrincipal();
|
||||
|
||||
if ((principal != null) && principal instanceof Authentication) {
|
||||
sc.setAuthentication((Authentication) principal);
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(
|
||||
"ContextHolder updated with Authentication from container: '"
|
||||
+ principal + "'");
|
||||
}
|
||||
} else {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(
|
||||
"ContextHolder not set with new Authentication as Principal was: '"
|
||||
+ principal + "'");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
throw new IllegalArgumentException(
|
||||
"Only HttpServletRequest is acceptable");
|
||||
}
|
||||
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing. We use IoC container lifecycle services instead.
|
||||
*
|
||||
* @param arg0 ignored
|
||||
*
|
||||
* @throws ServletException ignored
|
||||
*/
|
||||
public void init(FilterConfig arg0) throws ServletException {}
|
||||
}
|
||||
|
||||
+289
@@ -0,0 +1,289 @@
|
||||
/* Copyright 2004, 2005 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 net.sf.acegisecurity.context;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Populates the <code>ContextHolder</code> with information obtained from the
|
||||
* <code>HttpSession</code>.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* The <code>HttpSession</code> will be queried to retrieve the
|
||||
* <code>Context</code> that should be stored against the
|
||||
* <code>ContextHolder</code> for the duration of the web request. At the end
|
||||
* of the web request, any updates made to the <code>ContextHolder</code> will
|
||||
* be persisted back to the <code>HttpSession</code> by this filter.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* If a valid <code>Context</code> cannot be obtained from the
|
||||
* <code>HttpSession</code> for whatever reason, a fresh <code>Context</code>
|
||||
* will be created and used instead. The created object will be of the
|
||||
* instance defined by the {@link #setContext(Class)} method.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* No <code>HttpSession</code> will be created by this filter if one does not
|
||||
* already exist. If at the end of the web request the
|
||||
* <code>HttpSession</code> does not exist, a <code>HttpSession</code> will
|
||||
* <b>only</b> be created if the current contents of
|
||||
* <code>ContextHolder</code> are not {@link
|
||||
* java.lang.Object#equals(java.lang.Object)} to a <code>new</code> instance
|
||||
* of {@link #setContext(Class)}. This avoids needless
|
||||
* <code>HttpSession</code> creation, but automates the storage of changes
|
||||
* made to the <code>ContextHolder</code>.
|
||||
* </p>
|
||||
*
|
||||
* <P>
|
||||
* This filter will only execute once per request, to resolve servlet container
|
||||
* (specifically Weblogic) incompatibilities.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* If for whatever reason no <code>HttpSession</code> should <b>ever</b> be
|
||||
* created (eg this filter is only being used with Basic authentication or
|
||||
* similar clients that will never present the same <code>jsessionid</code>
|
||||
* etc), the {@link #setAllowSessionCreation(boolean)} should be set to
|
||||
* <code>false</code>. Only do this if you really need to conserve server
|
||||
* memory and are sure ensure all classes using the <code>ContextHolder</code>
|
||||
* are designed to have no persistence of the <code>Context</code> between web
|
||||
* requests.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* This filter MUST be executed BEFORE any authentication procesing mechanisms.
|
||||
* Authentication processing mechanisms (eg BASIC, CAS processing filters etc)
|
||||
* expect the <code>ContextHolder</code> to be contain a valid
|
||||
* <code>SecureContext</code> by the time they execute.
|
||||
* </p>
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @author Patrick Burleson
|
||||
* @version $Id$
|
||||
*/
|
||||
public class HttpSessionContextIntegrationFilter implements InitializingBean,
|
||||
Filter {
|
||||
//~ Static fields/initializers =============================================
|
||||
|
||||
protected static final Log logger = LogFactory.getLog(HttpSessionContextIntegrationFilter.class);
|
||||
private static final String FILTER_APPLIED = "__acegi_session_integration_filter_applied";
|
||||
public static final String ACEGI_SECURITY_CONTEXT_KEY = "ACEGI_SECURITY_CONTEXT";
|
||||
|
||||
//~ Instance fields ========================================================
|
||||
|
||||
private Class context;
|
||||
private Object contextObject;
|
||||
|
||||
/**
|
||||
* Indicates if this filter can create a <code>HttpSession</code> if needed
|
||||
* (sessions are always created sparingly, but setting this value to false
|
||||
* will prohibit sessions from ever being created). Defaults to true.
|
||||
*/
|
||||
private boolean allowSessionCreation = true;
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
public void setAllowSessionCreation(boolean allowSessionCreation) {
|
||||
this.allowSessionCreation = allowSessionCreation;
|
||||
}
|
||||
|
||||
public boolean isAllowSessionCreation() {
|
||||
return allowSessionCreation;
|
||||
}
|
||||
|
||||
public void setContext(Class secureContext) {
|
||||
this.context = secureContext;
|
||||
}
|
||||
|
||||
public Class getContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
if ((this.context == null)
|
||||
|| (!Context.class.isAssignableFrom(this.context))) {
|
||||
throw new IllegalArgumentException(
|
||||
"context must be defined and implement Context (typically use net.sf.acegisecurity.context.security.SecureContextImpl)");
|
||||
}
|
||||
|
||||
this.contextObject = generateNewContext();
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing. We use IoC container lifecycle services instead.
|
||||
*/
|
||||
public void destroy() {}
|
||||
|
||||
public void doFilter(ServletRequest request, ServletResponse response,
|
||||
FilterChain chain) throws IOException, ServletException {
|
||||
if ((request != null) && (request.getAttribute(FILTER_APPLIED) != null)) {
|
||||
// ensure that filter is only applied once per request
|
||||
chain.doFilter(request, response);
|
||||
} else {
|
||||
if (request != null) {
|
||||
request.setAttribute(FILTER_APPLIED, Boolean.TRUE);
|
||||
}
|
||||
|
||||
if (ContextHolder.getContext() != null) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn(
|
||||
"ContextHolder should have been null but contained: '"
|
||||
+ ContextHolder.getContext() + "'; setting to null now");
|
||||
}
|
||||
|
||||
ContextHolder.setContext(null);
|
||||
}
|
||||
|
||||
HttpSession httpSession = null;
|
||||
|
||||
try {
|
||||
httpSession = ((HttpServletRequest) request).getSession(false);
|
||||
} catch (IllegalStateException ignored) {}
|
||||
|
||||
if (httpSession != null) {
|
||||
Object contextObject = httpSession.getAttribute(ACEGI_SECURITY_CONTEXT_KEY);
|
||||
|
||||
if (contextObject != null) {
|
||||
if (contextObject instanceof Context) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(
|
||||
"Obtained from ACEGI_SECURITY_CONTEXT a valid Context and set to ContextHolder: '"
|
||||
+ contextObject + "'");
|
||||
}
|
||||
|
||||
ContextHolder.setContext((Context) contextObject);
|
||||
} else {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn(
|
||||
"ACEGI_SECURITY_CONTEXT did not contain a Context but contained: '"
|
||||
+ contextObject
|
||||
+ "'; are you improperly modifying the HttpSession directly (you should always use ContextHolder) or using the HttpSession attribute reserved for this class?");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(
|
||||
"HttpSession returned null object for ACEGI_SECURITY_CONTEXT");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("No HttpSession currently exists");
|
||||
}
|
||||
}
|
||||
|
||||
if (ContextHolder.getContext() == null) {
|
||||
ContextHolder.setContext(generateNewContext());
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(
|
||||
"As ContextHolder null, setup ContextHolder with a fresh new instance: '"
|
||||
+ ContextHolder.getContext() + "'");
|
||||
}
|
||||
}
|
||||
|
||||
// Proceed with chain
|
||||
chain.doFilter(request, response);
|
||||
|
||||
// Store context back to HttpSession
|
||||
try {
|
||||
httpSession = ((HttpServletRequest) request).getSession(false);
|
||||
} catch (IllegalStateException ignored) {}
|
||||
|
||||
// Generate a HttpSession only if we need to
|
||||
if (httpSession == null) {
|
||||
if (!allowSessionCreation) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(
|
||||
"Whilst ContextHolder contents have changed, the HttpSessionContextIntegrationFilter is prohibited from creating a HttpSession by the allowSessionCreation property being false");
|
||||
}
|
||||
} else if (!contextObject.equals(ContextHolder.getContext())) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(
|
||||
"HttpSession being created as ContextHolder contents are non-default");
|
||||
}
|
||||
|
||||
try {
|
||||
httpSession = ((HttpServletRequest) request).getSession(true);
|
||||
} catch (IllegalStateException ignored) {}
|
||||
} else {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(
|
||||
"HttpSession still null, but ContextHolder has not changed from default: ' "
|
||||
+ ContextHolder.getContext()
|
||||
+ "'; not creating HttpSession or storing ContextHolder contents");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If HttpSession exists, store current ContextHolder contents
|
||||
if (httpSession != null) {
|
||||
httpSession.setAttribute(ACEGI_SECURITY_CONTEXT_KEY,
|
||||
ContextHolder.getContext());
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Context stored to HttpSession: '"
|
||||
+ ContextHolder.getContext() + "'");
|
||||
}
|
||||
}
|
||||
|
||||
// Remove ContextHolder contents
|
||||
ContextHolder.setContext(null);
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(
|
||||
"ContextHolder set to null as request processing completed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Context generateNewContext() throws ServletException {
|
||||
try {
|
||||
return (Context) this.context.newInstance();
|
||||
} catch (InstantiationException ie) {
|
||||
throw new ServletException(ie);
|
||||
} catch (IllegalAccessException iae) {
|
||||
throw new ServletException(iae);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing. We use IoC container lifecycle services instead.
|
||||
*
|
||||
* @param filterConfig ignored
|
||||
*
|
||||
* @throws ServletException ignored
|
||||
*/
|
||||
public void init(FilterConfig filterConfig) throws ServletException {}
|
||||
}
|
||||
+3
-3
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -13,12 +13,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.sf.acegisecurity.ui.httpinvoker;
|
||||
package net.sf.acegisecurity.context.httpinvoker;
|
||||
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.AuthenticationCredentialsNotFoundException;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.logging.Log;
|
||||
+6
-6
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -13,7 +13,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.sf.acegisecurity.ui.rmi;
|
||||
package net.sf.acegisecurity.context.rmi;
|
||||
|
||||
import net.sf.acegisecurity.context.Context;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
@@ -34,10 +34,10 @@ import java.lang.reflect.InvocationTargetException;
|
||||
*
|
||||
* <p>
|
||||
* When constructed on the client via {@link
|
||||
* net.sf.acegisecurity.ui.rmi.ContextPropagatingRemoteInvocationFactory}, the
|
||||
* contents of the <code>ContextHolder</code> are stored inside the object.
|
||||
* The object is then passed to the server that is processing the remote
|
||||
* invocation. Upon the server invoking the remote invocation, it will
|
||||
* net.sf.acegisecurity.context.rmi.ContextPropagatingRemoteInvocationFactory},
|
||||
* the contents of the <code>ContextHolder</code> are stored inside the
|
||||
* object. The object is then passed to the server that is processing the
|
||||
* remote invocation. Upon the server invoking the remote invocation, it will
|
||||
* retrieve the passed contents of the <code>ContextHolder</code> and set them
|
||||
* to the server-side <code>ContextHolder</code> whilst the target object is
|
||||
* invoked. When the target invocation has been completed, the server-side
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -13,7 +13,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.sf.acegisecurity.ui.rmi;
|
||||
package net.sf.acegisecurity.context.rmi;
|
||||
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
|
||||
+3
-2
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -13,9 +13,10 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.sf.acegisecurity.context;
|
||||
package net.sf.acegisecurity.context.security;
|
||||
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.context.Context;
|
||||
|
||||
|
||||
/**
|
||||
+23
-2
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -13,9 +13,11 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.sf.acegisecurity.context;
|
||||
package net.sf.acegisecurity.context.security;
|
||||
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.context.ContextImpl;
|
||||
import net.sf.acegisecurity.context.ContextInvalidException;
|
||||
|
||||
|
||||
/**
|
||||
@@ -39,6 +41,25 @@ public class SecureContextImpl extends ContextImpl implements SecureContext {
|
||||
return this.authentication;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof SecureContextImpl) {
|
||||
SecureContextImpl test = (SecureContextImpl) obj;
|
||||
|
||||
if ((this.getAuthentication() == null)
|
||||
&& (test.getAuthentication() == null)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ((this.getAuthentication() != null)
|
||||
&& (test.getAuthentication() != null)
|
||||
&& this.getAuthentication().equals(test.getAuthentication())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append(super.toString());
|
||||
@@ -0,0 +1,45 @@
|
||||
/* Copyright 2004, 2005 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 net.sf.acegisecurity.context.security;
|
||||
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
|
||||
|
||||
/**
|
||||
* A simple static method for quickly accessing the <code>SecureContext</code>.
|
||||
*
|
||||
* <p>
|
||||
* Expects the <code>ContextHolder</code> to be populated and contain a valid
|
||||
* <code>SecureContext</code>.
|
||||
* </p>
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
*/
|
||||
public class SecureContextUtils {
|
||||
//~ Methods ================================================================
|
||||
|
||||
public static SecureContext getSecureContext() {
|
||||
if ((ContextHolder.getContext() == null)
|
||||
|| !(ContextHolder.getContext() instanceof SecureContext)) {
|
||||
throw new IllegalStateException("ContextHolder invalid: '"
|
||||
+ ContextHolder.getContext()
|
||||
+ "': are your filters ordered correctly? HttpSessionContextIntegrationFilter should have already executed by this time (look for it in the stack dump below)");
|
||||
}
|
||||
|
||||
return (SecureContext) ContextHolder.getContext();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<html>
|
||||
<body>
|
||||
Provides a <code>Context</code> that is designed to be compatible with
|
||||
Acegi Security.
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -27,7 +27,7 @@ import net.sf.acegisecurity.ConfigAttributeDefinition;
|
||||
import net.sf.acegisecurity.RunAsManager;
|
||||
import net.sf.acegisecurity.context.Context;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
import net.sf.acegisecurity.intercept.event.AuthenticationCredentialsNotFoundEvent;
|
||||
import net.sf.acegisecurity.intercept.event.AuthenticationFailureEvent;
|
||||
import net.sf.acegisecurity.intercept.event.AuthorizationFailureEvent;
|
||||
@@ -104,7 +104,6 @@ import java.util.Set;
|
||||
* object, return the <code>ContextHolder</code> to the object that existed
|
||||
* after the call to <code>AuthenticationManager</code>.
|
||||
* </li>
|
||||
*
|
||||
* <li>
|
||||
* If an <code>AfterInvocationManager</code> is defined, invoke the invocation
|
||||
* manager and allow it to replace the object due to be returned to the
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -20,7 +20,7 @@ import net.sf.acegisecurity.acl.AclEntry;
|
||||
import net.sf.acegisecurity.acl.AclManager;
|
||||
import net.sf.acegisecurity.acl.basic.AbstractBasicAclEntry;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -18,7 +18,7 @@ package net.sf.acegisecurity.taglibs.authz;
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.UserDetails;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -18,14 +18,15 @@ package net.sf.acegisecurity.taglibs.authz;
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.GrantedAuthorityImpl;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
|
||||
import org.springframework.web.util.ExpressionEvaluationUtils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import javax.servlet.jsp.JspException;
|
||||
import javax.servlet.jsp.tagext.Tag;
|
||||
import javax.servlet.jsp.tagext.TagSupport;
|
||||
import java.util.*;
|
||||
|
||||
import org.springframework.web.util.ExpressionEvaluationUtils;
|
||||
|
||||
|
||||
/**
|
||||
@@ -70,19 +71,18 @@ public class AuthorizeTag extends TagSupport {
|
||||
|
||||
public int doStartTag() throws JspException {
|
||||
if (((null == ifAllGranted) || "".equals(ifAllGranted))
|
||||
&& ((null == ifAnyGranted) || "".equals(ifAnyGranted))
|
||||
&& ((null == ifNotGranted) || "".equals(ifNotGranted))) {
|
||||
&& ((null == ifAnyGranted) || "".equals(ifAnyGranted))
|
||||
&& ((null == ifNotGranted) || "".equals(ifNotGranted))) {
|
||||
return Tag.SKIP_BODY;
|
||||
}
|
||||
|
||||
final Collection granted = getPrincipalAuthorities();
|
||||
|
||||
final String evaledIfNotGranted =
|
||||
ExpressionEvaluationUtils.evaluateString(
|
||||
"ifNotGranted", ifNotGranted, pageContext);
|
||||
final String evaledIfNotGranted = ExpressionEvaluationUtils
|
||||
.evaluateString("ifNotGranted", ifNotGranted, pageContext);
|
||||
|
||||
if ((null != evaledIfNotGranted) && !"".equals(evaledIfNotGranted)) {
|
||||
Set grantedCopy = retainAll(
|
||||
granted,
|
||||
Set grantedCopy = retainAll(granted,
|
||||
parseAuthoritiesString(evaledIfNotGranted));
|
||||
|
||||
if (!grantedCopy.isEmpty()) {
|
||||
@@ -90,22 +90,20 @@ public class AuthorizeTag extends TagSupport {
|
||||
}
|
||||
}
|
||||
|
||||
final String evaledIfAllGranted =
|
||||
ExpressionEvaluationUtils.evaluateString(
|
||||
"ifAllGranted", ifAllGranted, pageContext);
|
||||
final String evaledIfAllGranted = ExpressionEvaluationUtils
|
||||
.evaluateString("ifAllGranted", ifAllGranted, pageContext);
|
||||
|
||||
if ((null != evaledIfAllGranted) && !"".equals(evaledIfAllGranted)) {
|
||||
if (!granted.containsAll(
|
||||
parseAuthoritiesString(evaledIfAllGranted))) {
|
||||
if (!granted.containsAll(parseAuthoritiesString(evaledIfAllGranted))) {
|
||||
return Tag.SKIP_BODY;
|
||||
}
|
||||
}
|
||||
|
||||
final String evaledIfAnyGranted =
|
||||
ExpressionEvaluationUtils.evaluateString(
|
||||
"ifAnyGranted", ifAnyGranted, pageContext);
|
||||
final String evaledIfAnyGranted = ExpressionEvaluationUtils
|
||||
.evaluateString("ifAnyGranted", ifAnyGranted, pageContext);
|
||||
|
||||
if ((null != evaledIfAnyGranted) && !"".equals(evaledIfAnyGranted)) {
|
||||
Set grantedCopy = retainAll(
|
||||
granted,
|
||||
Set grantedCopy = retainAll(granted,
|
||||
parseAuthoritiesString(evaledIfAnyGranted));
|
||||
|
||||
if (grantedCopy.isEmpty()) {
|
||||
@@ -148,7 +146,7 @@ public class AuthorizeTag extends TagSupport {
|
||||
}
|
||||
|
||||
private Set retainAll(final Collection granted,
|
||||
final Set requiredAuthorities) {
|
||||
final Set requiredAuthorities) {
|
||||
Set grantedCopy = new HashSet(granted);
|
||||
grantedCopy.retainAll(requiredAuthorities);
|
||||
|
||||
|
||||
@@ -1,232 +0,0 @@
|
||||
/* Copyright 2004, 2005 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 net.sf.acegisecurity.ui;
|
||||
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.context.Context;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.context.SecureContextImpl;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
|
||||
|
||||
/**
|
||||
* Automatically populates a {@link net.sf.acegisecurity.context.SecureContext}
|
||||
* from a subclass-provided <code>Authentication</code> object.
|
||||
*
|
||||
* <p>
|
||||
* The container hosting the Acegi Security System for Spring secured
|
||||
* application is expected to expose an {@link Authentication} object in a
|
||||
* well-known location. The <code>Authentication</code> object will have been
|
||||
* created by the Acegi Security System for Spring and placed into the
|
||||
* well-known location via approaches such as container adapters or container
|
||||
* sessions.
|
||||
* </p>
|
||||
*
|
||||
* <P>
|
||||
* Once the <code>Authentication</code> object has been extracted from the
|
||||
* well-known location, the <code>AbstractIntegrationFilter</code> handles
|
||||
* putting it into the {@link ContextHolder}. It then removes it once the
|
||||
* filter chain has completed.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* This filter will not abort if an <code>Authentication</code> object cannot
|
||||
* be obtained from the well-known location. It will simply continue the
|
||||
* filter chain as normal.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* If the <code>ContextHolder</code> does not contain a valid {@link
|
||||
* SecureContext}, one will be created. The created object will be of the
|
||||
* instance defined by the {@link #setSecureContext(Class)} method.
|
||||
* </p>
|
||||
*
|
||||
* <P>
|
||||
* This filter will only execute once per request, to resolve servlet container
|
||||
* (specifically Weblogic) incompatibilities.
|
||||
* </p>
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @author Patrick Burleson
|
||||
* @version $Id$
|
||||
*/
|
||||
public abstract class AbstractIntegrationFilter implements InitializingBean,
|
||||
Filter {
|
||||
//~ Static fields/initializers =============================================
|
||||
|
||||
protected static final Log logger = LogFactory.getLog(AbstractIntegrationFilter.class);
|
||||
private static final String FILTER_APPLIED = "__acegi_integration_filterapplied";
|
||||
|
||||
//~ Instance fields ========================================================
|
||||
|
||||
private Class secureContext = SecureContextImpl.class;
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
public void setSecureContext(Class secureContext) {
|
||||
this.secureContext = secureContext;
|
||||
}
|
||||
|
||||
public Class getSecureContext() {
|
||||
return secureContext;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
if ((this.secureContext == null)
|
||||
|| (!SecureContext.class.isAssignableFrom(this.secureContext))) {
|
||||
throw new IllegalArgumentException(
|
||||
"secureContext must be defined and implement SecureContext");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a new <code>Authentication</code> object to the container's
|
||||
* well-known location, if supported the subclass.
|
||||
*
|
||||
* @param request which may be required by the implementing method to
|
||||
* access the well-known location for the current principal
|
||||
* @param authentication the new object to be written to the container
|
||||
*/
|
||||
public abstract void commitToContainer(ServletRequest request,
|
||||
Authentication authentication);
|
||||
|
||||
public void destroy() {}
|
||||
|
||||
public void doFilter(ServletRequest request, ServletResponse response,
|
||||
FilterChain chain) throws IOException, ServletException {
|
||||
if ((request != null) && (request.getAttribute(FILTER_APPLIED) != null)) {
|
||||
// ensure that filter is only applied once per request
|
||||
chain.doFilter(request, response);
|
||||
} else {
|
||||
if (request != null) {
|
||||
request.setAttribute(FILTER_APPLIED, Boolean.TRUE);
|
||||
}
|
||||
|
||||
// Populate authentication information
|
||||
Object extracted = this.extractFromContainer(request);
|
||||
|
||||
if (extracted instanceof Authentication) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Authentication '" + extracted
|
||||
+ "' added to ContextHolder from container");
|
||||
}
|
||||
|
||||
Authentication auth = (Authentication) extracted;
|
||||
|
||||
// Get or create existing SecureContext
|
||||
SecureContext sc = null;
|
||||
|
||||
if ((ContextHolder.getContext() == null)
|
||||
|| !(ContextHolder.getContext() instanceof SecureContext)) {
|
||||
try {
|
||||
sc = (SecureContext) this.secureContext.newInstance();
|
||||
} catch (InstantiationException ie) {
|
||||
throw new ServletException(ie);
|
||||
} catch (IllegalAccessException iae) {
|
||||
throw new ServletException(iae);
|
||||
}
|
||||
} else {
|
||||
sc = (SecureContext) ContextHolder.getContext();
|
||||
}
|
||||
|
||||
// Add Authentication to SecureContext, and save
|
||||
sc.setAuthentication(auth);
|
||||
ContextHolder.setContext((Context) sc);
|
||||
} else {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(
|
||||
"Authentication not added to ContextHolder (could not extract an authentication object from the container which is an instance of Authentication)");
|
||||
}
|
||||
}
|
||||
|
||||
// Proceed with chain
|
||||
chain.doFilter(request, response);
|
||||
|
||||
// Remove authentication information
|
||||
if ((ContextHolder.getContext() != null)
|
||||
&& ContextHolder.getContext() instanceof SecureContext) {
|
||||
// Get context holder
|
||||
SecureContext secureContext = (SecureContext) ContextHolder
|
||||
.getContext();
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(
|
||||
"Updating container with new Authentication object ('"
|
||||
+ secureContext.getAuthentication()
|
||||
+ "'), and then removing Authentication from ContextHolder");
|
||||
}
|
||||
|
||||
// Update container with new Authentication object (may have been updated during method invocation)
|
||||
this.commitToContainer(request,
|
||||
secureContext.getAuthentication());
|
||||
|
||||
// Remove authentication information from ContextHolder
|
||||
secureContext.setAuthentication(null);
|
||||
ContextHolder.setContext((Context) secureContext);
|
||||
} else {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(
|
||||
"ContextHolder does not contain any authentication information");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclasses must override this method to provide the <code>Object</code>
|
||||
* that contains the <code>Authentication</code> interface.
|
||||
*
|
||||
* <p>
|
||||
* For convenience we have allowed any <code>Object</code> to be returned
|
||||
* by subclasses, as the abstract class will ensure class casting safety
|
||||
* and ignore objects that do not implement <code>Authentication</code>.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* If no <code>Authentication</code> object is available, subclasses should
|
||||
* return <code>null</code>.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* If the subclass can locate multiple authentication objects, they should
|
||||
* return the object that was created by the Acegi Security System for
|
||||
* Spring (ie the object that implements <code>Authentication</code>).
|
||||
* </p>
|
||||
*
|
||||
* @param request the request, which may be of use in extracting the
|
||||
* authentication object
|
||||
*
|
||||
* @return <code>null</code> or an object that implements
|
||||
* <code>Authentication</code>
|
||||
*/
|
||||
public abstract Object extractFromContainer(ServletRequest request);
|
||||
|
||||
public void init(FilterConfig filterConfig) throws ServletException {}
|
||||
}
|
||||
@@ -22,8 +22,10 @@ import net.sf.acegisecurity.AuthenticationServiceException;
|
||||
import net.sf.acegisecurity.BadCredentialsException;
|
||||
import net.sf.acegisecurity.DisabledException;
|
||||
import net.sf.acegisecurity.LockedException;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContextUtils;
|
||||
import net.sf.acegisecurity.providers.cas.ProxyUntrustedException;
|
||||
import net.sf.acegisecurity.ui.webapp.HttpSessionIntegrationFilter;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@@ -34,6 +36,7 @@ import java.io.IOException;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
@@ -42,15 +45,13 @@ import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
|
||||
/**
|
||||
* Abstract processor of HTTP-based authentication requests, which places the
|
||||
* resulting <code>Authentication</code> object into the
|
||||
* <code>HttpSession</code>.
|
||||
* Abstract processor of browser-based HTTP-based authentication requests.
|
||||
*
|
||||
* <p>
|
||||
* This filter is responsible for processing authentication requests. If
|
||||
* authentication is successful, the resulting {@link Authentication} object
|
||||
* will be placed into the <code>HttpSession</code> with the attribute defined
|
||||
* by {@link HttpSessionIntegrationFilter#ACEGI_SECURITY_AUTHENTICATION_KEY}.
|
||||
* will be placed into the <code>ContextHolder</code>, which is guaranteed to
|
||||
* have already been created by an earlier filter.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
@@ -307,6 +308,9 @@ public abstract class AbstractProcessingFilter implements Filter,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing. We use IoC container lifecycle services instead.
|
||||
*/
|
||||
public void destroy() {}
|
||||
|
||||
public void doFilter(ServletRequest request, ServletResponse response,
|
||||
@@ -353,6 +357,15 @@ public abstract class AbstractProcessingFilter implements Filter,
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing. We use IoC container lifecycle services instead.
|
||||
*
|
||||
* @param arg0 ignored
|
||||
*
|
||||
* @throws ServletException ignored
|
||||
*/
|
||||
public void init(FilterConfig arg0) throws ServletException {}
|
||||
|
||||
protected void onPreAuthentication(HttpServletRequest request,
|
||||
HttpServletResponse response) throws IOException {}
|
||||
|
||||
@@ -390,8 +403,14 @@ public abstract class AbstractProcessingFilter implements Filter,
|
||||
logger.debug("Authentication success: " + authResult.toString());
|
||||
}
|
||||
|
||||
request.getSession().setAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY,
|
||||
authResult);
|
||||
SecureContext sc = SecureContextUtils.getSecureContext();
|
||||
sc.setAuthentication(authResult);
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(
|
||||
"Updated ContextHolder to contain the following Authentication: '"
|
||||
+ authResult + "'");
|
||||
}
|
||||
|
||||
String targetUrl = (String) request.getSession().getAttribute(ACEGI_SECURITY_TARGET_URL_KEY);
|
||||
request.getSession().removeAttribute(ACEGI_SECURITY_TARGET_URL_KEY);
|
||||
@@ -418,6 +437,14 @@ public abstract class AbstractProcessingFilter implements Filter,
|
||||
protected void unsuccessfulAuthentication(HttpServletRequest request,
|
||||
HttpServletResponse response, AuthenticationException failed)
|
||||
throws IOException {
|
||||
SecureContext sc = SecureContextUtils.getSecureContext();
|
||||
sc.setAuthentication(null);
|
||||
ContextHolder.setContext(sc);
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Updated ContextHolder to contain null Authentication");
|
||||
}
|
||||
|
||||
String failureUrl = authenticationFailureUrl;
|
||||
|
||||
if (failed instanceof AuthenticationServiceException
|
||||
@@ -451,7 +478,6 @@ public abstract class AbstractProcessingFilter implements Filter,
|
||||
|
||||
request.getSession().setAttribute(ACEGI_SECURITY_LAST_EXCEPTION_KEY,
|
||||
failed);
|
||||
request.getSession().removeAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY);
|
||||
|
||||
onUnsuccessfulAuthentication(request, response);
|
||||
|
||||
|
||||
@@ -18,10 +18,13 @@ package net.sf.acegisecurity.ui.basicauth;
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.AuthenticationException;
|
||||
import net.sf.acegisecurity.AuthenticationManager;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.HttpSessionContextIntegrationFilter;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContextUtils;
|
||||
import net.sf.acegisecurity.intercept.web.AuthenticationEntryPoint;
|
||||
import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;
|
||||
import net.sf.acegisecurity.ui.WebAuthenticationDetails;
|
||||
import net.sf.acegisecurity.ui.webapp.HttpSessionIntegrationFilter;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.logging.Log;
|
||||
@@ -73,7 +76,8 @@ import javax.servlet.http.HttpServletResponse;
|
||||
* <P>
|
||||
* If authentication is successful, the resulting {@link Authentication} object
|
||||
* will be placed into the <code>HttpSession</code> with the attribute defined
|
||||
* by {@link HttpSessionIntegrationFilter#ACEGI_SECURITY_AUTHENTICATION_KEY}.
|
||||
* by {@link
|
||||
* HttpSessionContextIntegrationFilter#ACEGI_SECURITY_AUTHENTICATION_KEY}.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
@@ -172,6 +176,7 @@ public class BasicProcessingFilter implements Filter, InitializingBean {
|
||||
authRequest.setDetails(new WebAuthenticationDetails(httpRequest));
|
||||
|
||||
Authentication authResult;
|
||||
SecureContext sc = SecureContextUtils.getSecureContext();
|
||||
|
||||
try {
|
||||
authResult = authenticationManager.authenticate(authRequest);
|
||||
@@ -182,6 +187,8 @@ public class BasicProcessingFilter implements Filter, InitializingBean {
|
||||
+ " failed: " + failed.toString());
|
||||
}
|
||||
|
||||
sc.setAuthentication(null);
|
||||
ContextHolder.setContext(sc);
|
||||
authenticationEntryPoint.commence(request, response, failed);
|
||||
|
||||
return;
|
||||
@@ -192,8 +199,8 @@ public class BasicProcessingFilter implements Filter, InitializingBean {
|
||||
logger.debug("Authentication success: " + authResult.toString());
|
||||
}
|
||||
|
||||
httpRequest.getSession().setAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY,
|
||||
authResult);
|
||||
sc.setAuthentication(authResult);
|
||||
ContextHolder.setContext(sc);
|
||||
}
|
||||
|
||||
chain.doFilter(request, response);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
Interfaces the system with various end-user authentication approaches, such
|
||||
as container adapters and web applications.
|
||||
Authentication processing mechanisms, which respond to the submission of authentication
|
||||
credentials using various protocols (eg BASIC, CAS, form login etc).
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,158 +0,0 @@
|
||||
/* Copyright 2004, 2005 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 net.sf.acegisecurity.ui.webapp;
|
||||
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.ui.AbstractIntegrationFilter;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
|
||||
/**
|
||||
* Populates a {@link net.sf.acegisecurity.context.SecureContext} from the
|
||||
* <code>HttpSession</code>.
|
||||
*
|
||||
* <P>
|
||||
* The filter will inspect the <code>HttpSession</code> for an attribute with
|
||||
* the name indicated by {@link #ACEGI_SECURITY_AUTHENTICATION_KEY}. If that
|
||||
* attribute contains an instance of {@link Authentication}, it will be placed
|
||||
* into the <code>ContextHolder</code>.
|
||||
* </p>
|
||||
*
|
||||
* <P>
|
||||
* This filter is normally used in conjunction with {@link
|
||||
* AuthenticationProcessingFilter}, which populates the
|
||||
* <code>HttpSession</code> with an <code>Authentication</code> object based
|
||||
* on a form login. Similarly, the {@link
|
||||
* net.sf.acegisecurity.ui.basicauth.BasicProcessingFilter} will populate the
|
||||
* <code>HttpSession</code> based on a BASIC authentication request.
|
||||
* Alternatively, users may elect to use their own approach for populating the
|
||||
* <code>HttpSession</code>.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* As with other <code>AbstractIntegrationFilter</code>s, this filter will
|
||||
* ensure the <code>ContextHolder</code> is populated with the
|
||||
* <code>Authentication</code> object for the duration of the HTTP request,
|
||||
* and is unbound from the <code>ContextHolder</code> at the completion of the
|
||||
* request.
|
||||
* </p>
|
||||
*
|
||||
* <P>
|
||||
* The filter can also copy the <code>Authentication</code> object to any
|
||||
* number of additional <code>HttpSession</code> attributes. To use this
|
||||
* capability, provide <code>String</code>s indicating the additional
|
||||
* attribute name(s) to {@link #setAdditionalAttributes(List)}.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* See {@link AbstractIntegrationFilter} for further information.
|
||||
* </p>
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
*/
|
||||
public class HttpSessionIntegrationFilter extends AbstractIntegrationFilter {
|
||||
//~ Static fields/initializers =============================================
|
||||
|
||||
public static final String ACEGI_SECURITY_AUTHENTICATION_KEY = "ACEGI_SECURITY_AUTHENTICATION";
|
||||
|
||||
//~ Instance fields ========================================================
|
||||
|
||||
private List additionalAttributes = null;
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
public void setAdditionalAttributes(List additionalAttributes) {
|
||||
validateList(additionalAttributes);
|
||||
this.additionalAttributes = additionalAttributes;
|
||||
}
|
||||
|
||||
public List getAdditionalAttributes() {
|
||||
return additionalAttributes;
|
||||
}
|
||||
|
||||
public void commitToContainer(ServletRequest request,
|
||||
Authentication authentication) {
|
||||
if (request instanceof HttpServletRequest
|
||||
&& ((HttpServletRequest) request).isRequestedSessionIdValid()) {
|
||||
HttpSession httpSession = ((HttpServletRequest) request).getSession(false);
|
||||
|
||||
if (httpSession != null) {
|
||||
httpSession.setAttribute(ACEGI_SECURITY_AUTHENTICATION_KEY,
|
||||
authentication);
|
||||
updateOtherLocations(httpSession, authentication);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Object extractFromContainer(ServletRequest request) {
|
||||
if (request instanceof HttpServletRequest) {
|
||||
HttpSession httpSession = null;
|
||||
|
||||
try {
|
||||
httpSession = ((HttpServletRequest) request).getSession(false);
|
||||
} catch (IllegalStateException ignored) {}
|
||||
|
||||
if (httpSession != null) {
|
||||
Object authObject = httpSession.getAttribute(ACEGI_SECURITY_AUTHENTICATION_KEY);
|
||||
|
||||
if (authObject instanceof Authentication) {
|
||||
updateOtherLocations(httpSession,
|
||||
(Authentication) authObject);
|
||||
|
||||
return authObject;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void updateOtherLocations(HttpSession session,
|
||||
Authentication authentication) {
|
||||
if (additionalAttributes == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Iterator iter = additionalAttributes.iterator();
|
||||
|
||||
while (iter.hasNext()) {
|
||||
String attribute = (String) iter.next();
|
||||
session.setAttribute(attribute, authentication);
|
||||
}
|
||||
}
|
||||
|
||||
private void validateList(List newAdditionalAttributes) {
|
||||
if (newAdditionalAttributes != null) {
|
||||
Iterator iter = newAdditionalAttributes.iterator();
|
||||
|
||||
while (iter.hasNext()) {
|
||||
Object objectToTest = iter.next();
|
||||
|
||||
if (!(objectToTest instanceof String)) {
|
||||
throw new IllegalArgumentException(
|
||||
"List of additional attributes can only contains Strings!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -13,7 +13,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.sf.acegisecurity.ui.wrapper;
|
||||
package net.sf.acegisecurity.wrapper;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
+2
-2
@@ -13,13 +13,13 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.sf.acegisecurity.ui.wrapper;
|
||||
package net.sf.acegisecurity.wrapper;
|
||||
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.GrantedAuthority;
|
||||
import net.sf.acegisecurity.UserDetails;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
|
||||
import java.security.Principal;
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
|
||||
package net.sf.acegisecurity;
|
||||
|
||||
import org.springframework.mock.web.MockHttpSession;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -315,10 +317,20 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
}
|
||||
|
||||
public HttpSession getSession(boolean arg0) {
|
||||
if (arg0) {
|
||||
if (this.session == null) {
|
||||
this.session = new MockHttpSession();
|
||||
}
|
||||
}
|
||||
|
||||
return this.session;
|
||||
}
|
||||
|
||||
public HttpSession getSession() {
|
||||
if (this.session == null) {
|
||||
this.session = new MockHttpSession();
|
||||
}
|
||||
|
||||
return this.session;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -17,7 +17,7 @@ package net.sf.acegisecurity;
|
||||
|
||||
import net.sf.acegisecurity.context.Context;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
|
||||
|
||||
/**
|
||||
|
||||
+46
-20
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -17,9 +17,14 @@ package net.sf.acegisecurity.adapters;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import net.sf.acegisecurity.*;
|
||||
import net.sf.acegisecurity.GrantedAuthority;
|
||||
import net.sf.acegisecurity.GrantedAuthorityImpl;
|
||||
import net.sf.acegisecurity.MockHttpServletRequest;
|
||||
import net.sf.acegisecurity.MockHttpServletResponse;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.security.SecureContextImpl;
|
||||
import net.sf.acegisecurity.context.security.SecureContextUtils;
|
||||
import net.sf.acegisecurity.util.MockFilterChain;
|
||||
|
||||
|
||||
/**
|
||||
@@ -41,41 +46,62 @@ public class HttpRequestIntegrationFilterTests extends TestCase {
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
public final void setUp() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
junit.textui.TestRunner.run(HttpRequestIntegrationFilterTests.class);
|
||||
}
|
||||
|
||||
public void testCorrectOperation() {
|
||||
public void testCorrectOperation() throws Exception {
|
||||
HttpRequestIntegrationFilter filter = new HttpRequestIntegrationFilter();
|
||||
PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key",
|
||||
"someone", "password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")});
|
||||
Object result = filter.extractFromContainer(new MockHttpServletRequest(
|
||||
principal, null));
|
||||
|
||||
if (!(result instanceof PrincipalAcegiUserToken)) {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(principal,
|
||||
null);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockFilterChain chain = new MockFilterChain(true);
|
||||
|
||||
filter.doFilter(request, response, chain);
|
||||
|
||||
if (!(SecureContextUtils.getSecureContext().getAuthentication() instanceof PrincipalAcegiUserToken)) {
|
||||
fail("Should have returned PrincipalAcegiUserToken");
|
||||
}
|
||||
|
||||
PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) result;
|
||||
assertEquals(principal, result);
|
||||
|
||||
filter.commitToContainer(new MockHttpServletRequest(principal, null),
|
||||
principal);
|
||||
PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) SecureContextUtils.getSecureContext()
|
||||
.getAuthentication();
|
||||
assertEquals(principal, castResult);
|
||||
}
|
||||
|
||||
public void testHandlesIfHttpRequestIsNullForSomeReason() {
|
||||
public void testHandlesIfHttpRequestIsNullForSomeReason()
|
||||
throws Exception {
|
||||
HttpRequestIntegrationFilter filter = new HttpRequestIntegrationFilter();
|
||||
assertEquals(null, filter.extractFromContainer(null));
|
||||
|
||||
try {
|
||||
filter.doFilter(null, null, null);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testHandlesIfThereIsNoPrincipal() {
|
||||
public void testHandlesIfThereIsNoPrincipal() throws Exception {
|
||||
HttpRequestIntegrationFilter filter = new HttpRequestIntegrationFilter();
|
||||
assertEquals(null,
|
||||
filter.extractFromContainer(new MockHttpServletRequest(null, null)));
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("foo");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockFilterChain chain = new MockFilterChain(true);
|
||||
|
||||
assertNull(SecureContextUtils.getSecureContext().getAuthentication());
|
||||
filter.doFilter(request, response, chain);
|
||||
assertNull(SecureContextUtils.getSecureContext().getAuthentication());
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
ContextHolder.setContext(new SecureContextImpl());
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
ContextHolder.setContext(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -17,6 +17,8 @@ package net.sf.acegisecurity.context;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import net.sf.acegisecurity.context.security.SecureContextImpl;
|
||||
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
|
||||
|
||||
|
||||
+245
@@ -0,0 +1,245 @@
|
||||
/* Copyright 2004, 2005 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 net.sf.acegisecurity.context;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.GrantedAuthority;
|
||||
import net.sf.acegisecurity.GrantedAuthorityImpl;
|
||||
import net.sf.acegisecurity.MockFilterConfig;
|
||||
import net.sf.acegisecurity.MockHttpServletRequest;
|
||||
import net.sf.acegisecurity.MockHttpServletResponse;
|
||||
import net.sf.acegisecurity.MockHttpSession;
|
||||
import net.sf.acegisecurity.adapters.PrincipalAcegiUserToken;
|
||||
import net.sf.acegisecurity.context.HttpSessionContextIntegrationFilter;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContextImpl;
|
||||
import net.sf.acegisecurity.context.security.SecureContextUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
|
||||
|
||||
/**
|
||||
* Tests {@link HttpSessionContextIntegrationFilter}.
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
*/
|
||||
public class HttpSessionContextIntegrationFilterTests extends TestCase {
|
||||
//~ Constructors ===========================================================
|
||||
|
||||
public HttpSessionContextIntegrationFilterTests() {
|
||||
super();
|
||||
}
|
||||
|
||||
public HttpSessionContextIntegrationFilterTests(String arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
public static void main(String[] args) {
|
||||
junit.textui.TestRunner.run(HttpSessionContextIntegrationFilterTests.class);
|
||||
}
|
||||
|
||||
public void testDetectsMissingOrInvalidContext() throws Exception {
|
||||
HttpSessionContextIntegrationFilter filter = new HttpSessionContextIntegrationFilter();
|
||||
|
||||
try {
|
||||
filter.afterPropertiesSet();
|
||||
fail("Shown have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
try {
|
||||
filter.setContext(Integer.class);
|
||||
assertEquals(Integer.class, filter.getContext());
|
||||
filter.afterPropertiesSet();
|
||||
fail("Shown have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testExistingContextContentsCopiedIntoContextHolderFromSessionAndChangesToContextCopiedBackToSession()
|
||||
throws Exception {
|
||||
// Build an Authentication object we simulate came from HttpSession
|
||||
PrincipalAcegiUserToken sessionPrincipal = new PrincipalAcegiUserToken("key",
|
||||
"someone", "password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")});
|
||||
|
||||
// Build an Authentication object we simulate our Authentication changed it to
|
||||
PrincipalAcegiUserToken updatedPrincipal = new PrincipalAcegiUserToken("key",
|
||||
"someone", "password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_DIFFERENT_ROLE")});
|
||||
|
||||
// Build a Context to store in HttpSession (simulating prior request)
|
||||
SecureContext sc = new SecureContextImpl();
|
||||
sc.setAuthentication(sessionPrincipal);
|
||||
|
||||
// Build a mock request
|
||||
MockHttpSession session = new MockHttpSession();
|
||||
session.setAttribute(HttpSessionContextIntegrationFilter.ACEGI_SECURITY_CONTEXT_KEY,
|
||||
sc);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(null,
|
||||
session);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
FilterChain chain = new MockFilterChain(sessionPrincipal,
|
||||
updatedPrincipal);
|
||||
|
||||
// Prepare filter
|
||||
HttpSessionContextIntegrationFilter filter = new HttpSessionContextIntegrationFilter();
|
||||
filter.setContext(SecureContextImpl.class);
|
||||
filter.afterPropertiesSet();
|
||||
|
||||
// Execute filter
|
||||
executeFilterInContainerSimulator(new MockFilterConfig(), filter,
|
||||
request, response, chain);
|
||||
|
||||
// Obtain new/update Authentication from HttpSession
|
||||
Context context = (Context) session.getAttribute(HttpSessionContextIntegrationFilter.ACEGI_SECURITY_CONTEXT_KEY);
|
||||
assertEquals(updatedPrincipal,
|
||||
((SecureContext) context).getAuthentication());
|
||||
}
|
||||
|
||||
public void testHttpSessionCreatedWhenContextHolderChanges()
|
||||
throws Exception {
|
||||
// Build an Authentication object we simulate our Authentication changed it to
|
||||
PrincipalAcegiUserToken updatedPrincipal = new PrincipalAcegiUserToken("key",
|
||||
"someone", "password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_DIFFERENT_ROLE")});
|
||||
|
||||
// Build a mock request
|
||||
MockHttpSession session = null;
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(null,
|
||||
session);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
FilterChain chain = new MockFilterChain(null, updatedPrincipal);
|
||||
|
||||
// Prepare filter
|
||||
HttpSessionContextIntegrationFilter filter = new HttpSessionContextIntegrationFilter();
|
||||
filter.setContext(SecureContextImpl.class);
|
||||
filter.afterPropertiesSet();
|
||||
|
||||
// Execute filter
|
||||
executeFilterInContainerSimulator(new MockFilterConfig(), filter,
|
||||
request, response, chain);
|
||||
|
||||
// Obtain new/update Authentication from HttpSession
|
||||
Context context = (Context) request.getSession().getAttribute(HttpSessionContextIntegrationFilter.ACEGI_SECURITY_CONTEXT_KEY);
|
||||
assertEquals(updatedPrincipal,
|
||||
((SecureContext) context).getAuthentication());
|
||||
}
|
||||
|
||||
public void testHttpSessionNotCreatedUnlessContextHolderChanges()
|
||||
throws Exception {
|
||||
// Build a mock request
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(null, null);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
FilterChain chain = new MockFilterChain(null, null);
|
||||
|
||||
// Prepare filter
|
||||
HttpSessionContextIntegrationFilter filter = new HttpSessionContextIntegrationFilter();
|
||||
filter.setContext(SecureContextImpl.class);
|
||||
filter.afterPropertiesSet();
|
||||
|
||||
// Execute filter
|
||||
executeFilterInContainerSimulator(new MockFilterConfig(), filter,
|
||||
request, response, chain);
|
||||
|
||||
// Obtain new/update Authentication from HttpSession
|
||||
assertNull(request.getSession(false));
|
||||
}
|
||||
|
||||
public void testHttpSessionWithNonContextInWellKnownLocationIsOverwritten()
|
||||
throws Exception {
|
||||
// Build an Authentication object we simulate our Authentication changed it to
|
||||
PrincipalAcegiUserToken updatedPrincipal = new PrincipalAcegiUserToken("key",
|
||||
"someone", "password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_DIFFERENT_ROLE")});
|
||||
|
||||
// Build a mock request
|
||||
MockHttpSession session = new MockHttpSession();
|
||||
session.setAttribute(HttpSessionContextIntegrationFilter.ACEGI_SECURITY_CONTEXT_KEY,
|
||||
"NOT_A_CONTEXT_OBJECT");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(null,
|
||||
session);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
FilterChain chain = new MockFilterChain(null, updatedPrincipal);
|
||||
|
||||
// Prepare filter
|
||||
HttpSessionContextIntegrationFilter filter = new HttpSessionContextIntegrationFilter();
|
||||
filter.setContext(SecureContextImpl.class);
|
||||
filter.afterPropertiesSet();
|
||||
|
||||
// Execute filter
|
||||
executeFilterInContainerSimulator(new MockFilterConfig(), filter,
|
||||
request, response, chain);
|
||||
|
||||
// Obtain new/update Authentication from HttpSession
|
||||
Context context = (Context) session.getAttribute(HttpSessionContextIntegrationFilter.ACEGI_SECURITY_CONTEXT_KEY);
|
||||
assertEquals(updatedPrincipal,
|
||||
((SecureContext) context).getAuthentication());
|
||||
}
|
||||
|
||||
private void executeFilterInContainerSimulator(FilterConfig filterConfig,
|
||||
Filter filter, ServletRequest request, ServletResponse response,
|
||||
FilterChain filterChain) throws ServletException, IOException {
|
||||
filter.init(filterConfig);
|
||||
filter.doFilter(request, response, filterChain);
|
||||
filter.destroy();
|
||||
}
|
||||
|
||||
//~ Inner Classes ==========================================================
|
||||
|
||||
private class MockFilterChain extends TestCase implements FilterChain {
|
||||
private Authentication changeContextHolder;
|
||||
private Authentication expectedOnContextHolder;
|
||||
|
||||
public MockFilterChain(Authentication expectedOnContextHolder,
|
||||
Authentication changeContextHolder) {
|
||||
this.expectedOnContextHolder = expectedOnContextHolder;
|
||||
this.changeContextHolder = changeContextHolder;
|
||||
}
|
||||
|
||||
private MockFilterChain() {}
|
||||
|
||||
public void doFilter(ServletRequest arg0, ServletResponse arg1)
|
||||
throws IOException, ServletException {
|
||||
if (expectedOnContextHolder != null) {
|
||||
assertEquals(expectedOnContextHolder,
|
||||
SecureContextUtils.getSecureContext().getAuthentication());
|
||||
}
|
||||
|
||||
if (changeContextHolder != null) {
|
||||
SecureContext sc = SecureContextUtils.getSecureContext();
|
||||
sc.setAuthentication(changeContextHolder);
|
||||
ContextHolder.setContext(sc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -18,6 +18,8 @@ package net.sf.acegisecurity.context;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContextImpl;
|
||||
import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;
|
||||
|
||||
|
||||
|
||||
+5
-4
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -13,14 +13,15 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.sf.acegisecurity.ui.httpinvoker;
|
||||
package net.sf.acegisecurity.context.httpinvoker;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.context.SecureContextImpl;
|
||||
import net.sf.acegisecurity.context.httpinvoker.AuthenticationSimpleHttpInvokerRequestExecutor;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContextImpl;
|
||||
import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;
|
||||
|
||||
import java.io.IOException;
|
||||
+6
-4
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -13,7 +13,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.sf.acegisecurity.ui.rmi;
|
||||
package net.sf.acegisecurity.context.rmi;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
@@ -21,8 +21,10 @@ import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.MockMethodInvocation;
|
||||
import net.sf.acegisecurity.TargetObject;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.context.SecureContextImpl;
|
||||
import net.sf.acegisecurity.context.rmi.ContextPropagatingRemoteInvocation;
|
||||
import net.sf.acegisecurity.context.rmi.ContextPropagatingRemoteInvocationFactory;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContextImpl;
|
||||
import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;
|
||||
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
+3
-3
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -28,8 +28,8 @@ import net.sf.acegisecurity.SecurityConfig;
|
||||
import net.sf.acegisecurity.TargetObject;
|
||||
import net.sf.acegisecurity.acl.basic.SomeDomain;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.context.SecureContextImpl;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContextImpl;
|
||||
import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
+3
-3
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -35,8 +35,8 @@ import net.sf.acegisecurity.MockRunAsManager;
|
||||
import net.sf.acegisecurity.RunAsManager;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.ContextImpl;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.context.SecureContextImpl;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContextImpl;
|
||||
import net.sf.acegisecurity.intercept.method.AbstractMethodDefinitionSource;
|
||||
import net.sf.acegisecurity.intercept.method.MockMethodDefinitionSource;
|
||||
import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;
|
||||
|
||||
+3
-3
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -27,8 +27,8 @@ import net.sf.acegisecurity.MockJoinPoint;
|
||||
import net.sf.acegisecurity.MockRunAsManager;
|
||||
import net.sf.acegisecurity.TargetObject;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.context.SecureContextImpl;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContextImpl;
|
||||
import net.sf.acegisecurity.intercept.method.MethodDefinitionMap;
|
||||
import net.sf.acegisecurity.intercept.method.MethodDefinitionSourceEditor;
|
||||
import net.sf.acegisecurity.providers.TestingAuthenticationToken;
|
||||
|
||||
+3
-3
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -34,8 +34,8 @@ import net.sf.acegisecurity.MockRunAsManager;
|
||||
import net.sf.acegisecurity.RunAsManager;
|
||||
import net.sf.acegisecurity.SecurityConfig;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.context.SecureContextImpl;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContextImpl;
|
||||
import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -26,8 +26,8 @@ import net.sf.acegisecurity.acl.AclManager;
|
||||
import net.sf.acegisecurity.acl.basic.MockAclObjectIdentity;
|
||||
import net.sf.acegisecurity.acl.basic.SimpleAclEntry;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.context.SecureContextImpl;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContextImpl;
|
||||
import net.sf.acegisecurity.providers.TestingAuthenticationToken;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -20,8 +20,8 @@ import junit.framework.TestCase;
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.GrantedAuthority;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.context.SecureContextImpl;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContextImpl;
|
||||
import net.sf.acegisecurity.providers.TestingAuthenticationToken;
|
||||
import net.sf.acegisecurity.providers.dao.User;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -20,7 +20,7 @@ import junit.framework.TestCase;
|
||||
import net.sf.acegisecurity.GrantedAuthority;
|
||||
import net.sf.acegisecurity.GrantedAuthorityImpl;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContextImpl;
|
||||
import net.sf.acegisecurity.context.security.SecureContextImpl;
|
||||
import net.sf.acegisecurity.providers.TestingAuthenticationToken;
|
||||
|
||||
import javax.servlet.jsp.JspException;
|
||||
|
||||
+31
-19
@@ -1,16 +1,34 @@
|
||||
/* Copyright 2004, 2005 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 net.sf.acegisecurity.taglibs.authz;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import net.sf.acegisecurity.GrantedAuthority;
|
||||
import net.sf.acegisecurity.GrantedAuthorityImpl;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContextImpl;
|
||||
import net.sf.acegisecurity.context.security.SecureContextImpl;
|
||||
import net.sf.acegisecurity.providers.TestingAuthenticationToken;
|
||||
|
||||
import org.springframework.mock.web.MockPageContext;
|
||||
|
||||
import javax.servlet.jsp.JspException;
|
||||
import javax.servlet.jsp.tagext.Tag;
|
||||
|
||||
|
||||
/**
|
||||
* Test case to implement commons-el expression language expansion.
|
||||
*/
|
||||
@@ -18,40 +36,37 @@ public class AuthorizeTagExpressionLanguageTests extends TestCase {
|
||||
//~ Instance fields ========================================================
|
||||
|
||||
private final AuthorizeTag authorizeTag = new AuthorizeTag();
|
||||
private MockPageContext pageContext;
|
||||
private SecureContextImpl context;
|
||||
private TestingAuthenticationToken currentUser;
|
||||
private MockPageContext pageContext;
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
public void testAllGrantedUsesExpressionLanguageWhenExpressionIsEL()
|
||||
throws JspException {
|
||||
throws JspException {
|
||||
pageContext.setAttribute("authority", "ROLE_TELLER");
|
||||
authorizeTag.setIfAllGranted("${authority}");
|
||||
|
||||
assertEquals(
|
||||
"allows body - authority var contains ROLE_TELLER",
|
||||
Tag.EVAL_BODY_INCLUDE, authorizeTag.doStartTag());
|
||||
assertEquals("allows body - authority var contains ROLE_TELLER",
|
||||
Tag.EVAL_BODY_INCLUDE, authorizeTag.doStartTag());
|
||||
}
|
||||
|
||||
public void testAnyGrantedUsesExpressionLanguageWhenExpressionIsEL()
|
||||
throws JspException {
|
||||
throws JspException {
|
||||
pageContext.setAttribute("authority", "ROLE_TELLER");
|
||||
authorizeTag.setIfAnyGranted("${authority}");
|
||||
|
||||
assertEquals(
|
||||
"allows body - authority var contains ROLE_TELLER",
|
||||
Tag.EVAL_BODY_INCLUDE, authorizeTag.doStartTag());
|
||||
assertEquals("allows body - authority var contains ROLE_TELLER",
|
||||
Tag.EVAL_BODY_INCLUDE, authorizeTag.doStartTag());
|
||||
}
|
||||
|
||||
public void testNotGrantedUsesExpressionLanguageWhenExpressionIsEL()
|
||||
throws JspException {
|
||||
throws JspException {
|
||||
pageContext.setAttribute("authority", "ROLE_TELLER");
|
||||
authorizeTag.setIfNotGranted("${authority}");
|
||||
|
||||
assertEquals(
|
||||
"allows body - authority var contains ROLE_TELLER",
|
||||
Tag.SKIP_BODY, authorizeTag.doStartTag());
|
||||
assertEquals("allows body - authority var contains ROLE_TELLER",
|
||||
Tag.SKIP_BODY, authorizeTag.doStartTag());
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
@@ -60,11 +75,8 @@ public class AuthorizeTagExpressionLanguageTests extends TestCase {
|
||||
pageContext = new MockPageContext();
|
||||
authorizeTag.setPageContext(pageContext);
|
||||
|
||||
currentUser = new TestingAuthenticationToken(
|
||||
"abc", "123",
|
||||
new GrantedAuthority[]{
|
||||
new GrantedAuthorityImpl("ROLE_TELLER"),
|
||||
});
|
||||
currentUser = new TestingAuthenticationToken("abc", "123",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_TELLER"),});
|
||||
|
||||
context = new SecureContextImpl();
|
||||
context.setAuthentication(currentUser);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -20,7 +20,7 @@ import junit.framework.TestCase;
|
||||
import net.sf.acegisecurity.GrantedAuthority;
|
||||
import net.sf.acegisecurity.GrantedAuthorityImpl;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContextImpl;
|
||||
import net.sf.acegisecurity.context.security.SecureContextImpl;
|
||||
import net.sf.acegisecurity.providers.TestingAuthenticationToken;
|
||||
|
||||
import javax.servlet.jsp.JspException;
|
||||
|
||||
@@ -1,308 +0,0 @@
|
||||
/* Copyright 2004 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 net.sf.acegisecurity.ui;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.GrantedAuthority;
|
||||
import net.sf.acegisecurity.GrantedAuthorityImpl;
|
||||
import net.sf.acegisecurity.adapters.MockPrincipal;
|
||||
import net.sf.acegisecurity.adapters.PrincipalAcegiUserToken;
|
||||
import net.sf.acegisecurity.context.Context;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.context.SecureContextImpl;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
|
||||
|
||||
/**
|
||||
* Tests {@link AbstractIntegrationFilter}.
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
*/
|
||||
public class AbstractIntegrationFilterTests extends TestCase {
|
||||
//~ Constructors ===========================================================
|
||||
|
||||
public AbstractIntegrationFilterTests() {
|
||||
super();
|
||||
}
|
||||
|
||||
public AbstractIntegrationFilterTests(String arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
public final void setUp() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
junit.textui.TestRunner.run(AbstractIntegrationFilterTests.class);
|
||||
}
|
||||
|
||||
public void testContextHolderContentsPreserved() throws Exception {
|
||||
PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key",
|
||||
"someone", "password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")});
|
||||
MockAbstractIntegrationFilterImpl filter = new MockAbstractIntegrationFilterImpl(principal);
|
||||
MockFilterChain chain = new MockFilterChain(true, principal);
|
||||
|
||||
MockSecureContextImpl secureContext = new MockSecureContextImpl(
|
||||
"FOO_BAR");
|
||||
ContextHolder.setContext(secureContext);
|
||||
assertEquals(secureContext, ContextHolder.getContext());
|
||||
|
||||
executeFilterInContainerSimulator(filter, null, null, chain);
|
||||
|
||||
MockSecureContextImpl after = (MockSecureContextImpl) ContextHolder
|
||||
.getContext();
|
||||
assertEquals(secureContext.getInfo(), after.getInfo());
|
||||
ContextHolder.setContext(null);
|
||||
}
|
||||
|
||||
public void testContextHolderHasAuthenticationRemoved()
|
||||
throws Exception {
|
||||
PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key",
|
||||
"someone", "password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")});
|
||||
MockAbstractIntegrationFilterImpl filter = new MockAbstractIntegrationFilterImpl(principal);
|
||||
MockFilterChain chain = new MockFilterChain(true, principal);
|
||||
|
||||
SecureContext secureContext = new SecureContextImpl();
|
||||
secureContext.setAuthentication(principal);
|
||||
ContextHolder.setContext(secureContext);
|
||||
assertEquals(secureContext, ContextHolder.getContext());
|
||||
|
||||
executeFilterInContainerSimulator(filter, null, null, chain);
|
||||
|
||||
SecureContext after = (SecureContext) ContextHolder.getContext();
|
||||
assertEquals(null, after.getAuthentication());
|
||||
ContextHolder.setContext(null);
|
||||
}
|
||||
|
||||
public void testIgnoredWhenConcreteClassReturnsANonAuthenticationObject()
|
||||
throws Exception {
|
||||
MockPrincipal principal = new MockPrincipal();
|
||||
MockAbstractIntegrationFilterImpl filter = new MockAbstractIntegrationFilterImpl(principal);
|
||||
MockFilterChain chain = new MockFilterChain(false, null);
|
||||
|
||||
Context before = ContextHolder.getContext();
|
||||
|
||||
if (before != null) {
|
||||
if (before instanceof SecureContext) {
|
||||
assertEquals(null, ((SecureContext) before).getAuthentication());
|
||||
}
|
||||
}
|
||||
|
||||
executeFilterInContainerSimulator(filter, null, null, chain);
|
||||
|
||||
Context after = ContextHolder.getContext();
|
||||
|
||||
if (after != null) {
|
||||
if (after instanceof SecureContext) {
|
||||
assertEquals(null, ((SecureContext) after).getAuthentication());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testIgnoredWhenConcreteClassReturnsNullAuthenticationObject()
|
||||
throws Exception {
|
||||
MockAbstractIntegrationFilterImpl filter = new MockAbstractIntegrationFilterImpl(null);
|
||||
MockFilterChain chain = new MockFilterChain(false, null);
|
||||
|
||||
Context before = ContextHolder.getContext();
|
||||
|
||||
if (before != null) {
|
||||
if (before instanceof SecureContext) {
|
||||
assertEquals(null, ((SecureContext) before).getAuthentication());
|
||||
}
|
||||
}
|
||||
|
||||
executeFilterInContainerSimulator(filter, null, null, chain);
|
||||
|
||||
Context after = ContextHolder.getContext();
|
||||
|
||||
if (after != null) {
|
||||
if (after instanceof SecureContext) {
|
||||
assertEquals(null, ((SecureContext) after).getAuthentication());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testRejectsInvalidSecureContextClass()
|
||||
throws Exception {
|
||||
MockAbstractIntegrationFilterImpl filter = new MockAbstractIntegrationFilterImpl(null);
|
||||
|
||||
// Test rejects classes not implementing SecureContext
|
||||
filter.setSecureContext(String.class);
|
||||
|
||||
try {
|
||||
filter.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
// Test accepts classes implementing SecureContext
|
||||
filter.setSecureContext(SecureContextImpl.class);
|
||||
filter.afterPropertiesSet();
|
||||
assertTrue(true);
|
||||
|
||||
// Test rejects null
|
||||
filter.setSecureContext(null);
|
||||
|
||||
try {
|
||||
filter.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testSecureContextSettersGetters() throws Exception {
|
||||
MockAbstractIntegrationFilterImpl filter = new MockAbstractIntegrationFilterImpl(null);
|
||||
|
||||
// check the default
|
||||
assertEquals(SecureContextImpl.class, filter.getSecureContext());
|
||||
|
||||
// check the setter
|
||||
filter.setSecureContext(null);
|
||||
assertNull(filter.getSecureContext());
|
||||
}
|
||||
|
||||
public void testSuccessWhenConcreteClassReturnsValidAuthenticationObject()
|
||||
throws Exception {
|
||||
PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key",
|
||||
"someone", "password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")});
|
||||
MockAbstractIntegrationFilterImpl filter = new MockAbstractIntegrationFilterImpl(principal);
|
||||
MockFilterChain chain = new MockFilterChain(true, principal);
|
||||
|
||||
Context before = ContextHolder.getContext();
|
||||
|
||||
if (before != null) {
|
||||
if (before instanceof SecureContext) {
|
||||
assertEquals(null, ((SecureContext) before).getAuthentication());
|
||||
}
|
||||
}
|
||||
|
||||
executeFilterInContainerSimulator(filter, null, null, chain);
|
||||
|
||||
Context after = ContextHolder.getContext();
|
||||
|
||||
if (after != null) {
|
||||
if (after instanceof SecureContext) {
|
||||
assertEquals(null, ((SecureContext) after).getAuthentication());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void executeFilterInContainerSimulator(Filter filter,
|
||||
ServletRequest request, ServletResponse response,
|
||||
FilterChain filterChain) throws ServletException, IOException {
|
||||
filter.init(null);
|
||||
filter.doFilter(request, response, filterChain);
|
||||
filter.destroy();
|
||||
}
|
||||
|
||||
//~ Inner Classes ==========================================================
|
||||
|
||||
private class MockAbstractIntegrationFilterImpl
|
||||
extends AbstractIntegrationFilter {
|
||||
private Object extractFromContainerResult;
|
||||
|
||||
public MockAbstractIntegrationFilterImpl(
|
||||
Object extractFromContainerResult) {
|
||||
this.extractFromContainerResult = extractFromContainerResult;
|
||||
}
|
||||
|
||||
private MockAbstractIntegrationFilterImpl() {
|
||||
super();
|
||||
}
|
||||
|
||||
public void commitToContainer(ServletRequest request,
|
||||
Authentication authentication) {
|
||||
this.extractFromContainerResult = authentication;
|
||||
}
|
||||
|
||||
public Object extractFromContainer(ServletRequest request) {
|
||||
return this.extractFromContainerResult;
|
||||
}
|
||||
}
|
||||
|
||||
private class MockFilterChain implements FilterChain {
|
||||
private Authentication expectedAuthenticationObjectInContextHolder;
|
||||
private boolean expectContextHolderContainSecureContext = false;
|
||||
|
||||
public MockFilterChain(
|
||||
boolean expectContextHolderContainSecureContext,
|
||||
Authentication expectedAuthenticationObjectInContextHolder) {
|
||||
if ((expectedAuthenticationObjectInContextHolder != null)
|
||||
&& !expectContextHolderContainSecureContext) {
|
||||
throw new IllegalArgumentException(
|
||||
"If an Authentication object is expected, the ContextHolder should contain a SecureContext");
|
||||
}
|
||||
|
||||
this.expectContextHolderContainSecureContext = expectContextHolderContainSecureContext;
|
||||
this.expectedAuthenticationObjectInContextHolder = expectedAuthenticationObjectInContextHolder;
|
||||
}
|
||||
|
||||
private MockFilterChain() {
|
||||
super();
|
||||
}
|
||||
|
||||
public void doFilter(ServletRequest request, ServletResponse response)
|
||||
throws IOException, ServletException {
|
||||
if (expectContextHolderContainSecureContext) {
|
||||
Context context = ContextHolder.getContext();
|
||||
|
||||
if (!(context instanceof SecureContext)) {
|
||||
fail("ContextHolder should have contained SecureContext");
|
||||
}
|
||||
} else {
|
||||
if (ContextHolder.getContext() != null) {
|
||||
fail("ContextHolder should have been null but wasn't");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class MockSecureContextImpl extends SecureContextImpl {
|
||||
private String info;
|
||||
|
||||
public MockSecureContextImpl(String info) {
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
private MockSecureContextImpl() {
|
||||
super();
|
||||
}
|
||||
|
||||
public String getInfo() {
|
||||
return this.info;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -26,8 +26,10 @@ import net.sf.acegisecurity.MockAuthenticationManager;
|
||||
import net.sf.acegisecurity.MockFilterConfig;
|
||||
import net.sf.acegisecurity.MockHttpServletRequest;
|
||||
import net.sf.acegisecurity.MockHttpServletResponse;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.security.SecureContextImpl;
|
||||
import net.sf.acegisecurity.context.security.SecureContextUtils;
|
||||
import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;
|
||||
import net.sf.acegisecurity.ui.webapp.HttpSessionIntegrationFilter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -59,10 +61,6 @@ public class AbstractProcessingFilterTests extends TestCase {
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
public final void setUp() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
junit.textui.TestRunner.run(AbstractProcessingFilterTests.class);
|
||||
}
|
||||
@@ -117,7 +115,7 @@ public class AbstractProcessingFilterTests extends TestCase {
|
||||
executeFilterInContainerSimulator(config, filter, request, response,
|
||||
chain);
|
||||
assertEquals("/myApp/failed.jsp", response.getRedirect());
|
||||
assertTrue(request.getSession().getAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY) == null);
|
||||
assertNull(SecureContextUtils.getSecureContext().getAuthentication());
|
||||
}
|
||||
|
||||
public void testFilterProcessesUrlVariationsRespected()
|
||||
@@ -144,10 +142,10 @@ public class AbstractProcessingFilterTests extends TestCase {
|
||||
executeFilterInContainerSimulator(config, filter, request, response,
|
||||
chain);
|
||||
assertEquals("/logged_in.jsp", response.getRedirect());
|
||||
assertTrue(request.getSession().getAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY) != null);
|
||||
assertNotNull(SecureContextUtils.getSecureContext().getAuthentication());
|
||||
assertEquals("test",
|
||||
((Authentication) request.getSession().getAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY)).getPrincipal()
|
||||
.toString());
|
||||
SecureContextUtils.getSecureContext().getAuthentication()
|
||||
.getPrincipal().toString());
|
||||
}
|
||||
|
||||
public void testGettersSetters() {
|
||||
@@ -233,10 +231,10 @@ public class AbstractProcessingFilterTests extends TestCase {
|
||||
executeFilterInContainerSimulator(config, filter, request, response,
|
||||
chain);
|
||||
assertEquals("/logged_in.jsp", response.getRedirect());
|
||||
assertTrue(request.getSession().getAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY) != null);
|
||||
assertNotNull(SecureContextUtils.getSecureContext().getAuthentication());
|
||||
assertEquals("test",
|
||||
((Authentication) request.getSession().getAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY)).getPrincipal()
|
||||
.toString());
|
||||
SecureContextUtils.getSecureContext().getAuthentication()
|
||||
.getPrincipal().toString());
|
||||
}
|
||||
|
||||
public void testStartupDetectsInvalidAuthenticationFailureUrl()
|
||||
@@ -327,10 +325,10 @@ public class AbstractProcessingFilterTests extends TestCase {
|
||||
executeFilterInContainerSimulator(config, filter, request, response,
|
||||
chain);
|
||||
assertEquals("/logged_in.jsp", response.getRedirect());
|
||||
assertTrue(request.getSession().getAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY) != null);
|
||||
assertNotNull(SecureContextUtils.getSecureContext().getAuthentication());
|
||||
assertEquals("test",
|
||||
((Authentication) request.getSession().getAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY)).getPrincipal()
|
||||
.toString());
|
||||
SecureContextUtils.getSecureContext().getAuthentication()
|
||||
.getPrincipal().toString());
|
||||
|
||||
// Now try again but this time have filter deny access
|
||||
// Setup our HTTP request
|
||||
@@ -346,7 +344,7 @@ public class AbstractProcessingFilterTests extends TestCase {
|
||||
// Test
|
||||
executeFilterInContainerSimulator(config, filter, request, response,
|
||||
chain);
|
||||
assertTrue(request.getSession().getAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY) == null);
|
||||
assertNull(SecureContextUtils.getSecureContext().getAuthentication());
|
||||
}
|
||||
|
||||
public void testSuccessfulAuthenticationButWithAlwaysUseDefaultTargetUrlCausesRedirectToDefaultTargetUrl()
|
||||
@@ -377,7 +375,7 @@ public class AbstractProcessingFilterTests extends TestCase {
|
||||
executeFilterInContainerSimulator(config, filter, request, response,
|
||||
chain);
|
||||
assertEquals("/foobar", response.getRedirect());
|
||||
assertTrue(request.getSession().getAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY) != null);
|
||||
assertNotNull(SecureContextUtils.getSecureContext().getAuthentication());
|
||||
}
|
||||
|
||||
public void testSuccessfulAuthenticationCausesRedirectToSessionSpecifiedUrl()
|
||||
@@ -404,7 +402,17 @@ public class AbstractProcessingFilterTests extends TestCase {
|
||||
executeFilterInContainerSimulator(config, filter, request, response,
|
||||
chain);
|
||||
assertEquals("/my-destination", response.getRedirect());
|
||||
assertTrue(request.getSession().getAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY) != null);
|
||||
assertNotNull(SecureContextUtils.getSecureContext().getAuthentication());
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
ContextHolder.setContext(new SecureContextImpl());
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
ContextHolder.setContext(null);
|
||||
}
|
||||
|
||||
private void executeFilterInContainerSimulator(FilterConfig filterConfig,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -17,7 +17,6 @@ package net.sf.acegisecurity.ui.basicauth;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.MockAuthenticationEntryPoint;
|
||||
import net.sf.acegisecurity.MockAuthenticationManager;
|
||||
import net.sf.acegisecurity.MockFilterConfig;
|
||||
@@ -25,7 +24,9 @@ import net.sf.acegisecurity.MockHttpServletRequest;
|
||||
import net.sf.acegisecurity.MockHttpServletResponse;
|
||||
import net.sf.acegisecurity.MockHttpSession;
|
||||
import net.sf.acegisecurity.UserDetails;
|
||||
import net.sf.acegisecurity.ui.webapp.HttpSessionIntegrationFilter;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.security.SecureContextImpl;
|
||||
import net.sf.acegisecurity.context.security.SecureContextUtils;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
@@ -64,10 +65,6 @@ public class BasicProcessingFilterTests extends TestCase {
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
public final void setUp() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
junit.textui.TestRunner.run(BasicProcessingFilterTests.class);
|
||||
}
|
||||
@@ -125,7 +122,7 @@ public class BasicProcessingFilterTests extends TestCase {
|
||||
executeFilterInContainerSimulator(config, filter, request, response,
|
||||
chain);
|
||||
|
||||
assertTrue(request.getSession().getAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY) == null);
|
||||
assertNull(SecureContextUtils.getSecureContext().getAuthentication());
|
||||
}
|
||||
|
||||
public void testGettersSetters() {
|
||||
@@ -167,7 +164,7 @@ public class BasicProcessingFilterTests extends TestCase {
|
||||
executeFilterInContainerSimulator(config, filter, request, response,
|
||||
chain);
|
||||
|
||||
assertTrue(request.getSession().getAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY) == null);
|
||||
assertNull(SecureContextUtils.getSecureContext().getAuthentication());
|
||||
}
|
||||
|
||||
public void testNormalOperation() throws Exception {
|
||||
@@ -198,10 +195,11 @@ public class BasicProcessingFilterTests extends TestCase {
|
||||
executeFilterInContainerSimulator(config, filter, request, response,
|
||||
chain);
|
||||
|
||||
assertTrue(request.getSession().getAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY) != null);
|
||||
assertNotNull(SecureContextUtils.getSecureContext().getAuthentication());
|
||||
assertEquals("marissa",
|
||||
((UserDetails) ((Authentication) request.getSession().getAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY))
|
||||
.getPrincipal()).getUsername());
|
||||
((UserDetails) SecureContextUtils.getSecureContext()
|
||||
.getAuthentication().getPrincipal())
|
||||
.getUsername());
|
||||
}
|
||||
|
||||
public void testOtherAuthorizationSchemeIsIgnored()
|
||||
@@ -231,7 +229,7 @@ public class BasicProcessingFilterTests extends TestCase {
|
||||
executeFilterInContainerSimulator(config, filter, request, response,
|
||||
chain);
|
||||
|
||||
assertTrue(request.getSession().getAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY) == null);
|
||||
assertNull(SecureContextUtils.getSecureContext().getAuthentication());
|
||||
}
|
||||
|
||||
public void testStartupDetectsMissingAuthenticationEntryPoint()
|
||||
@@ -290,10 +288,11 @@ public class BasicProcessingFilterTests extends TestCase {
|
||||
executeFilterInContainerSimulator(config, filter, request, response,
|
||||
chain);
|
||||
|
||||
assertTrue(request.getSession().getAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY) != null);
|
||||
assertNotNull(SecureContextUtils.getSecureContext().getAuthentication());
|
||||
assertEquals("marissa",
|
||||
((UserDetails) ((Authentication) request.getSession().getAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY))
|
||||
.getPrincipal()).getUsername());
|
||||
((UserDetails) SecureContextUtils.getSecureContext()
|
||||
.getAuthentication().getPrincipal())
|
||||
.getUsername());
|
||||
|
||||
// NOW PERFORM FAILED AUTHENTICATION
|
||||
// Setup our HTTP request
|
||||
@@ -313,7 +312,7 @@ public class BasicProcessingFilterTests extends TestCase {
|
||||
executeFilterInContainerSimulator(config, filter, request, response,
|
||||
chain);
|
||||
|
||||
assertTrue(request.getSession().getAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY) == null);
|
||||
assertNull(SecureContextUtils.getSecureContext().getAuthentication());
|
||||
assertEquals(401, response.getError());
|
||||
}
|
||||
|
||||
@@ -345,10 +344,20 @@ public class BasicProcessingFilterTests extends TestCase {
|
||||
executeFilterInContainerSimulator(config, filter, request, response,
|
||||
chain);
|
||||
|
||||
assertTrue(request.getSession().getAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY) == null);
|
||||
assertNull(SecureContextUtils.getSecureContext().getAuthentication());
|
||||
assertEquals(401, response.getError());
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
ContextHolder.setContext(new SecureContextImpl());
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
ContextHolder.setContext(null);
|
||||
}
|
||||
|
||||
private void executeFilterInContainerSimulator(FilterConfig filterConfig,
|
||||
Filter filter, ServletRequest request, ServletResponse response,
|
||||
FilterChain filterChain) throws ServletException, IOException {
|
||||
|
||||
-199
@@ -1,199 +0,0 @@
|
||||
/* Copyright 2004 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 net.sf.acegisecurity.ui.webapp;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import net.sf.acegisecurity.GrantedAuthority;
|
||||
import net.sf.acegisecurity.GrantedAuthorityImpl;
|
||||
import net.sf.acegisecurity.MockHttpServletRequest;
|
||||
import net.sf.acegisecurity.MockHttpSession;
|
||||
import net.sf.acegisecurity.adapters.PrincipalAcegiUserToken;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
|
||||
/**
|
||||
* Tests {@link HttpSessionIntegrationFilter}.
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
*/
|
||||
public class HttpSessionIntegrationFilterTests extends TestCase {
|
||||
//~ Constructors ===========================================================
|
||||
|
||||
public HttpSessionIntegrationFilterTests() {
|
||||
super();
|
||||
}
|
||||
|
||||
public HttpSessionIntegrationFilterTests(String arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
public final void setUp() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
junit.textui.TestRunner.run(HttpSessionIntegrationFilterTests.class);
|
||||
}
|
||||
|
||||
public void testCommitFailSilentlyIfNullsProvided() {
|
||||
HttpSessionIntegrationFilter filter = new HttpSessionIntegrationFilter();
|
||||
filter.commitToContainer(null, null);
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
public void testCommitOperation() {
|
||||
// Build an Authentication object we want returned
|
||||
PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key",
|
||||
"someone", "password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")});
|
||||
|
||||
// Build a mock request
|
||||
MockHttpSession session = new MockHttpSession();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(null,
|
||||
session);
|
||||
|
||||
// Try to commit
|
||||
HttpSessionIntegrationFilter filter = new HttpSessionIntegrationFilter();
|
||||
filter.commitToContainer(request, principal);
|
||||
|
||||
// Check it committed the object
|
||||
Object result = session.getAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY);
|
||||
assertEquals(principal, result);
|
||||
}
|
||||
|
||||
public void testCommitOperationGracefullyIgnoredIfSessionIsNull() {
|
||||
PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key",
|
||||
"someone", "password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")});
|
||||
|
||||
// Build a mock request
|
||||
MockHttpSession session = null;
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(null,
|
||||
session);
|
||||
|
||||
HttpSessionIntegrationFilter filter = new HttpSessionIntegrationFilter();
|
||||
filter.commitToContainer(request, principal);
|
||||
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
public void testCorrectOperation() {
|
||||
// Build a mock session containing the authenticated user
|
||||
PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key",
|
||||
"someone", "password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")});
|
||||
MockHttpSession session = new MockHttpSession();
|
||||
session.setAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY,
|
||||
principal);
|
||||
|
||||
// Confirm filter can extract required credentials from session
|
||||
HttpSessionIntegrationFilter filter = new HttpSessionIntegrationFilter();
|
||||
Object result = filter.extractFromContainer(new MockHttpServletRequest(
|
||||
null, session));
|
||||
|
||||
if (!(result instanceof PrincipalAcegiUserToken)) {
|
||||
fail("Should have returned PrincipalAcegiUserToken");
|
||||
}
|
||||
|
||||
PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) result;
|
||||
assertEquals(principal, result);
|
||||
}
|
||||
|
||||
public void testDetectsInvalidAdditionalAttributes() {
|
||||
HttpSessionIntegrationFilter filter = new HttpSessionIntegrationFilter();
|
||||
List list = new Vector();
|
||||
list.add(new Integer(4));
|
||||
|
||||
try {
|
||||
filter.setAdditionalAttributes(list);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testHandlesIfHttpRequestIsNullForSomeReason() {
|
||||
HttpSessionIntegrationFilter filter = new HttpSessionIntegrationFilter();
|
||||
assertEquals(null, filter.extractFromContainer(null));
|
||||
}
|
||||
|
||||
public void testHandlesIfHttpSessionIsNullForSomeReason() {
|
||||
HttpSessionIntegrationFilter filter = new HttpSessionIntegrationFilter();
|
||||
assertEquals(null,
|
||||
filter.extractFromContainer(new MockHttpServletRequest(null, null)));
|
||||
}
|
||||
|
||||
public void testHandlesIfThereIsNoPrincipalInTheHttpSession() {
|
||||
HttpSessionIntegrationFilter filter = new HttpSessionIntegrationFilter();
|
||||
assertEquals(null,
|
||||
filter.extractFromContainer(
|
||||
new MockHttpServletRequest(null, new MockHttpSession())));
|
||||
}
|
||||
|
||||
public void testSettingEmptyListForAdditionalAttributesIsAcceptable() {
|
||||
HttpSessionIntegrationFilter filter = new HttpSessionIntegrationFilter();
|
||||
filter.setAdditionalAttributes(new Vector());
|
||||
assertTrue(filter.getAdditionalAttributes() != null);
|
||||
}
|
||||
|
||||
public void testSettingNullForAdditionalAttributesIsAcceptable() {
|
||||
HttpSessionIntegrationFilter filter = new HttpSessionIntegrationFilter();
|
||||
filter.setAdditionalAttributes(null);
|
||||
assertNull(filter.getAdditionalAttributes());
|
||||
}
|
||||
|
||||
public void testUpdatesAdditionalAttributes() {
|
||||
// Build a mock session containing the authenticated user
|
||||
PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key",
|
||||
"someone", "password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")});
|
||||
MockHttpSession session = new MockHttpSession();
|
||||
session.setAttribute(HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY,
|
||||
principal);
|
||||
|
||||
// Check our attributes are not presently set
|
||||
assertNull(session.getAttribute("SOME_EXTRA_ATTRIBUTE_1"));
|
||||
assertNull(session.getAttribute("SOME_EXTRA_ATTRIBUTE_2"));
|
||||
|
||||
// Generate filter
|
||||
HttpSessionIntegrationFilter filter = new HttpSessionIntegrationFilter();
|
||||
List list = new Vector();
|
||||
list.add("SOME_EXTRA_ATTRIBUTE_1");
|
||||
list.add("SOME_EXTRA_ATTRIBUTE_2");
|
||||
filter.setAdditionalAttributes(list);
|
||||
|
||||
// Confirm filter can extract required credentials from session
|
||||
Object result = filter.extractFromContainer(new MockHttpServletRequest(
|
||||
null, session));
|
||||
|
||||
if (!(result instanceof PrincipalAcegiUserToken)) {
|
||||
fail("Should have returned PrincipalAcegiUserToken");
|
||||
}
|
||||
|
||||
PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) result;
|
||||
assertEquals(principal, result);
|
||||
|
||||
// Now double-check it updated our earlier set additionalAttributes
|
||||
assertEquals(principal, session.getAttribute("SOME_EXTRA_ATTRIBUTE_1"));
|
||||
assertEquals(principal, session.getAttribute("SOME_EXTRA_ATTRIBUTE_2"));
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 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.
|
||||
@@ -13,12 +13,14 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.sf.acegisecurity.ui.wrapper;
|
||||
package net.sf.acegisecurity.wrapper;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import net.sf.acegisecurity.MockFilterConfig;
|
||||
import net.sf.acegisecurity.MockHttpServletRequest;
|
||||
import net.sf.acegisecurity.wrapper.ContextHolderAwareRequestFilter;
|
||||
import net.sf.acegisecurity.wrapper.ContextHolderAwareRequestWrapper;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
+4
-3
@@ -13,7 +13,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.sf.acegisecurity.ui.wrapper;
|
||||
package net.sf.acegisecurity.wrapper;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
@@ -22,10 +22,11 @@ import net.sf.acegisecurity.GrantedAuthority;
|
||||
import net.sf.acegisecurity.GrantedAuthorityImpl;
|
||||
import net.sf.acegisecurity.MockHttpServletRequest;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.context.SecureContextImpl;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContextImpl;
|
||||
import net.sf.acegisecurity.providers.TestingAuthenticationToken;
|
||||
import net.sf.acegisecurity.providers.dao.User;
|
||||
import net.sf.acegisecurity.wrapper.ContextHolderAwareRequestWrapper;
|
||||
|
||||
|
||||
/**
|
||||
Reference in New Issue
Block a user