ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzatz_5.4.0.1/51/sec/secdjasx.htm

86 lines
4.1 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
<title>Example: JAAS programmatic login</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h4><a name="secdjasx"></a>Example: JAAS programmatic login</h4>
<p>The following example illustrates how application programs may perform a programmatic login using JAAS authentication:</p>
<pre> LoginContext lc = null;
try {
lc = new LoginContext(&quot;WSLogin&quot;,
new WSCallbackHandlerImpl(&quot;userName&quot;, &quot;realm&quot;, &quot;password&quot;));
}
catch (LoginException le) {
System.out.println(&quot;Cannot create LoginContext. &quot; + le.getMessage());
// insert error processing code
}
catch(SecurityException se) {
System.out.println(&quot;Cannot create LoginContext.&quot; + se.getMessage();
// Insert error processing
}
try {
lc.login();
}
catch(LoginExcpetion le) {
System.out.printlin(&quot;Fails to create Subject. &quot; + le.getMessage());
// Insert error processing code
}</pre>
<p>Shown in the example, the new LoginContext is initialized with the WSLogin login configuration and the WSCallbackHandlerImpl CallbackHandler. The WSCallbackHandlerImpl is suitable for use on server side application where prompting is not desirable. A WSCallbackHandlerImpl instance is initialized by the specified user id, password, and the realm information. The present WSLoginModuleImpl class implementation that is specified by WSLogin can only retrieve authentication information from the specified CallbackHandler. A LoginContext may be constructed with a Subject object but the Subject will be disregarded by the present WSLoginModuleImpl implementation.</p>
<p>In addition, you can also develop your own LoginModule if the default WSLoginModuleImpl implementation cannot address all your requirements. WebSphere provides utility functions that can be used by custom LoginModule which are described in the next section.</p>
<p>In cases where there is no java.naming.provider.url set as a system property or in the jndi.properties file, a default InitialContext does not function if the application server is not at the localhost:2809 location. In this situation, perform a new InitialContext programmatically before of the JAAS login. JAAS needs to know where the SecurityServer resides to verify that the user ID and password are correct, prior to doing a commit(). By performing a new InitialContext as shown below, the security code has the information needed to find the SecurityServer location and the target realm.</p>
<pre>...
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
...
// Perform an InitialContext and default lookup prior to logging
// in so that target realm and bootstrap host/port can be determined
// for SecurityServer lookup.
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
&quot;com.ibm.websphere.naming.WsnInitialContextFactory&quot;);
env.put(Context.PROVIDER_URL, &quot;corbaloc:iiop:myhost.mycompany.com:2809&quot;);
Context initialContext = new InitialContext(env);
Object obj = initialContext.lookup(&quot;&quot;);
LoginContext lc = null;
try {
lc = new LoginContext(&quot;WSLogin&quot;,
new WSCallbackHandlerImpl(&quot;userName&quot;, &quot;realm&quot;, &quot;password&quot;));
} catch (LoginException le) {
System.out.println(&quot;Cannot create LoginContext.&quot; + le.getMessage());
// insert error processing code
} catch(SecurityException se) {
System.out.printlin(&quot;Cannot create LoginContext.&quot; + se.getMessage();
// Insert error processing
}
try {
lc.login();
} catch(LoginException le) {
System.out.printlin(&quot;Fails to create Subject.&quot; + le.getMessage());
// Insert error processing code
}</pre>
</body>
</html>