Friday, April 29, 2005

More On JAAS

Someone may unahppy with writting a ungly config to tell JAAS to use which module. However, up to now there is no easy solution to prevent the JAAS config file.....(we can't hard code it into the program anyway).... and itis unpleasent to write a long start command( a lot of maintance work!!)
so my solution is to pass a URL to the class and set the corresponding system property. Although this way is not prefect....(the prefect way it to pass it as a annotation or in a centralic configuration file)... but it look better then typing a long parameters ^_^..


For Example:

NT.config:
NT {
com.tagish.auth.win32.NTSystemLogin required returnNames=true returnSIDs=true defaultDomain="c08913";
};


In the code add this:

URL loginConfigUrl = ClassLoader.getSystemResource("NT.config");
String loginConfigFile = loginConfigUrl.getFile();
System.err.println(loginConfigFile);
System.setProperty("java.security.auth.login.config",
loginConfigFile);
lc = new LoginContext("NT", new MyCallbackHandler());

Eliminate the -Djava.library.path argument in command line for Native Library
Beside, as I tried to pack the natve library into my LoginBeam , So i would like to load the native libraray automatically without specific the native library class path. The System.load() run sucessfully without exception. However, It reported to me that It fail to find the Native library while calling it ........... "Cannot create LoginContext. java.lang.UnsatisfiedLinkError: no NTSystem in java.library.path"

I will investingate it later after the exam......XD

public static void main(String[] args) {
LoginContext lc;
Subject subject = null;

try {
//Load the Native library NTSystem.dll
//-Djava.library.path=C:\Data\eclipse\workspace\NTLoginSystem\bin\lib //load successfully for the native library
URL nativeLib = ClassLoader.getSystemResource("lib/NTSystem.dll");
System.load(nativeLib.getFile());

// find the login config file in the classpath
//Load the NT.config, so we don't need to specific the NT.config at command line
URL loginConfigUrl = ClassLoader.getSystemResource("lib/NT.config");
String loginConfigFile = loginConfigUrl.getFile();
System.setProperty("java.security.auth.login.config",
loginConfigFile);

lc = new LoginContext("NT", new MyCallbackHandler());
lc.login();//Exception here
subject = lc.getSubject();
System.out.println("Login Success");
System.out.println(subject);
} catch (LoginException le) {
System.err
.println("Cannot create LoginContext. " + le.getMessage());
} catch (SecurityException se) {
System.err
.println("Cannot create LoginContext. " + se.getMessage());
}
}





No comments: