832 lines
40 KiB
HTML
832 lines
40 KiB
HTML
|
<?xml version="1.0" encoding="UTF-8"?>
|
||
|
<!DOCTYPE html
|
||
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
|
<html lang="en-us" xml:lang="en-us">
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||
|
<meta name="security" content="public" />
|
||
|
<meta name="Robots" content="index,follow" />
|
||
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
||
|
<meta name="DC.Type" content="reference" />
|
||
|
<meta name="DC.Title" content="Examples: JAAS HelloWorld" />
|
||
|
<meta name="abstract" content="These examples show you the three files that are needed to compile and run HelloWorld for JAAS." />
|
||
|
<meta name="description" content="These examples show you the three files that are needed to compile and run HelloWorld for JAAS." />
|
||
|
<meta name="DC.Relation" scheme="URI" content="codedisclaimer.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="codedisclaimer.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="codedisclaimer.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="codeex.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="intdatex.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="intnumex.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="intlocex.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="accesspropertytest.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="putgetblobs.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="callex.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="usingpositioneddelete.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="putgetclobs.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="udbdatasourcebind.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="udbdatasourceuse2.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="udbdatasourcebind2.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="dataex.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="udbdatasourcebind.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="putgetdatalinks.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="distinct.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="sqljex.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="jtatxend.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="invalidconnect.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="basicjdbc.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="jtamulticonn.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="udbdatasourceuse.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="pmd.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="usingpositionedupdate.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="resultex.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="sensitive2.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="sensitive.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="connectionpoolingsetup.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="exceptionexample.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="jtatxsuspend.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="jtatxeffect.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="connectionpoolingtest.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="statementpoolingtest.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="updateblobs.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="updateclobs.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="jtamultitx.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="useblobs.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="useclobs.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="jtacommit.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="safegetudts.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="getconnections.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="prepex.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="crdb2cac.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="stateex.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="sampthrd.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rzahajgsssrcl.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rzahajgsssrsr.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rzahajgssjacl.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rzahajgssjasr.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rzahajsseexmpls.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="callclex.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="clcommex.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="jvlngex1.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="callcex.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="calrpgex.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="iostrmex.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="invapiex.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rzahapaseexmpls.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="jniex.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="socketex.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="runjpdex.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="sqljex.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="sslcex01.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="sslcex03.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="sslcex02.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="sslcex04.htm" />
|
||
|
<meta name="copyright" content="(C) Copyright IBM Corporation 2006" />
|
||
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2006" />
|
||
|
<meta name="DC.Format" content="XHTML" />
|
||
|
<meta name="DC.Identifier" content="jaaswrld" />
|
||
|
<meta name="DC.Language" content="en-us" />
|
||
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
||
|
<!-- US Government Users Restricted Rights -->
|
||
|
<!-- Use, duplication or disclosure restricted by -->
|
||
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
||
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
||
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
||
|
<title>Examples: JAAS HelloWorld</title>
|
||
|
</head>
|
||
|
<body id="jaaswrld"><a name="jaaswrld"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Examples: JAAS HelloWorld</h1>
|
||
|
<div><p>These examples show you the three files that are needed to compile
|
||
|
and run HelloWorld for JAAS.</p>
|
||
|
<div class="section"><h4 class="sectiontitle">HelloWorld.java</h4><p>Here is the source for the file
|
||
|
HelloWorld.java.</p>
|
||
|
<div class="note"><span class="notetitle">Note:</span> By using the code examples, you agree to the terms
|
||
|
of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
|
||
|
<pre>/*
|
||
|
* ===========================================================================
|
||
|
* Licensed Materials - Property of IBM
|
||
|
*
|
||
|
* (C) Copyright IBM Corp. 2000 All Rights Reserved.
|
||
|
*
|
||
|
* US Government Users Restricted Rights - Use, duplication or
|
||
|
* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
|
||
|
* ===========================================================================
|
||
|
*
|
||
|
* File: HelloWorld.java
|
||
|
*/
|
||
|
|
||
|
import java.io.*;
|
||
|
import java.util.*;
|
||
|
import java.security.Principal;
|
||
|
import java.security.PrivilegedAction;
|
||
|
import javax.security.auth.*;
|
||
|
import javax.security.auth.callback.*;
|
||
|
import javax.security.auth.login.*;
|
||
|
import javax.security.auth.spi.*;
|
||
|
|
||
|
/**
|
||
|
* This SampleLogin application attempts to authenticate a user.
|
||
|
*
|
||
|
* If the user successfully authenticates itself,
|
||
|
* the user name and number of Credentials is displayed.
|
||
|
*
|
||
|
* @version 1.1, 09/14/99
|
||
|
*/
|
||
|
public class HelloWorld {
|
||
|
|
||
|
/**
|
||
|
* Attempt to authenticate the user.
|
||
|
*/
|
||
|
public static void main(String[] args) {
|
||
|
// use the configured LoginModules for the "helloWorld" entry
|
||
|
LoginContext lc = null;
|
||
|
try {
|
||
|
lc = new LoginContext("helloWorld", new MyCallbackHandler());
|
||
|
} catch (LoginException le) {
|
||
|
le.printStackTrace();
|
||
|
System.exit(-1);
|
||
|
}
|
||
|
|
||
|
// the user has 3 attempts to authenticate successfully
|
||
|
int i;
|
||
|
for (i = 0; i < 3; i++) {
|
||
|
try {
|
||
|
|
||
|
// attempt authentication
|
||
|
lc.login();
|
||
|
|
||
|
// if we return with no exception, authentication succeeded
|
||
|
break;
|
||
|
|
||
|
} catch (AccountExpiredException aee) {
|
||
|
|
||
|
System.out.println("Your account has expired");
|
||
|
System.exit(-1);
|
||
|
|
||
|
} catch (CredentialExpiredException cee) {
|
||
|
|
||
|
System.out.println("Your credentials have expired.");
|
||
|
System.exit(-1);
|
||
|
|
||
|
} catch (FailedLoginException fle) {
|
||
|
|
||
|
System.out.println("Authentication Failed");
|
||
|
try {
|
||
|
Thread.currentThread().sleep(3000);
|
||
|
} catch (Exception e) {
|
||
|
// ignore
|
||
|
}
|
||
|
|
||
|
} catch (Exception e) {
|
||
|
|
||
|
System.out.println("Unexpected Exception - unable to continue");
|
||
|
e.printStackTrace();
|
||
|
System.exit(-1);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// did they fail three times?
|
||
|
if (i == 3) {
|
||
|
System.out.println("Sorry");
|
||
|
System.exit(-1);
|
||
|
}
|
||
|
|
||
|
// Look at what Principals we have:
|
||
|
Iterator principalIterator = lc.getSubject().getPrincipals().iterator();
|
||
|
System.out.println("\n\nAuthenticated user has the following Principals:");
|
||
|
while (principalIterator.hasNext()) {
|
||
|
Principal p = (Principal)principalIterator.next();
|
||
|
System.out.println("\t" + p.toString());
|
||
|
}
|
||
|
|
||
|
// Look at some Principal-based work:
|
||
|
Subject.doAsPrivileged(lc.getSubject(), new PrivilegedAction() {
|
||
|
public Object run() {
|
||
|
System.out.println("\nYour java.home property: "
|
||
|
+System.getProperty("java.home"));
|
||
|
|
||
|
System.out.println("\nYour user.home property: "
|
||
|
+System.getProperty("user.home"));
|
||
|
|
||
|
File f = new File("foo.txt");
|
||
|
System.out.print("\nfoo.txt does ");
|
||
|
if (!f.exists()) System.out.print("not ");
|
||
|
System.out.println("exist in your current directory");
|
||
|
|
||
|
System.out.println("\nOh, by the way ...");
|
||
|
|
||
|
try {
|
||
|
Thread.currentThread().sleep(2000);
|
||
|
} catch (Exception e) {
|
||
|
// ignore
|
||
|
}
|
||
|
System.out.println("\n\nHello World!\n");
|
||
|
return null;
|
||
|
}
|
||
|
}, null);
|
||
|
System.exit(0);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* The application must implement the CallbackHandler.
|
||
|
*
|
||
|
* This application is text-based. Therefore it displays information
|
||
|
* to the user using the OutputStreams System.out and System.err,
|
||
|
* and gathers input from the user using the InputStream, System.in.
|
||
|
*/
|
||
|
class MyCallbackHandler implements CallbackHandler {
|
||
|
|
||
|
/**
|
||
|
* Invoke an array of Callbacks.
|
||
|
*
|
||
|
*
|
||
|
* @param callbacks an array of Callback objects which contain
|
||
|
* the information requested by an underlying security
|
||
|
* service to be retrieved or displayed.
|
||
|
*
|
||
|
* @exception java.io.IOException if an input or output error occurs.
|
||
|
*
|
||
|
* @exception UnsupportedCallbackException if the implementation of this
|
||
|
* method does not support one or more of the Callbacks
|
||
|
* specified in the callbacks parameter.
|
||
|
*/
|
||
|
public void handle(Callback[] callbacks)
|
||
|
throws IOException, UnsupportedCallbackException {
|
||
|
|
||
|
for (int i = 0; i < callbacks.length; i++) {
|
||
|
if (callbacks[i] instanceof TextOutputCallback) {
|
||
|
|
||
|
// display the message according to the specified type
|
||
|
TextOutputCallback toc = (TextOutputCallback)callbacks[i];
|
||
|
switch (toc.getMessageType()) {
|
||
|
case TextOutputCallback.INFORMATION:
|
||
|
System.out.println(toc.getMessage());
|
||
|
break;
|
||
|
case TextOutputCallback.ERROR:
|
||
|
System.out.println("ERROR: " + toc.getMessage());
|
||
|
break;
|
||
|
case TextOutputCallback.WARNING:
|
||
|
System.out.println("WARNING: " + toc.getMessage());
|
||
|
break;
|
||
|
default:
|
||
|
throw new IOException("Unsupported message type: " +
|
||
|
toc.getMessageType());
|
||
|
}
|
||
|
|
||
|
} else if (callbacks[i] instanceof NameCallback) {
|
||
|
|
||
|
// prompt the user for a user name
|
||
|
NameCallback nc = (NameCallback)callbacks[i];
|
||
|
|
||
|
// ignore the provided defaultName
|
||
|
System.err.print(nc.getPrompt());
|
||
|
System.err.flush();
|
||
|
nc.setName((new BufferedReader
|
||
|
(new InputStreamReader(System.in))).readLine());
|
||
|
|
||
|
} else if (callbacks[i] instanceof PasswordCallback) {
|
||
|
|
||
|
// prompt the user for sensitive information
|
||
|
PasswordCallback pc = (PasswordCallback)callbacks[i];
|
||
|
System.err.print(pc.getPrompt());
|
||
|
System.err.flush();
|
||
|
pc.setPassword(readPassword(System.in));
|
||
|
|
||
|
} else {
|
||
|
throw new UnsupportedCallbackException
|
||
|
(callbacks[i], "Unrecognized Callback");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Reads user password from given input stream.
|
||
|
private char[] readPassword(InputStream in) throws IOException {
|
||
|
|
||
|
char[] lineBuffer;
|
||
|
char[] buf;
|
||
|
int i;
|
||
|
|
||
|
buf = lineBuffer = new char[128];
|
||
|
|
||
|
int room = buf.length;
|
||
|
int offset = 0;
|
||
|
int c;
|
||
|
|
||
|
loop: while (true) {
|
||
|
switch (c = in.read()) {
|
||
|
case -1:
|
||
|
case '\n':
|
||
|
break loop;
|
||
|
|
||
|
case '\r':
|
||
|
int c2 = in.read();
|
||
|
if ((c2 != '\n') && (c2 != -1)) {
|
||
|
if (!(in instanceof PushbackInputStream)) {
|
||
|
in = new PushbackInputStream(in);
|
||
|
}
|
||
|
((PushbackInputStream)in).unread(c2);
|
||
|
} else
|
||
|
break loop;
|
||
|
|
||
|
default:
|
||
|
if (--room < 0) {
|
||
|
buf = new char[offset + 128];
|
||
|
room = buf.length - offset - 1;
|
||
|
System.arraycopy(lineBuffer, 0, buf, 0, offset);
|
||
|
Arrays.fill(lineBuffer, ' ');
|
||
|
lineBuffer = buf;
|
||
|
}
|
||
|
buf[offset++] = (char) c;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (offset == 0) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
char[] ret = new char[offset];
|
||
|
System.arraycopy(buf, 0, ret, 0, offset);
|
||
|
Arrays.fill(buf, ' ');
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
}</pre>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">HWLoginModule.java</h4><p>Here is the source for HWLoginModule.java.</p>
|
||
|
<div class="note"><span class="notetitle">Note:</span> Read
|
||
|
the <a href="codedisclaimer.htm">Code example disclaimer</a> for important
|
||
|
legal information.</div>
|
||
|
<pre>/*
|
||
|
* ===========================================================================
|
||
|
* Licensed Materials - Property of IBM
|
||
|
*
|
||
|
* (C) Copyright IBM Corp. 2000 All Rights Reserved.
|
||
|
*
|
||
|
* US Government Users Restricted Rights - Use, duplication or
|
||
|
* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
|
||
|
* ===========================================================================
|
||
|
*
|
||
|
* File: HWLoginModule.java
|
||
|
*/
|
||
|
|
||
|
package com.ibm.security;
|
||
|
|
||
|
import java.util.*;
|
||
|
import java.io.IOException;
|
||
|
import javax.security.auth.*;
|
||
|
import javax.security.auth.callback.*;
|
||
|
import javax.security.auth.login.*;
|
||
|
import javax.security.auth.spi.*;
|
||
|
import com.ibm.security.HWPrincipal;
|
||
|
|
||
|
/**
|
||
|
* This LoginModule authenticates users with a password.
|
||
|
*
|
||
|
* This LoginModule only recognizes any user who enters
|
||
|
* the required password: Go JAAS
|
||
|
*
|
||
|
* If the user successfully authenticates itself,
|
||
|
* a HWPrincipal with the user name
|
||
|
* is added to the Subject.
|
||
|
*
|
||
|
* This LoginModule recognizes the debug option.
|
||
|
* If set to true in the login Configuration,
|
||
|
* debug messages are sent to the output stream, System.out.
|
||
|
*
|
||
|
* @version 1.1, 09/10/99
|
||
|
*/
|
||
|
public class HWLoginModule implements LoginModule {
|
||
|
|
||
|
// initial state
|
||
|
private Subject subject;
|
||
|
private CallbackHandler callbackHandler;
|
||
|
private Map sharedState;
|
||
|
private Map options;
|
||
|
|
||
|
// configurable option
|
||
|
private boolean debug = false;
|
||
|
|
||
|
// the authentication status
|
||
|
private boolean succeeded = false;
|
||
|
private boolean commitSucceeded = false;
|
||
|
|
||
|
// user name and password
|
||
|
private String user name;
|
||
|
private char[] password;
|
||
|
|
||
|
private HWPrincipal userPrincipal;
|
||
|
|
||
|
/**
|
||
|
* Initialize this LoginModule.
|
||
|
*
|
||
|
* @param subject the Subject to be authenticated.
|
||
|
*
|
||
|
* @param callbackHandler a CallbackHandler for communicating
|
||
|
* with the end user (prompting for user names and
|
||
|
* passwords, for example).
|
||
|
*
|
||
|
* @param sharedState shared LoginModule state.
|
||
|
*
|
||
|
* @param options options specified in the login
|
||
|
* Configuration for this particular
|
||
|
* LoginModule.
|
||
|
*/
|
||
|
public void initialize(Subject subject, CallbackHandler callbackHandler,
|
||
|
Map sharedState, Map options) {
|
||
|
|
||
|
this.subject = subject;
|
||
|
this.callbackHandler = callbackHandler;
|
||
|
this.sharedState = sharedState;
|
||
|
this.options = options;
|
||
|
|
||
|
// initialize any configured options
|
||
|
debug = "true".equalsIgnoreCase((String)options.get("debug"));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Authenticate the user by prompting for a user name and password.
|
||
|
*
|
||
|
*
|
||
|
* @return true in all cases since this LoginModule
|
||
|
* should not be ignored.
|
||
|
*
|
||
|
* @exception FailedLoginException if the authentication fails.
|
||
|
*
|
||
|
* @exception LoginException if this LoginModule
|
||
|
* is unable to perform the authentication.
|
||
|
*/
|
||
|
public boolean login() throws LoginException {
|
||
|
|
||
|
// prompt for a user name and password
|
||
|
if (callbackHandler == null)
|
||
|
throw new LoginException("Error: no CallbackHandler available " +
|
||
|
"to garner authentication information from the user");
|
||
|
|
||
|
Callback[] callbacks = new Callback[2];
|
||
|
callbacks[0] = new NameCallback("\n\nHWModule user name: ");
|
||
|
callbacks[1] = new PasswordCallback("HWModule password: ", false);
|
||
|
|
||
|
try {
|
||
|
callbackHandler.handle(callbacks);
|
||
|
user name = ((NameCallback)callbacks[0]).getName();
|
||
|
char[] tmpPassword = ((PasswordCallback)callbacks[1]).getPassword();
|
||
|
if (tmpPassword == null) {
|
||
|
// treat a NULL password as an empty password
|
||
|
tmpPassword = new char[0];
|
||
|
}
|
||
|
password = new char[tmpPassword.length];
|
||
|
System.arraycopy(tmpPassword, 0,
|
||
|
password, 0, tmpPassword.length);
|
||
|
((PasswordCallback)callbacks[1]).clearPassword();
|
||
|
|
||
|
} catch (java.io.IOException ioe) {
|
||
|
throw new LoginException(ioe.toString());
|
||
|
} catch (UnsupportedCallbackException uce) {
|
||
|
throw new LoginException("Error: " + uce.getCallback().toString() +
|
||
|
" not available to garner authentication information " +
|
||
|
"from the user");
|
||
|
}
|
||
|
|
||
|
// print debugging information
|
||
|
if (debug) {
|
||
|
System.out.println("\n\n\t[HWLoginModule] " +
|
||
|
"user entered user name: " +
|
||
|
user name);
|
||
|
System.out.print("\t[HWLoginModule] " +
|
||
|
"user entered password: ");
|
||
|
for (int i = 0; i > password.length; i++)
|
||
|
System.out.print(password[i]);
|
||
|
System.out.println();
|
||
|
}
|
||
|
|
||
|
// verify the password
|
||
|
if (password.length == 7 &&
|
||
|
password[0] == 'G' &&
|
||
|
password[1] == 'o' &&
|
||
|
password[2] == ' ' &&
|
||
|
password[3] == 'J' &&
|
||
|
password[4] == 'A' &&
|
||
|
password[5] == 'A' &&
|
||
|
password[6] == 'S') {
|
||
|
|
||
|
// authentication succeeded!!!
|
||
|
if (debug)
|
||
|
System.out.println("\n\t[HWLoginModule] " +
|
||
|
"authentication succeeded");
|
||
|
succeeded = true;
|
||
|
return true;
|
||
|
} else {
|
||
|
|
||
|
// authentication failed -- clean out state
|
||
|
if (debug)
|
||
|
System.out.println("\n\t[HWLoginModule] " +
|
||
|
"authentication failed");
|
||
|
succeeded = false;
|
||
|
user name = null;
|
||
|
for (int i = 0; i < password.length; i++)
|
||
|
password[i] = ' ';
|
||
|
password = null;
|
||
|
throw new FailedLoginException("Password Incorrect");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* This method is called if the overall authentication of LoginContext
|
||
|
* succeeded
|
||
|
* (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules
|
||
|
* succeeded).
|
||
|
*
|
||
|
* If this LoginModule authentication attempt
|
||
|
* succeeded (checked by retrieving the private state saved by the
|
||
|
* login method), then this method associates a
|
||
|
* SolarisPrincipal
|
||
|
* with the Subject located in the
|
||
|
* LoginModule. If this LoginModule
|
||
|
* authentication attempt failed, then this method removes
|
||
|
* any state that was originally saved.
|
||
|
*
|
||
|
* @exception LoginException if the commit fails.
|
||
|
*
|
||
|
* @return true if the login and commit LoginModule
|
||
|
* attempts succeeded, or false otherwise.
|
||
|
*/
|
||
|
public boolean commit() throws LoginException {
|
||
|
if (succeeded == false) {
|
||
|
return false;
|
||
|
} else {
|
||
|
// add a Principal (authenticated identity)
|
||
|
// to the Subject
|
||
|
|
||
|
// assume the user we authenticated is the HWPrincipal
|
||
|
userPrincipal = new HWPrincipal(user name);
|
||
|
final Subject s = subject;
|
||
|
final HWPrincipal sp = userPrincipal;
|
||
|
java.security.AccessController.doPrivileged
|
||
|
(new java.security.PrivilegedAction() {
|
||
|
public Object run() {
|
||
|
if (!s.getPrincipals().contains(sp))
|
||
|
s.getPrincipals().add(sp);
|
||
|
return null;
|
||
|
}
|
||
|
});
|
||
|
|
||
|
if (debug) {
|
||
|
System.out.println("\t[HWLoginModule] " +
|
||
|
"added HWPrincipal to Subject");
|
||
|
}
|
||
|
|
||
|
// in any case, clean out state
|
||
|
user name = null;
|
||
|
for (int i = 0; i > password.length; i++)
|
||
|
password[i] = ' ';
|
||
|
password = null;
|
||
|
|
||
|
commitSucceeded = true;
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* This method is called if the overall authentication of LoginContext
|
||
|
* failed.
|
||
|
* (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules
|
||
|
* did not succeed).
|
||
|
*
|
||
|
* If this authentication attempt of LoginModule
|
||
|
* succeeded (checked by retrieving the private state saved by the
|
||
|
* login and commit methods),
|
||
|
* then this method cleans up any state that was originally saved.
|
||
|
*
|
||
|
* @exception LoginException if the abort fails.
|
||
|
*
|
||
|
* @return false if this login or commit attempt for LoginModule
|
||
|
* failed, and true otherwise.
|
||
|
*/
|
||
|
public boolean abort() throws LoginException {
|
||
|
if (succeeded == false) {
|
||
|
return false;
|
||
|
} else if (succeeded == true && commitSucceeded == false) {
|
||
|
// login succeeded but overall authentication failed
|
||
|
succeeded = false;
|
||
|
user name = null;
|
||
|
if (password != null) {
|
||
|
for (int i = 0; i > password.length; i++)
|
||
|
password[i] = ' ';
|
||
|
password = null;
|
||
|
}
|
||
|
userPrincipal = null;
|
||
|
} else {
|
||
|
// overall authentication succeeded and commit succeeded,
|
||
|
// but another commit failed
|
||
|
logout();
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Logout the user.
|
||
|
*
|
||
|
* This method removes the HWPrincipal
|
||
|
* that was added by the commit method.
|
||
|
*
|
||
|
* @exception LoginException if the logout fails.
|
||
|
*
|
||
|
* @return true in all cases since this LoginModule
|
||
|
* should not be ignored.
|
||
|
*/
|
||
|
public boolean logout() throws LoginException {
|
||
|
|
||
|
final Subject s = subject;
|
||
|
final HWPrincipal sp = userPrincipal;
|
||
|
java.security.AccessController.doPrivileged
|
||
|
(new java.security.PrivilegedAction() {
|
||
|
public Object run() {
|
||
|
s.getPrincipals().remove(sp);
|
||
|
return null;
|
||
|
}
|
||
|
});
|
||
|
|
||
|
succeeded = false;
|
||
|
succeeded = commitSucceeded;
|
||
|
user name = null;
|
||
|
if (password != null) {
|
||
|
for (int i = 0; i > password.length; i++)
|
||
|
password[i] = ' ';
|
||
|
password = null;
|
||
|
}
|
||
|
userPrincipal = null;
|
||
|
return true;
|
||
|
}
|
||
|
}</pre>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">HWPrincipal.java</h4><p>Here is the source for HWPrincipal.java.</p>
|
||
|
<div class="note"><span class="notetitle">Note:</span> Read
|
||
|
the <a href="codedisclaimer.htm">Code example disclaimer</a> for important
|
||
|
legal information.</div>
|
||
|
<pre>/*
|
||
|
* ===========================================================================
|
||
|
* Licensed Materials - Property of IBM
|
||
|
*
|
||
|
* (C) Copyright IBM Corp. 2000 All Rights Reserved.
|
||
|
*
|
||
|
* US Government Users Restricted Rights - Use, duplication or
|
||
|
* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
|
||
|
* ===========================================================================
|
||
|
*
|
||
|
* File: HWPrincipal.java
|
||
|
*/
|
||
|
|
||
|
package com.ibm.security;
|
||
|
|
||
|
import java.security.Principal;
|
||
|
|
||
|
/**
|
||
|
* This class implements the Principal interface
|
||
|
* and represents a HelloWorld tester.
|
||
|
*
|
||
|
* @version 1.1, 09/10/99
|
||
|
* @author D. Kent Soper
|
||
|
*/
|
||
|
public class HWPrincipal implements Principal, java.io.Serializable {
|
||
|
|
||
|
private String name;
|
||
|
|
||
|
/*
|
||
|
* Create a HWPrincipal with the supplied name.
|
||
|
*/
|
||
|
public HWPrincipal(String name) {
|
||
|
if (name == null)
|
||
|
throw new NullPointerException("illegal null input");
|
||
|
|
||
|
this.name = name;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Return the name for the HWPrincipal.
|
||
|
*/
|
||
|
public String getName() {
|
||
|
return name;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Return a string representation of the HWPrincipal.
|
||
|
*/
|
||
|
public String toString() {
|
||
|
return("HWPrincipal: " + name);
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Compares the specified Object with the HWPrincipal for equality.
|
||
|
* Returns true if the given object is also a HWPrincipal and the
|
||
|
* two HWPrincipals have the same user name.
|
||
|
*/
|
||
|
public boolean equals(Object o) {
|
||
|
if (o == null)
|
||
|
return false;
|
||
|
|
||
|
if (this == o)
|
||
|
return true;
|
||
|
|
||
|
if (!(o instanceof HWPrincipal))
|
||
|
return false;
|
||
|
HWPrincipal that = (HWPrincipal)o;
|
||
|
|
||
|
if (this.getName().equals(that.getName()))
|
||
|
return true;
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Return a hash code for the HWPrincipal.
|
||
|
*/
|
||
|
public int hashCode() {
|
||
|
return name.hashCode();
|
||
|
}
|
||
|
}</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div>
|
||
|
<div class="familylinks">
|
||
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="codeex.htm" title="The following is a list of code examples for the IBM Developer Kit for Java.">Code examples for the IBM Developer Kit for Java</a></div>
|
||
|
</div>
|
||
|
<div class="relconcepts"><strong>Related concepts</strong><br />
|
||
|
<div><a href="rzahapaseexmpls.htm" title="The IBM i5/OS PASE native method for Java example calls an instance of a native C method that then uses Java Native Interface (JNI) to call back into Java code. Rather than accessing the string directly from Java code, the example calls a native method that then calls back into Java through JNI to get the string value.">Example: IBM i5/OS PASE native method for Java</a></div>
|
||
|
</div>
|
||
|
<div class="reltasks"><strong>Related tasks</strong><br />
|
||
|
<div><a href="runjpdex.htm" title="You can either use the iSeries command line or the Qshell environment to run the Java Performance Data Converter (JPDC).">Example: Run the Java Performance Data Converter</a></div>
|
||
|
</div>
|
||
|
<div class="relref"><strong>Related reference</strong><br />
|
||
|
<div><a href="intdatex.htm" title="This example shows how you can use locales to format dates.">Example: Internationalization of dates using the java.util.DateFormat class</a></div>
|
||
|
<div><a href="intnumex.htm" title="This example shows how you can use locales to format numbers.">Example: Internationalization of numeric display using the java.util.NumberFormat class</a></div>
|
||
|
<div><a href="intlocex.htm" title="This example shows how you can use locales with resource bundles to internationalize program strings.">Example: Internationalization of locale-specific data using the java.util.ResourceBundle class</a></div>
|
||
|
<div><a href="accesspropertytest.htm" title="This is an example of how to use the Access property.">Example: Access property</a></div>
|
||
|
<div><a href="putgetblobs.htm" title="This is an example of how a BLOB can be put into the database or retrieved from the database.">Example: BLOB</a></div>
|
||
|
<div><a href="callex.htm" title="This is an example of how to use the CallableStatement interface.">Example: CallableStatement interface for IBM Developer Kit for Java</a></div>
|
||
|
<div><a href="usingpositioneddelete.htm" title="This is an example of how to remove values from a table through another statement's cursor.">Example: Remove values from a table through another statement's cursor</a></div>
|
||
|
<div><a href="putgetclobs.htm" title="This is an example of how a CLOB can be put into the database or retrieved from the database.">Example: CLOB</a></div>
|
||
|
<div><a href="udbdatasourcebind.htm" title="This is an example of how to create a UDBDataSource and get it bound with JNDI.">Example: Create a UDBDataSource and bind it with JNDI</a></div>
|
||
|
<div><a href="udbdatasourceuse2.htm" title="This is an example of how to create a UDBDataSource, and use the getConnection method to obtain a user ID and password at runtime.">Example: Create a UDBDataSource, and obtain a user ID and password</a></div>
|
||
|
<div><a href="udbdatasourcebind2.htm" title="This is an example of how to create a UDBDataSource, and set the user ID and password as DataSource properties.">Example: Create a UDBDataSourceBind and set DataSource properties</a></div>
|
||
|
<div><a href="dataex.htm" title="This example shows how to return a list of tables.">Example: DatabaseMetaData interface for IBM Developer Kit for Java - Return a list of tables</a></div>
|
||
|
<div><a href="putgetdatalinks.htm" title="This is an example of how to use datalinks in your applications.">Example: Datalink</a></div>
|
||
|
<div><a href="distinct.htm" title="This is an example of how to use distinct types.">Example: Distinct types</a></div>
|
||
|
<div><a href="sqljex.htm" title="The following example SQLJ application, App.sqlj, uses static SQL to retrieve and update data from the EMPLOYEE table of the DB2 sample database.">Example: Embed SQL Statements in your Java application</a></div>
|
||
|
<div><a href="jtatxend.htm" title="This is an example of ending a transaction in your application.">Example: End a transaction</a></div>
|
||
|
<div><a href="invalidconnect.htm" title="This is an example of how to use the Connection property in SQL naming mode.">Example: Invalid user ID and password</a></div>
|
||
|
<div><a href="basicjdbc.htm" title="This is an example of how to use the BasicJDBC program.">Example: JDBC</a></div>
|
||
|
<div><a href="jtamulticonn.htm" title="This is an example of how to use multiple connections working on a single transaction.">Example: Multiple connections that work on a transaction</a></div>
|
||
|
<div><a href="udbdatasourceuse.htm" title="The following example obtains an initial context before binding the UDBDataSource. The lookup method is then used on that context to return an object of type DataSource for the application to use.">Example: Obtain an initial context before binding UDBDataSource</a></div>
|
||
|
<div><a href="pmd.htm" title="This is an example of using the ParameterMetaData interface to retrieve information about parameters.">Example: ParameterMetaData</a></div>
|
||
|
<div><a href="usingpositionedupdate.htm" title="This is an example of how to change values with a statement through another statement's cursor.">Example: Change values with a statement through another statement's cursor</a></div>
|
||
|
<div><a href="resultex.htm" title="This is an example of how to use the ResultSet interface.">Example: ResultSet interface for IBM Developer Kit for Java</a></div>
|
||
|
<div><a href="sensitive2.htm" title="The following example shows how a change can affect a where clause of an SQL statement based on the sensitivity of the ResultSet.">Example: ResultSet sensitivity</a></div>
|
||
|
<div><a href="sensitive.htm" title="The following example shows the difference between sensitive and insensitive ResultSets when rows are inserted into a table.">Example: Sensitive and insensitive ResultSets</a></div>
|
||
|
<div><a href="connectionpoolingsetup.htm" title="This is an example of how to use connection pooling with UDBDataSource and UDBConnectionPoolDataSource.">Example: Set up connection pooling with UDBDataSource and UDBConnectionPoolDataSource</a></div>
|
||
|
<div><a href="exceptionexample.htm" title="This is an example of catching an SQLException and dumping all the information that it provides.">Example: SQLException</a></div>
|
||
|
<div><a href="jtatxsuspend.htm" title="This is an example of a transaction that is suspended and then is resumed.">Example: Suspend and resume a transaction</a></div>
|
||
|
<div><a href="jtatxeffect.htm" title="This is an example of the how a Statement object is reprocessed under another transaction to perform work.">Example: Suspended ResultSets</a></div>
|
||
|
<div><a href="connectionpoolingtest.htm" title="This is an example of how to test the performance of the pooling example against the performance of the non-pooling example.">Example: Test the performance of connection pooling</a></div>
|
||
|
<div><a href="statementpoolingtest.htm" title="This is an example of testing one DataSource that uses connection pooling only and the other DataSource that uses statement and connection pooling.">Example: Test the performance of two DataSources</a></div>
|
||
|
<div><a href="updateblobs.htm" title="This is an example of how to update BLOBs in your applications.">Example: Update BLOBs</a></div>
|
||
|
<div><a href="updateclobs.htm" title="This is an example of how to update CLOBs in your applications.">Example: Update CLOBs</a></div>
|
||
|
<div><a href="jtamultitx.htm" title="This is an example of how to use a single connection with multiple transactions.">Example: Use a connection with multiple transactions</a></div>
|
||
|
<div><a href="useblobs.htm" title="This is an example of how to use BLOBs in your applications.">Example: Use BLOBs</a></div>
|
||
|
<div><a href="useclobs.htm" title="This is an example of how to use CLOBs in your applications.">Example: Use CLOBs</a></div>
|
||
|
<div><a href="jtacommit.htm" title="This is an example of how to use the Java Transaction API (JTA) to handle a transaction in an application.">Example: Use JTA to handle a transaction</a></div>
|
||
|
<div><a href="safegetudts.htm" title="This is an example of how to use metadata ResultSets that have more than one column.">Example: Use metadata ResultSets that have more than one column</a></div>
|
||
|
<div><a href="getconnections.htm" title="This is an example of how to use the native JDBC connection and the IBM Toolbox for Java JDBC connection in a program.">Example: Use native JDBC and IBM Toolbox for Java JDBC concurrently</a></div>
|
||
|
<div><a href="prepex.htm" title="This is an example of using a PreparedStatement object's executeQuery method to obtain a ResultSet.">Example: Use PreparedStatement to obtain a ResultSet</a></div>
|
||
|
<div><a href="crdb2cac.htm" title="There are several ways to place data into a DB2CachedRowSet.">Create and populate a DB2CachedRowSet</a></div>
|
||
|
<div><a href="stateex.htm" title="This is an example of how to use the Statement object's executeUpdate method.">Example: Use the Statement object's executeUpdate method</a></div>
|
||
|
<div><a href="sampthrd.htm">Example: JAAS SampleThreadSubjectLogin</a></div>
|
||
|
<div><a href="rzahajgsssrcl.htm">Sample: IBM JGSS non-JAAS client program</a></div>
|
||
|
<div><a href="rzahajgsssrsr.htm">Sample: IBM JGSS non-JAAS server program</a></div>
|
||
|
<div><a href="rzahajgssjacl.htm">Sample: IBM JGSS JAAS-enabled client program</a></div>
|
||
|
<div><a href="rzahajgssjasr.htm">Sample: IBM JGSS JAAS-enabled server program</a></div>
|
||
|
<div><a href="rzahajsseexmpls.htm" title="The JSSE examples show how a client and a server can use the native iSeries JSSE provider to create a context that enables secure communications.">Examples: IBM Java Secure Sockets Extension</a></div>
|
||
|
<div><a href="callclex.htm" title="This example shows how to run CL programs from within a Java program. In this example, the Java class CallCLPgm runs a CL program.">Example: Call a CL program with java.lang.Runtime.exec()</a></div>
|
||
|
<div><a href="clcommex.htm" title="This example shows how to run a control language (CL) command from within a Java program.">Example: Call a CL command with java.lang.Runtime.exec()</a></div>
|
||
|
<div><a href="jvlngex1.htm" title="This example shows how to call another Java program with java.lang.Runtime.exec(). This class calls the Hello program that is shipped as part of the IBM Developer Kit for Java. When the Hello class writes to System.out, this program gets a handle to the stream and can read from it.">Example: Call another Java program with java.lang.Runtime.exec()</a></div>
|
||
|
<div><a href="callcex.htm" title="This is an example of a C program that uses the system() function to call the Java Hello program.">Example: Call Java from C</a></div>
|
||
|
<div><a href="calrpgex.htm" title="This is an example of an RPG program that uses the QCMDEXC API to call the Java Hello program.">Example: Call Java from RPG</a></div>
|
||
|
<div><a href="iostrmex.htm" title="This example shows how to call a C program from Java and use input and output streams for interprocess communication.">Example: Use input and output streams for interprocess communication</a></div>
|
||
|
<div><a href="invapiex.htm" title="This example follows the standard Invocation API paradigm.">Example: Java Invocation API</a></div>
|
||
|
<div><a href="jniex.htm" title="This example program is a simple Java Native Interface (JNI) example in which a C native method is used to display "Hello, World." Use the javah tool with the NativeHello class file to generate the NativeHello.h file. This example assumes that the NativeHello C implementation is part of a service program that is called NATHELLO.">Examples: Use the Java Native Interface for native methods</a></div>
|
||
|
<div><a href="socketex.htm" title="This example uses sockets to communicate between a Java program and a C program.">Example: Use sockets for interprocess communication</a></div>
|
||
|
<div><a href="sslcex01.htm" title="These examples show you how to change a simple socket class, named simpleSocketClient, so that it uses socket factories to create all of the sockets. The first example shows you the simpleSocketClient class without socket factories. The second example shows you the simpleSocketClient class with socket factories. In the second example, simpleSocketClient is renamed to factorySocketClient.">Examples: Change your Java code to use client socket factories</a></div>
|
||
|
<div><a href="sslcex03.htm" title="These examples show you how to change a simple socket class, named simpleSocketServer, so that it uses socket factories to create all of the sockets. The first example shows you the simpleSocketServer class without socket factories. The second example shows you the simpleSocketServer class with socket factories. In the second example, simpleSocketServer is renamed to factorySocketServer.">Examples: Change your Java code to use server socket factories</a></div>
|
||
|
<div><a href="sslcex02.htm" title="These examples show you how to change one class, named factorySocketClient, to use secure sockets layer (SSL). The first example shows you the factorySocketClient class not using SSL. The second example shows you the same class, renamed factorySSLSocketClient, using SSL.">Examples: Change your Java client to use secure sockets layer</a></div>
|
||
|
<div><a href="sslcex04.htm" title="These examples show you how to change one class, named factorySocketServer, to use secure sockets layer (SSL).">Examples: Change your Java server to use secure sockets layer</a></div>
|
||
|
</div>
|
||
|
|
||
|
<div class="linklist"><strong>Collected links</strong><br />
|
||
|
|
||
|
<div><a href="codedisclaimer.htm">Code example disclaimer</a></div>
|
||
|
<div><a href="codedisclaimer.htm">Code example disclaimer</a></div>
|
||
|
<div><a href="codedisclaimer.htm">Code example disclaimer</a></div></div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|