Store failed Authentication request in AuthenticationException, using template pattern (patch thanks to Wesley Hall).

This commit is contained in:
Ben Alex 2004-07-24 07:18:04 +00:00
parent 21a98eb41f
commit 951c1a02df
3 changed files with 84 additions and 6 deletions

View File

@ -0,0 +1,75 @@
/* 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;
/**
* An abstract implementation of the {@link AuthenticationManager}.
*
* @author Wesley Hall
* @version $Id$
*/
public abstract class AbstractAuthenticationManager
implements AuthenticationManager {
//~ Methods ================================================================
/**
* <p>
* An implementation of the <code>authenticate</code> method that calls the
* abstract method <code>doAuthenticatation</code> to do its work.
* </p>
*
* <p>
* If doAuthenticate throws an <code>AuthenticationException</code> then
* the exception is populated with the failed <code>Authentication</code>
* object that failed.
* </p>
*
* @param authentication the authentication request object
*
* @return a fully authenticated object including credentials
*
* @throws AuthenticationException if authentication fails
*/
public final Authentication authenticate(Authentication authentication)
throws AuthenticationException {
try {
return doAuthentication(authentication);
} catch (AuthenticationException e) {
e.setAuthentication(authentication);
throw e;
}
}
/**
* <p>
* Concrete implementations of this class override this method to provide
* the authentication service.
* </p>
*
* <p>
* The contract for this method is documented in the {@link
* AuthenticationManager#authenticate(net.sf.acegisecurity.Authentication)}.
* </p>
*
* @param authentication the authentication request object
*
* @return a fully authenticated object including credentials
*
* @throws AuthenticationException if authentication fails
*/
protected abstract Authentication doAuthentication(
Authentication authentication) throws AuthenticationException;
}

View File

@ -15,9 +15,9 @@
package net.sf.acegisecurity.providers; package net.sf.acegisecurity.providers;
import net.sf.acegisecurity.AbstractAuthenticationManager;
import net.sf.acegisecurity.Authentication; import net.sf.acegisecurity.Authentication;
import net.sf.acegisecurity.AuthenticationException; import net.sf.acegisecurity.AuthenticationException;
import net.sf.acegisecurity.AuthenticationManager;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -29,13 +29,15 @@ import java.util.List;
/** /**
* Iterates an {@link Authentication} request through a list of {@link * Iterates an {@link Authentication} request through a list of {@link
* AuthenticationProvider}s. * AuthenticationProvider}s.
* *
* @author Ben Alex * @author Ben Alex
* @author Wesley Hall
* @version $Id$ * @version $Id$
*/ */
public class ProviderManager implements InitializingBean, AuthenticationManager { public class ProviderManager extends AbstractAuthenticationManager
implements InitializingBean {
//~ Static fields/initializers ============================================= //~ Static fields/initializers =============================================
private static final Log logger = LogFactory.getLog(ProviderManager.class); private static final Log logger = LogFactory.getLog(ProviderManager.class);
@ -109,7 +111,7 @@ public class ProviderManager implements InitializingBean, AuthenticationManager
* @throws AuthenticationException if authentication fails. * @throws AuthenticationException if authentication fails.
* @throws ProviderNotFoundException DOCUMENT ME! * @throws ProviderNotFoundException DOCUMENT ME!
*/ */
public Authentication authenticate(Authentication authentication) public Authentication doAuthentication(Authentication authentication)
throws AuthenticationException { throws AuthenticationException {
Iterator iter = providers.iterator(); Iterator iter = providers.iterator();

View File

@ -20,9 +20,10 @@ package net.sf.acegisecurity;
* <code>grantAccess</code> is set to <code>true</code>. * <code>grantAccess</code> is set to <code>true</code>.
* *
* @author Ben Alex * @author Ben Alex
* @author Wesley Hall
* @version $Id$ * @version $Id$
*/ */
public class MockAuthenticationManager implements AuthenticationManager { public class MockAuthenticationManager extends AbstractAuthenticationManager {
//~ Instance fields ======================================================== //~ Instance fields ========================================================
private boolean grantAccess = true; private boolean grantAccess = true;
@ -39,7 +40,7 @@ public class MockAuthenticationManager implements AuthenticationManager {
//~ Methods ================================================================ //~ Methods ================================================================
public Authentication authenticate(Authentication authentication) public Authentication doAuthentication(Authentication authentication)
throws AuthenticationException { throws AuthenticationException {
if (grantAccess) { if (grantAccess) {
return authentication; return authentication;