Thursday, October 7, 2010

Custom Login Module

import com.sun.appserv.security.AppservPasswordLoginModule;
import com.sun.enterprise.security.auth.realm.jdbc.JDBCRealm;
import javax.security.auth.login.LoginException;

/**
* Class SimpleCustomLogin is used for designing our own custom module
* it extends a class provided by GF server that helps us authenticating the user
* Using this we need to take care of single method authenticateUser() that will
* have our logic to confirm about the user.
*/
public class SimpleCustomLoginModule extends AppservPasswordLoginModule {

public SimpleCustomLoginModule() {

//maintaining the log to ensure that our LoginModule is initialized.
log("SimpleCustomLoginModule: Initialization");

}//end of constructor

/**
* this method helps us maintain server log
* so that we can make amendments according to our need
* and it also helps determining the flow.
*/
private void log(String s) {
System.out.println((new StringBuilder())
.append("SimpleCustomLoginModule::").append(s).toString());
}//end of log()

/**
* This is the method we need to overrdide which will have our LOGIC for
* authentication .In this we are using the DATABASE as backend for user.
* So we need to use the JDBCRealm ( Realm - is a set of roles of user
* with their respective users to know more about realm check this out
* http://download.oracle.com/javaee/5/tutorial/doc/
* OR
* http://download-llnw.oracle.com/javaee/6/tutorial/doc/bncas.html).
* Now we need to configure this realm in
* our web.xml under login-config tag from where the server retrieves it.
* Also maintaining evrything in the server log as well.
* We paas authenticated user group(roles here) to commitUserAuthentication
* method for authorization.

*/
@Override
protected void authenticateUser() throws LoginException
{
System.out.println("in authenticate user.....");
//getting the instance of current realm as JDBCRealm
JDBCRealm samplerealm = (JDBCRealm)_currentRealm;
System.out.println("jdbc realm as ....." +samplerealm);

/**
* Checking the instance of the realm whether its taking our JDBC realm
* or not. This will load the user's from database with their name,
* password and roles. Else if not will throw LoginException.
*/
if ( !( _currentRealm instanceof JDBCRealm ) ) {
String msg = sm.getString("filelm.badrealm");
System.out.println("not instance of jdbc realm ....");
throw new LoginException(msg);
}

/**
* Here we get the user's roles from the database in grplist array.
* But here we get only one element in the array.So we need to check.
* Since the value stored in table is in form of varchar(simple strings)
* as roles -- "user,test,......so on"
* Hence we separate it later in groupListToForward.
*/
String[] grpList = samplerealm.authenticate(_username, _password);
System.out.println("username " + _username + "password " + _password);
System.out.println("length from jdbc authenticate ... groups are.." + grpList);

/**
* This id to check whether the user group(role) is null or not.
*/
if ( grpList == null ) { // JAAS behavior
String msg = sm.getString("filelm.faillogin", _username);
throw new LoginException(msg);
}//endif


/**
* make a copy of groupList to pass to LoginModule.
* DO NOT PASS the grpList as is - as it will get overwritten.
* Resulting in logins passing only once.
* In the above grouplist we get only string with multiple user's role.
* grplist value will be like "user,weballow,test"
* But here we split them into multiple roles as roles as
* 1. user 2. weballow 3. test
* That will be passed for proper authorization.
*/
String[] groupListToForward = grpList[0].toString().split(",");
System.out.println("values of group array ..." + groupListToForward.length);
for (int i = 0; i< groupListToForward.length; i++) {
System.out.println("value of groups " + groupListToForward[i].toString());
 }
System.out.println("calling commit...");
commitUserAuthentication(groupListToForward);
  }//end of authernticateUser()

}//end of SimpleCustomLoginModule

3 comments:

Anonymous said...

Eventfulness is actually a synonym associated with life, and here is where individual lenders monetarily help out people or its borrowers [url=http://www.bzbka.co.uk/]http://www.fastlongtermloans.co.uk/[/url] fast long term loans For this reason, most e-commerce web developers to offer custom made shopping golf carts style of program sites http://www.fastlongtermloans.co.uk/

Anonymous said...

PMI bills stay in put until the customer's equity actually reaches 20%, then many companies will remove it, in case asked [url=http://www.shorttermloanspapa.co.uk/]short term loans[/url] http://www.shorttermloanspapa.co.uk/ In this era associated with e-Learning, where there is less connection between the coach and the tutored http://www.kkubt.co.uk/

Unknown said...

There are many requirements that do not need to do this if you want to use the described loans. Even have to fax a document or need to grant security to the lender. This means that you have absolute freedom to cash according to your needs. With the help of no credit check loans you are able to cover a variety of personal requirements. This means that it is allowed to reach the small amounts ranging from £ 100 to £ 1000 for short term period of 12 months from the date of approval.
After complete a single online application form you can obtain payday loans no credit check without the security the lender. Therefore, these loans are excellent product at the time of the monetary fiscal policy as soon as possible.