JAAS 程式化登入

程式化登入是一種表單登入類型,支援應用程式呈現網站特定的登入表單,以進行鑑別。

當 Enterprise Bean 用戶端應用程式需要使用者提供識別資訊時,應用程式的寫出器必須收集該資訊並鑑別使用者。 程式設計師的工作可以根據執行實際使用者鑑別的位置進行廣泛分類:

  • 在用戶端程式中
  • 在伺服器程式中

Web 應用程式的使用者可以多種方式接收鑑別資料的提示。 Web 應用程式部署描述子檔案中的 <login-config> 元素定義用來收集此資訊的機制。 想要自訂登入程序的程式設計師,可以使用表單型登入來提供應用程式特定的 HTML 表單,以收集登入資訊,而不是依賴一般用途裝置 (例如瀏覽器中的 401 對話視窗)。

除非啟用 管理安全 ,否則不會進行鑑別。 如果您想要對 Web 應用程式使用表單型登入,您必須在每一個 Web 應用程式的部署描述子中,在 <login-config> 元素的 auth-method 標籤中指定 FORM

應用程式可以使用 WebSphere® Application Server 表單登入類型來呈現網站特定的登入表單。 Java™ Platform, Enterprise Edition (Java EE) 規格將表單登入定義為 Web 應用程式的其中一個鑑別方法。 WebSphere Application Server 提供表單登出機制。

Java 鑑別和授權服務程式化登入

「Java 鑑別和授權服務 (JAAS)」是 WebSphere Application Server中的新特性。 它也是 Java EE 1.4 規格所規定的。 JAAS 是策略性鑑別應用程式設計介面 (API) 的集合,用來取代 Common Object Request Broker Architecture (CORBA) 程式化登入 API。 WebSphere Application Server 提供部分 JAAS延伸:

在開始使用程式化登入 API 進行開發之前,請考量下列要點:
  • 對於純 Java 用戶端應用程式或用戶端儲存器應用程式,請先起始設定用戶端 Object Request Broker (ORB) 安全,再執行 JAAS 登入。 在 JAAS 登入之前執行下列程式碼來執行此動作:
    ...
    import java.util.Hashtable;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    ...
    // Perform an InitialContext and default lookup prior to logging 
    // in to initialize ORB security and for the bootstrap host/port 
    // to be determined for SecurityServer lookup. If you do not want 
    // to validate the userid/password during the JAAS login, disable 
    // the com.ibm.CORBA.validateBasicAuth property in the 
    // sas.client.props file.
    
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, 
        "com.ibm.websphere.naming.WsnInitialContextFactory");
    env.put(Context.PROVIDER_URL, 
         "corbaloc:iiop:myhost.mycompany.com:2809");
    Context initialContext = new InitialContext(env);
    Object obj = initialContext.lookup("");
    
    [z/OS]附註: 每當連接至 z/OS® 伺服器時,請設定 com.ibm.CORBA.validateBasicAuth=false 。 此功能目前無法從分散式用戶端運作到 z/OS 伺服器,因為 SecurityServer 是使用 z/OS 系統上不接受的 "UNAUTHENTICATED" 主體找到的。
  • 對於純 Java 用戶端應用程式或用戶端儲存器應用程式,請確定已適當地指定目標「Java 命名和目錄介面 (JNDI)」引導內容的主機名稱和埠號。 如需詳細資料,請參閱 開發使用 CosNaming (CORBA 命名介面) 一節。
  • 如果應用程式使用自訂 JAAS 登入配置,請確定 自訂 JAAS 登入配置已適當定義
  • 部分 JAAS API 受到 Java 2 安全許可權保護。 如果應用程式碼使用這些 API ,請確定這些許可權已新增至應用程式 was.policy 檔。 如需詳細資料,請參閱 將 was.policy 檔新增至 Java 2 安全的應用程式使用 PolicyTool 來編輯 Java 2 安全的原則檔配置 Java 2 安全的 was.policy 檔 小節。 如需哪些 API 受到 Java 2 安全許可權保護的詳細資料,請檢查 IBM® Developer Kit Java Technology Edition; JAAS 和 WebSphere Application Server 公用 API 文件,以取得詳細資料。 下列清單包含在本文件所提供的範例程式碼中使用的 API。
    • javax.security.auth.login.LoginContext 建構子受到 javax.security.auth.AuthPermission "createLoginContext" 保護。
    • javax.security.auth.Subject.doAs 和 com.ibm.websphere.security.auth.WSSubject.doAs 受到 javax.security.auth.AuthPermission "doAs" 保護。
    • javax.security.auth.Subject.doAsPrivileged 和 com.ibm.websphere.security.auth.WSSubject.doAsPrivileged 受到 javax.security.auth.AuthPermission "doAsPrivileged" 的保護。
  • com.ibm.websphere.security.auth.WSSubject: 由於 JAAS 1.0版中的設計監督, javax.security.auth.Subject.getSubject 不會傳回與 java.security.AccessController.doPrivileged 程式碼區塊。 這可能呈現有問題的不一致行為,並導致不想要的工作暫行解決方法。 com.ibm.websphere.security.auth.WSSubject API 提供暫行解決方法,可讓「主體」與執行中的執行緒產生關聯。 com.ibm.websphere.security.auth.WSSubject API 會將 JAAS 模型延伸至 Java EE 資源,以進行授權檢查。 與 com.ibm.websphere.security.auth.WSSubject.doAs 或 com.ibm.websphere.security.auth.WSSubject.doAsPrivileged 程式碼區塊內執行中的執行緒相關聯的「主題」用於 Java EE 資源授權檢查。
  • 定義新的 JAAS 登入配置的管理主控台支援: 您可以在管理主控台中配置 JAAS 登入配置,並將它儲存在 WebSphere Application Server 配置 API 中。 應用程式可以在管理主控台中定義新的 JAAS 登入配置,資料會持續保存在 WebSphere Application Server 配置 API 所儲存的配置儲存庫中。 不過, WebSphere Application Server 仍然支援 JAAS 預設實作所提供的預設 JAAS 登入配置格式。 如果同時以 WebSphere Application Server 配置 API 和純文字檔案格式來定義重複登入配置,則 WebSphere Application Server 配置 API 中的登入配置優先。 在 WebSphere Application Server 配置 API 中定義登入配置的優點包括:
    • 利用管理主控台來定義 JAAS 登入配置。
    • 集中管理 JAAS 登入配置。
    • WebSphere Application Server Network Deployment 安裝中配送 JAAS 登入配置。
  • WebSphere Application Server的 JAAS 登入配置: WebSphere Application Server 為應用程式提供 JAAS 登入配置,以執行 WebSphere Application Server 安全執行時期的程式化鑑別。 WebSphere Application Server 的這些 JAAS 登入配置會根據所提供的鑑別資料,對所配置的鑑別機制、簡易 WebSphere 鑑別機制 (SWAM) 或小型認證機構 (LTPA) ,以及使用者登錄 (本端 OS、LDAP 或自訂) 執行鑑別。 這些 JAAS 登入配置中的已鑑別主體包含必要的主體和認證,可供 WebSphere Application Server 安全執行時期用來對 Java EE 角色型受保護資源執行授權檢查。
    附註: SWAM 在 WebSphere Application Server 9.0 中已淘汰,並將在未來版本中移除。
    以下是 WebSphere Application Server所提供的 JAAS 登入配置:
    • WSLogin JAAS 登入配置 :Java 用戶端、用戶端儲存器應用程式、Servlet、JSP 檔、Enterprise Bean 等,可用來執行以使用者 ID 和密碼為基礎的鑑別,或 WebSphere Application Server 安全記號的通用 JAAS 登入配置 執行時期。 不過,這項配置不支援用戶端儲存器部署描述子中指定的 CallbackHandler 處理程式。
    • ClientContainer JAAS 登入配置: 這個 JAAS 登入配置可辨識用戶端儲存器部署描述子中指定的 CallbackHandler 處理程式。 這個登入配置的登入模組會使用用戶端儲存器部署描述子中的 CallbackHandler 處理程式 (如果有指定的話) ,即使應用程式碼在登入環境定義中指定了一個 CallbackHandler 處理程式也一樣。 這是用於用戶端儲存器應用程式。
    • 使用先前提及的 JAAS 登入配置來鑑別的主體包含 com.ibm.websphere.security.auth.WSPrincipal 主體和 com.ibm.websphere.security.auth.WSCredential 認證。 如果將已鑑別的 Subject 傳給 com.ibm.websphere.security.auth.WSSubject.doAs 方法或其他 doAs 方法, WebSphere Application Server 安全執行時期可以根據 com.ibm.websphere.security.auth.WSCredential 認證,對 Java EE 資源執行授權檢查。
  • 客戶定義的 JAAS 登入配置: 您可以 定義其他 JAAS 登入配置。 使用這些登入配置,對自訂鑑別機制執行程式化鑑別。 不過,如果主體不包含必要的主體和認證, WebSphere Application Server 安全執行時期可能不會使用這些客戶定義 JAAS 登入配置中的主體來執行授權檢查。

從 JAAS 登入尋找主要原因登入異常狀況

如果您在發出 LoginContext.login API 之後收到 LoginException 異常狀況,您可以從所配置的使用者登錄中找到主要原因異常狀況。 在登入模組中, com.ibm.websphere.security.auth.WSLoginFailedException 類別會覆蓋登錄異常狀況。 此異常狀況具有 getCause 方法,您可以使用該方法來拉出在發出前一個指令之後包裝的異常狀況。

您不一定會取得 WSLoginFailed異常狀況,但這裡會顯示從使用者登錄產生的大部分異常狀況。 下列範例說明含有相關聯 catch 區塊的 LoginContext.login API。 如果您想要發出 getCause API ,請將 WSLoginFailed異常狀況強制轉型為 com.ibm.websphere.security.auth.WSLoginFailedException 類別。

下列 determineCause 範例可用於處理「 CustomUser登錄」異常狀況類型。
try 
    {
         lc.login(); 
    } 
    catch (LoginException le)
    {
	// drill down through the exceptions as they might cascade through the runtime
	Throwable root_exception = determineCause(le);
	
	// now you can use "root_exception" to compare to a particular exception type
	// for example, if you have implemented a CustomUserRegistry type, you would 
  //  know what to look for here.
    }


/* Method used to drill down into the WSLoginFailedException to find the 
"root cause" exception */

    public Throwable determineCause(Throwable e) 
      {
				Throwable root_exception = e, temp_exception = null;

				// keep looping until there are no more embedded WSLoginFailedException or 
				// WSSecurityException exceptions 
         while (true) 
				{
						if (e instanceof com.ibm.websphere.security.auth.WSLoginFailedException)
						{
							temp_exception = ((com.ibm.websphere.security.auth.WSLoginFailedException)
							e).getCause();
						}
						else if (e instanceof com.ibm.websphere.security.WSSecurityException)
						{
							temp_exception = ((com.ibm.websphere.security.WSSecurityException)
							e).getCause();
						}
						else if (e instanceof javax.naming.NamingException)
								// check for Ldap embedded exception
								{
										temp_exception = ((javax.naming.NamingException)e).getRootCause();
								}
						else if (e instanceof your_custom_exception_here)
						{
								// your custom processing here, if necessary
						}
						else
						{
								// this exception is not one of the types we are looking for,
								// lets return now, this is the root from the WebSphere 
								//  Application Server perspective
								return root_exception;
						}
						if (temp_exception != null)
						{
								// we have an exception; go back and see if this has another
								// one embedded within it.
								root_exception = temp_exception;
								e = temp_exception;
								continue;
						}
						else
						{
								// we finally have the root exception from this call path, this
								// has to occur at some point
								return root_exception;
						}
				}
		}

從 Servlet 過濾器尋找主要原因登入異常狀況

在處理後置表單登入處理時,您也可以從 Servlet 過濾器收到主要原因異常狀況。 此異常狀況非常有用,因為它會向使用者顯示發生的情況。 您可以發出下列 API ,以取得主要原因異常狀況:
Throwable t = com.ibm.websphere.security.auth.WSSubject.getRootLoginException();  
if (t != null)  	
         t = determineCause(t);

發生異常狀況時,您可以透過先前的 determineCause 範例來執行它,以取得原生登錄主要原因。

啟用將主要原因登入異常狀況傳播至 Pure Java 用戶端

目前,基於安全理由,主要原因不會延伸到純用戶端。 不過,您可能想要將主要原因傳播至授信環境中的純用戶端。 如果您想要啟用將登入異常狀況傳送至純用戶端的主要原因,請按一下「 WebSphere Application Server 管理主控台」上的 安全 > 廣域安全 > 自訂內容 ,並設定下列內容:

com.ibm.websphere.security.registry.propagateExceptionsToClient=true

無提示程式化登入

WebSphere Application Server 提供 javax.security.auth.callback.CallbackHandler 介面的非提示實作,稱為 com.ibm.websphere.security.auth.callback.WSCallbackHandlerImpl。 使用此介面,應用程式可以將鑑別資料推送至 WebSphere LoginModule 實例,以執行鑑別。 這項功能有助於伺服器端應用程式碼鑑別身分,以及使用該身分來呼叫下游 Java EE 資源。
javax.security.auth.login.LoginContext lc = null;

try {
lc = new javax.security.auth.login.LoginContext("WSLogin",
new com.ibm.websphere.security.auth.callback.WSCallbackHandlerImpl("user", 
      "securityrealm", "securedpassword"));

// create a LoginContext and specify a CallbackHandler implementation
// CallbackHandler implementation determine how authentication data is collected
// in this case, the authentication data is "push" to the authentication mechanism
//   implemented by the LoginModule.
} catch (javax.security.auth.login.LoginException e) {
System.err.println("ERROR: failed to instantiate a LoginContext and the exception: " 
+ e.getMessage());
e.printStackTrace();

// maybe javax.security.auth.AuthPermission "createLoginContext" is not granted
//   to the application, or the JAAS login configuration is not defined.
}

if (lc != null)
try {
lc.login();  // perform login
javax.security.auth.Subject s = lc.getSubject();
// get the authenticated subject

// Invoke a Java EE resource using the authenticated subject
com.ibm.websphere.security.auth.WSSubject.doAs(s,
new java.security.PrivilegedAction() {
public Object run() {
try {
bankAccount.deposit(100.00);  // where bankAccount is a protected EJB
} catch (Exception e) {
System.out.println("ERROR: error while accessing EJB resource, exception: " 
+ e.getMessage());
e.printStackTrace();
}
return null;
}
}
);
} catch (javax.security.auth.login.LoginException e) {
System.err.println("ERROR: login failed with exception: " + e.getMessage());
e.printStackTrace();

// login failed, might want to provide relogin logic
}

您可以將 com.ibm.websphere.security.auth.callback.WSCallbackHandlerImpl 回呼處理常式與純 Java 用戶端、用戶端應用程式儲存器、Enterprise Bean、 JavaServer Pages (JSP) 檔、Servlet 或其他 Java 2 Platform Enterprise Edition (Java EE) 資源搭配使用。

附註: WSCallbackHandlerImpl 回呼處理常式會因您使用 WebSphere Application Server 安全或「Web 服務安全」而不同。 基於安全考量,它位於 sas.jar 檔案中,以及「Web 服務安全」的 was-wssecurity.jar 檔案中。

使用者介面提示程式化登入

WebSphere Application Server 也提供 javax.security.auth.callback.CallbackHandler 實作的使用者介面實作,以透過使用者介面登入提示來收集使用者的鑑別資料。 com.ibm.websphere.security.auth.callback.WSGUICallbackHandlerImpl 回呼處理常式會呈現使用者介面登入畫面,以提示使用者輸入鑑別資料。
[AIX HP-UX Solaris][z/OS]附註: 此行為要求 DISPLAY 環境呼叫 X11 伺服器。
javax.security.auth.login.LoginContext lc = null;

try {
lc = new javax.security.auth.login.LoginContext("WSLogin",
new com.ibm.websphere.security.auth.callback.WSGUICallbackHandlerImpl());

// create a LoginContext and specify a CallbackHandler implementation
// CallbackHandler implementation determine how authentication data is collected
// in this case, the authentication date is collected by GUI login prompt
//   and pass to the authentication mechanism implemented by the LoginModule.
} catch (javax.security.auth.login.LoginException e) {
System.err.println("ERROR: failed to instantiate a LoginContext and the exception: " 
+ e.getMessage());
e.printStackTrace();

// maybe javax.security.auth.AuthPermission "createLoginContext" is not granted
//   to the application, or the JAAS login configuration is not defined.
}

if (lc != null)
try {
lc.login();  // perform login
javax.security.auth.Subject s = lc.getSubject();
// get the authenticated subject

// Invoke a Java EE resources using the authenticated subject
com.ibm.websphere.security.auth.WSSubject.doAs(s,
new java.security.PrivilegedAction() {
public Object run() {
try {
bankAccount.deposit(100.00);  // where bankAccount is a protected enterprise bean
} catch (Exception e) {
System.out.println("ERROR: error while accessing EJB resource, exception: " 
+ e.getMessage());
e.printStackTrace();
}
return null;
}
}
);
} catch (javax.security.auth.login.LoginException e) {
System.err.println("ERROR: login failed with exception: " + e.getMessage());
e.printStackTrace();

// login failed, might want to provide relogin logic
}
注意: 請勿對 Enterprise Bean、Servlet、JSP 檔等伺服器端資源使用 com.ibm.websphere.security.auth.callback.WSGUICallbackHandlerImpl 回呼處理常式。 使用者介面登入提示會封鎖伺服器進行使用者輸入。 此行為不適用於伺服器處理程序。

WebSphere Application Server 也提供 javax.security.auth.callback.CallbackHandler 介面的 Kerberos 認證快取實作。 回呼處理常式 com.ibm.websphere.security.auth.callback.WSCcacheCallBackHandlerImpl。 使用此介面,應用程式可以將鑑別資料推送至 WebSphere LoginModule 實例,以執行鑑別。

此功能僅適用於使用 Kerberos 認證快取向 WebSphere Application Server 進行鑑別的用戶端應用程式碼。

如果 wsjaas_client.conf 檔案中存在下列選項,請將它們設為 false:
   useDefaultKeytab=false
useDefaultCcache=false
tryFirstPass=false
useFirstPass=false
forwardable=false
renewable=false
renewable=false
noaddress=false
javax.security.auth.login.LoginContext lc = null;

String krb5Ccache = /etc/krb5/krb5cc_utle;

