Enhanced error detection at startup time. Added support for handling null usernames and passwords.

This commit is contained in:
Ben Alex 2004-03-28 12:01:51 +00:00
parent 2e1b4b4ffc
commit 68ee9aaabb
1 changed files with 37 additions and 19 deletions

View File

@ -12,7 +12,6 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package net.sf.acegisecurity.adapters.jboss; package net.sf.acegisecurity.adapters.jboss;
import net.sf.acegisecurity.Authentication; import net.sf.acegisecurity.Authentication;
@ -56,23 +55,34 @@ import javax.security.auth.login.LoginException;
* @version $Id$ * @version $Id$
*/ */
public class JbossAcegiLoginModule extends AbstractServerLoginModule { public class JbossAcegiLoginModule extends AbstractServerLoginModule {
//~ Instance fields ========================================================
private AuthenticationManager authenticationManager; private AuthenticationManager authenticationManager;
private Principal identity; private Principal identity;
private String key; private String key;
private char[] credential; private char[] credential;
//~ Methods ================================================================
public void initialize(Subject subject, CallbackHandler callbackHandler, public void initialize(Subject subject, CallbackHandler callbackHandler,
Map sharedState, Map options) { Map sharedState, Map options) {
super.initialize(subject, callbackHandler, sharedState, options); super.initialize(subject, callbackHandler, sharedState, options);
this.key = (String) options.get("key"); this.key = (String) options.get("key");
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext((String) options if ((key == null) || "".equals(key)) {
.get("appContextLocation")); throw new IllegalArgumentException("key must be defined");
}
String appContextLocation = (String) options.get("appContextLocation");
if ((appContextLocation == null) || "".equals(appContextLocation)) {
throw new IllegalArgumentException(
"appContextLocation must be defined");
}
if (Thread.currentThread().getContextClassLoader().getResource(appContextLocation) == null) {
throw new IllegalArgumentException("Cannot locate " +
appContextLocation);
}
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(appContextLocation);
Map beans = ctx.getBeansOfType(AuthenticationManager.class, true, true); Map beans = ctx.getBeansOfType(AuthenticationManager.class, true, true);
if (beans.size() == 0) { if (beans.size() == 0) {
@ -94,8 +104,16 @@ public class JbossAcegiLoginModule extends AbstractServerLoginModule {
if ((username == null) && (password == null)) { if ((username == null) && (password == null)) {
identity = null; identity = null;
super.log.trace("Authenticating as unauthenticatedIdentity=" super.log.trace("Authenticating as unauthenticatedIdentity=" +
+ identity); identity);
}
if (username == null) {
username = "";
}
if (password == null) {
password = "";
} }
if (identity == null) { if (identity == null) {
@ -127,8 +145,8 @@ public class JbossAcegiLoginModule extends AbstractServerLoginModule {
} }
super.loginOk = true; super.loginOk = true;
super.log.trace("User '" + identity + "' authenticated, loginOk=" super.log.trace("User '" + identity + "' authenticated, loginOk=" +
+ loginOk); loginOk);
return true; return true;
} }
@ -158,8 +176,8 @@ public class JbossAcegiLoginModule extends AbstractServerLoginModule {
// prompt for a username and password // prompt for a username and password
if (callbackHandler == null) { if (callbackHandler == null) {
throw new LoginException("Error: no CallbackHandler available " throw new LoginException("Error: no CallbackHandler available " +
+ "to collect authentication information"); "to collect authentication information");
} }
NameCallback nc = new NameCallback("User name: ", "guest"); NameCallback nc = new NameCallback("User name: ", "guest");
@ -184,8 +202,8 @@ public class JbossAcegiLoginModule extends AbstractServerLoginModule {
} catch (java.io.IOException ioe) { } catch (java.io.IOException ioe) {
throw new LoginException(ioe.toString()); throw new LoginException(ioe.toString());
} catch (UnsupportedCallbackException uce) { } catch (UnsupportedCallbackException uce) {
throw new LoginException("CallbackHandler does not support: " throw new LoginException("CallbackHandler does not support: " +
+ uce.getCallback()); uce.getCallback());
} }
info[0] = username; info[0] = username;