1 46 47 package org.mr.kernel.security.authentication; 48 49 import org.mr.kernel.security.MantaSecurityException; 50 51 import javax.security.auth.Subject ; 52 import javax.security.auth.login.Configuration ; 53 import javax.security.auth.login.LoginContext ; 54 import javax.security.auth.login.LoginException ; 55 56 import org.apache.commons.logging.Log; 57 import org.apache.commons.logging.LogFactory; 58 59 73 public class MantaACLAuthenticationManager { 74 private static MantaACLAuthenticationManager _instance; 75 private Log _logger; 76 77 private MantaACLAuthenticationManager(){} 78 79 84 public static MantaACLAuthenticationManager getInstance() { 85 if (_instance == null){ 86 synchronized(MantaACLAuthenticationManager.class){ 87 if (_instance == null) 88 _instance = new MantaACLAuthenticationManager(); 89 } 90 } 91 return _instance; 92 } 93 94 105 public Subject isAuthenticated(String username, String password) throws MantaSecurityException { 106 try { 107 MantaCallbackHandler callbackHander = new MantaCallbackHandler(username, password); 108 Configuration.setConfiguration(new AuthenticationConfiguration()); 109 LoginContext loginContext = new LoginContext ("", callbackHander); 110 loginContext.login(); 111 112 if (getLogger().isInfoEnabled()) 113 getLogger().info("[isAuthenticated] User " + username + " has been authenticated"); 114 115 Subject subject = loginContext.getSubject(); 116 loginContext.logout(); 117 return subject; 118 } 119 catch (LoginException le){ 120 if (getLogger().isErrorEnabled()) 121 getLogger().error("[isAuthenticated] Error while authenticating user " + username + ". " + le.getMessage()); 122 throw new MantaSecurityException("Error while authenticating user " + username + ". " + le.getMessage()); 123 } 124 } 125 126 131 public Log getLogger(){ 132 if (_logger == null){ 133 _logger = LogFactory.getLog(getClass().getName()); 134 } 135 return _logger; 136 } 137 } 138 | Popular Tags |