try {
lc = new javax.security.auth.login.LoginContext("WSKRB5Login",
new com.ibm.websphere.security.auth.callback.WSCcacheCallBackHandlerImpl(user, krb5Realm, krb5Ccache, false));
// create a LoginContext and specify a CallbackHandler implementation
// CallbackHandler implementation determines how authentication data is collected
// in this case, the authentication date is collected by stdin prompt
// and passed to the authentication mechanism implemented by the LoginModule.
} catch (javax.security.auth.login.LoginException e) {
System.err.println("ERROR: failed to instantiate a LoginContext and the exception: 
          " + e.getMessage());
e.printStackTrace();

// maybe javax.security.auth.AuthPermission "createLoginContext" is not granted
//   to the application, or the JAAS login configuration is not defined.
}

if (lc != null)
try {
lc.login();  // perform login
javax.security.auth.Subject s = lc.getSubject();
// get the authenticated subject

// Invoke a Java EE resource using the authenticated subject
com.ibm.websphere.security.auth.WSSubject.doAs(s,
new java.security.PrivilegedAction() {
public Object run() {
try {
bankAccount.deposit(100.00);  
// where bankAccount is a protected enterprise bean
} catch (Exception e) {
System.out.println("ERROR: error while accessing EJB resource, exception: " 
       + e.getMessage());
e.printStackTrace();
}
return null;
}
}
);
} catch (javax.security.auth.login.LoginException e) {
System.err.println("ERROR: login failed with exception: " + e.getMessage());
e.printStackTrace();

// login failed, might want to provide relogin logic
}

Application Server with the default Kerberos credential cache.

javax.security.auth.login.LoginContext lc = null;

try {
lc = new javax.security.auth.login.LoginContext("WSKRB5Login",
new com.ibm.websphere.security.auth.callback.WSCcacheCallBackHandlerImpl(user, krb5Realm, null, true));
// create a LoginContext and specify a CallbackHandler implementation
// CallbackHandler implementation determines how authentication data is collected
// in this case, the authentication date is collected by stdin prompt
// and passed to the authentication mechanism implemented by the LoginModule.
} catch (javax.security.auth.login.LoginException e) {
System.err.println("ERROR: failed to instantiate a LoginContext and the exception: 
          " + e.getMessage());
e.printStackTrace();

// maybe javax.security.auth.AuthPermission "createLoginContext" is not granted
//   to the application, or the JAAS login configuration is not defined.
}

if (lc != null)
try {
lc.login();  // perform login
javax.security.auth.Subject s = lc.getSubject();
// get the authenticated subject

// Invoke a Java EE resource using the authenticated subject
com.ibm.websphere.security.auth.WSSubject.doAs(s,
new java.security.PrivilegedAction() {
public Object run() {
try {
bankAccount.deposit(100.00);  
// where bankAccount is a protected enterprise bean
} catch (Exception e) {
System.out.println("ERROR: error while accessing EJB resource, exception: " 
       + e.getMessage());
e.printStackTrace();
}
return null;
}
}
);
} catch (javax.security.auth.login.LoginException e) {
System.err.println("ERROR: login failed with exception: " + e.getMessage());
e.printStackTrace();

// login failed, might want to provide relogin logic
}

Application Server with the Microsoft native Kerberos credential cache. The client must
login to the Microsoft Domain Controller.

javax.security.auth.login.LoginContext lc = null;

try {
lc = new javax.security.auth.login.LoginContext("WSKRB5Login",
new com.ibm.websphere.security.auth.callback.WSCcacheCallBackHandlerImpl(null, null, null, true));
// create a LoginContext and specify a CallbackHandler implementation
// CallbackHandler implementation determines how authentication data is collected
// in this case, the authentication date is collected by stdin prompt
// and passed to the authentication mechanism implemented by the LoginModule.
} catch (javax.security.auth.login.LoginException e) {
System.err.println("ERROR: failed to instantiate a LoginContext and the exception: 
          " + e.getMessage());
e.printStackTrace();

// maybe javax.security.auth.AuthPermission "createLoginContext" is not granted
//   to the application, or the JAAS login configuration is not defined.
}

if (lc != null)
try {
lc.login();  // perform login
javax.security.auth.Subject s = lc.getSubject();
// get the authenticated subject

// Invoke a Java EE resource using the authenticated subject
com.ibm.websphere.security.auth.WSSubject.doAs(s,
new java.security.PrivilegedAction() {
public Object run() {
try {
bankAccount.deposit(100.00);  
// where bankAccount is a protected enterprise bean
} catch (Exception e) {
System.out.println("ERROR: error while accessing EJB resource, exception: " 
       + e.getMessage());
e.printStackTrace();
}
return null;
}
}
);
} catch (javax.security.auth.login.LoginException e) {
System.err.println("ERROR: login failed with exception: " + e.getMessage());
e.printStackTrace();

// login failed, might want to provide relogin logic
}

WSKRB5Login 模組

WSKRB5Login JAAS 登入配置: 是一種通用 JAAS 登入配置, Java 用戶端、用戶端儲存器應用程式、Servlet、JSP 檔或 Enterprise Bean 可以用來執行鑑別,以 Kerberos 主體名稱密碼或 WebSphere Application Server 安全執行時期的 Kerberos 認證快取為基礎。 不過,這項配置不支援用戶端儲存器部署描述子中指定的 CallbackHandler 處理程式。

將您已建立的 krb5.inikrb5.conf 檔案放在預設位置。 如果任一檔案不在預設位置中,您必須以正確路徑和 Kerberos 配置檔名稱來設定 java.security.krb5.conf JVM 系統內容。

在 Windows ® 平台上,預設位置為 c:\winnt\krb5.ini。

在 LinuxR 平台上,預設位置是 /etc/krb5.conf。

在其他 Unix 平台上,預設位置是 /etc/krb5/krb5.conf。

在 z/OS 平台上,預設位置是 /etc/krb5/krb5.conf。

Kerberos 配置設定、 Kerberos 金鑰配送中心 (KDC) 名稱及領域設定是在 Kerberos 配置檔中提供,或透過 java.security.krb5.kdc 及 java.security.krb5.realm 系統內容提供。 檔案。

Stdin 提示程式化登入

WebSphere Application Server 也提供 javax.security.auth.callback.CallbackHandler 介面的 stdin 實作。 回呼處理常式 com.ibm.websphere.security.auth.callback.WSStdinCallbackHandlerImpl會提示並透過 stdin 提示從使用者收集鑑別資料。
javax.security.auth.login.LoginContext lc = null;

try {
lc = new javax.security.auth.login.LoginContext("WSLogin",
new com.ibm.websphere.security.auth.callback.WSStdinCallbackHandlerImpl());

// create a LoginContext and specify a CallbackHandler implementation
// CallbackHandler implementation determines how authentication data is collected
// in this case, the authentication date is collected by stdin prompt
// and passed to the authentication mechanism implemented by the LoginModule.
} catch (javax.security.auth.login.LoginException e) {
System.err.println("ERROR: failed to instantiate a LoginContext and the exception: 
          " + e.getMessage());
e.printStackTrace();

// maybe javax.security.auth.AuthPermission "createLoginContext" is not granted
//   to the application, or the JAAS login configuration is not defined.
}

if (lc != null)
try {
lc.login();  // perform login
javax.security.auth.Subject s = lc.getSubject();
// get the authenticated subject

// Invoke a Java EE resource using the authenticated subject
com.ibm.websphere.security.auth.WSSubject.doAs(s,
new java.security.PrivilegedAction() {
public Object run() {
try {
bankAccount.deposit(100.00);  
// where bankAccount is a protected enterprise bean
} catch (Exception e) {
System.out.println("ERROR: error while accessing EJB resource, exception: " 
       + e.getMessage());
e.printStackTrace();
}
return null;
}
}
);
} catch (javax.security.auth.login.LoginException e) {
System.err.println("ERROR: login failed with exception: " + e.getMessage());
e.printStackTrace();

// login failed, might want to provide relogin logic
}
注意: 請勿對 Enterprise Bean、Servlet、JSP 檔等伺服器端資源使用 com.ibm.websphere.security.auth.callback.WSStdinCallbackHandlerImpl 回呼處理常式。 來自 stdin 提示的輸入不會傳送至伺服器環境。 大部分伺服器在背景中執行,且沒有主控台。 不過,如果伺服器有主控台, stdin 提示會封鎖伺服器進行使用者輸入。 此行為不適用於伺服器處理程序。