?? s7turnpasswordloginmodule.java.svn-base
字號:
/* * OBPasswordLoginModule.java * * Created on August 1, 2007, 10:05 AM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package com.s7turn.security;import com.sun.enterprise.security.auth.login.PasswordLoginModule;import java.lang.reflect.InvocationTargetException;import javax.naming.NamingException;import javax.security.auth.login.LoginException;import com.sun.logging.LogDomains;import java.lang.reflect.Method;import javax.naming.Context;import javax.naming.InitialContext;import java.util.logging.Logger;/** * Realm Class * @author ansonau */public class S7TurnPasswordLoginModule extends PasswordLoginModule{ private static Logger logger = LogDomains.getLogger (LogDomains.SECURITY_LOGGER); /** * Authenicate a user * @throws javax.security.auth.login.LoginException Throw when authenication fail */ protected void authenticate () throws LoginException { // Get the current realm and check whether it is instance of your realm if (!(_currentRealm instanceof S7TurnAppservRealm)) { throw new LoginException ("S7turnAppservRealm:badrealm"); } S7TurnAppservRealm myCustomRealm = (S7TurnAppservRealm) _currentRealm; Context ctx; Object remote = null; String[] roles = null; log("User {1} with password[{2}] is logging.", _username, _password); try { ctx = new InitialContext (); remote = ctx.lookup ("com.s7turn.security.session.AuthenicateServiceRemote"); Method method = remote.getClass ().getMethod ("authenicate",new Class[]{String.class,String.class }); Object result = method.invoke (remote,new Object[]{_username,_password}); roles = (String[]) result; } catch (NamingException ex) { log("Exception throws with message {1}.", ex.getMessage() ); throw new LoginException (ex.getMessage ()); } catch ( NoSuchMethodException ex) { log("Exception throws with message {1}.", ex.getMessage() ); throw new LoginException (ex.getMessage ()); } catch( IllegalAccessException ex) { log("Exception throws with message {1}.", ex.getMessage() ); throw new LoginException (ex.getMessage ()); } catch(InvocationTargetException ex) { log("Exception throws with message {1}.", ex.getMessage() ); throw new LoginException (ex.getMessage ()); } catch( NullPointerException ex ) { log("Exception throws with message {1}.", ex.getMessage() ); throw new LoginException( "Cannot find the Remote Bean : com.s7turn.security.session.AuthenicateServiceRemote" ); } catch(Exception ex) { log("Exception throws with message {1}.", ex.getMessage() ); throw new LoginException( ex.getMessage() ); } if( roles == null || roles.length == 0 ) { throw new LoginException ("Authenication fail or no role for " + _username); } log ("Login succeeded for " + _username); /* Enumeration groupEnum; try { groupEnum = myCustomRealm.getGroupNames (_username); } catch (NoSuchUserException ex) { throw new LoginException (ex.getMessage ()); } Vector<String> groups = new Vector (); while(groupEnum.hasMoreElements ()) { groups.add ((String)groupEnum.nextElement ()); } */ /* * Call the commitAuthentication to populate * grpList with the set of groups to which * _username belongs in this realm. */ /* commitUserAuthentication(_username, _password, _currentRealm, groupListToForward); */ //String[] g = new String[]{"IMPERSONATE","ReportViewer"}; //commitUserAuthentication (g); log( "User %s is logged in by with password %s. ", _username, _password ); super.commitAuthentication (_username, _password,_currentRealm, roles); //commitUserAuthentication (roles); //this.commit(); } /* * Helper methods. * * Simple message print method used throught the program */ /** * Log a msg * @param mesg Log a msg */ public static void log (String fmt, Object... param) { //logger.log (Level.INFO, "SimpleCustomRealm:"+mesg ); String mesg = String.format(fmt, param); //System.out.println(mesg); logger.info(mesg); } }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -