セキュリティー・パブリック API
Liberty プロファイルのセキュリティー・パブリック API は、セキュリティー・インフラストラクチャーを拡張する方法を提供します。
Liberty プロファイルには、セキュリティー機能の実装に使用できるパブリック API が含まれています。Liberty プロファイルのセキュリティー・パブリック API は、完全プロファイルのセキュリティー・パブリック API のサブセットです。 主なクラスは WSSecurityHelper、WSSubject、および RegistryHelper です。これらのクラスには、完全プロファイル版で使用可能なメソッドのサブセットが含まれています。 また、新しいクラス WebSecurityHelper もあります。
以下のセクションでは、これらの主なクラスについて説明します。 このほかに UserRegistry、WSCredential などのクラスや、その他の例外クラスもあります。
Liberty プロファイルでサポートされるすべてのセキュリティー・パブリック API は、Java™ API 文書に記載されています。各 Liberty プロファイル API の Java API 文書は、インフォメーション・センターの プログラミング・インターフェース (API) のセクションに詳述されていて、${wlp.install.dir}/dev ディレクトリーのいずれかの javadoc サブディレクトリー内の個別 .zip ファイル内にもあります。
- WSSecurityHelper
- このクラスには、isServerSecurityEnabled() と isGlobalSecurityEnabled() のメソッドのみが含まれます。
両者とも、server.xml ファイルで次のいずれかのフィーチャーが有効の場合、true を返します。
- appSecurity-2.0
- zosSecurity-1.0
- 注:
- Liberty プロファイルにセルは存在しないため、Liberty プロファイルではグローバル・セキュリティーとサーバー・セキュリティーの違いはありません。 そのため、両方のメソッドで同じ値が返されます。
- メソッド revokeSSOCookies(javax.servlet.http.HttpServletRequest req,javax.servlet.http.HttpServletResponse res) は、Liberty プロファイルでサポートされません。 代わりに、Servlet 3.0の logout 関数を使用できます。
- メソッド getLTPACookieFromSSOToken() は、新しいパブリック API クラス WebSecurityHelper に名前が変更されました。
- WSSubject
- このクラスでは、セキュリティー・スレッド・コンテキストの照会と設定を行うユーティリティー・メソッドが提供されます。
完全プロファイルの WSSubject のすべてのメソッドが、Liberty プロファイルでサポートされます。注: Java 2 セキュリティーはサポートされていますが、Liberty プロファイルではデフォルトで使用可能になっていません。そのため、デフォルトでは、WSSubject での Java 2 セキュリティー検査は実行されません。
- RegistryHelper
- このクラスでは、UserRegistry オブジェクトおよびトラステッド・レルムの情報へのアクセスを提供します。Liberty プロファイルでは、完全プロファイル メソッドの以下のサブセットが含まれています。
- public static UserRegistry getUserRegistry(String realmName) throws WSSecurityException
- public static List<String> getInboundTrustedRealms(String realmName) throws WSSecurityException
- public static boolean isRealmInboundTrusted(String inboundRealm, String localRealm)
- WebSecurityHelper
- このクラスには、名前変更された getLTPACookieFromSSOToken() メソッドが含まれています。これは、WSSecurityHelper から移動されました。
- public static Cookie getLTPACookieFromSSOToken() throws Exception
セキュリティー・パブリック API コードの例
次の例では、Liberty プロファイルでセキュリティー・パブリック API を使用して、
プログラマチック・ログインとサブジェクト操作を行う方法を示しています。
- 例 1: サブジェクトを作成して許可に使用します。
- この例では、WSSecurityHelper、WSSubject、および UserRegistry を使用することでプログラマチック・ログインを実行して、Java サブジェクトを作成してからアクションを実行し、そのサブジェクトを必要な許可に使用する方法を示します。注: 以下のコードでは、WSSecurityHelper を使用することによって、セキュリティーが有効かどうかを、セキュリティー処理を進める前にチェックしています。 この検査は、Liberty プロファイルのモジュラー性のために広く使用されています。セキュリティーが有効になっていない場合は、セキュリティー・ランタイムはロードされません。WSSecurityHelper は、セキュリティーが有効でなくても常にロードされます。
import java.rmi.RemoteException; import java.security.PrivilegedAction; import javax.security.auth.Subject; import javax.security.auth.callback.CallbackHandler; import javax.security.auth.login.LoginContext; import javax.security.auth.login.LoginException; import com.ibm.websphere.security.CustomRegistryException; import com.ibm.websphere.security.UserRegistry; import com.ibm.websphere.security.WSSecurityException; import com.ibm.websphere.security.WSSecurityHelper; import com.ibm.websphere.security.auth.WSSubject; import com.ibm.websphere.security.auth.callback.WSCallbackHandlerImpl; import com.ibm.wsspi.security.registry.RegistryHelper; public class myServlet { ... if (WSSecurityHelper.isServerSecurityEnabled()) { UserRegistry ur = null; try { ur = RegistryHelper.getUserRegistry(null); } catch (WSSecurityException e1) { // record some diagnostic info return; } String userid = "user1"; String password = "user1password"; try { if (ur.isValidUser(userid)) { // create a Subject, authenticating with // a userid and password CallbackHandler wscbh = new WSCallbackHandlerImpl(userid, password); LoginContext ctx; ctx = new LoginContext("WSLogin", wscbh); ctx.login(); Subject subject = ctx.getSubject(); // Perform an action using the Subject for // any required authorization WSSubject.doAs(subject, action); } } catch (CustomRegistryException e) { // record some diagnostic info return; } catch (RemoteException e) { // record some diagnostic info return; } catch (LoginException e) { // record some diagnostic info return; } } ... private final PrivilegedAction action = new PrivilegedAction() { @Override public Object run() { // do something useful here return null; } }; }
- 例 2: サブジェクトを作成して、スレッドの現行サブジェクトにします。
- 次の例では、WSSecurityHelper と WSSubject を使用し、
プログラマチック・ログインを行って Java サブジェクトを作成し、
そのサブジェクトをスレッドの現行サブジェクトにして、最後に、元のセキュリティー・スレッド・コンテキストを復元する方法を示しています。
注: 以下のコードでは、WSSecurityHelper を使用することによって、セキュリティーが有効かどうかを、セキュリティー処理を進める前にチェックしています。
import javax.security.auth.Subject; import javax.security.auth.callback.CallbackHandler; import javax.security.auth.login.LoginContext; import javax.security.auth.login.LoginException; import com.ibm.websphere.security.WSSecurityException; import com.ibm.websphere.security.WSSecurityHelper; import com.ibm.websphere.security.auth.WSSubject; import com.ibm.websphere.security.auth.callback.WSCallbackHandlerImpl; ... if (WSSecurityHelper.isServerSecurityEnabled()) { CallbackHandler wscbh = new WSCallbackHandlerImpl("user1", "user1password"); LoginContext ctx; try { // create a Subject, authenticating with // a userid and password ctx = new LoginContext("WSLogin", wscbh); ctx.login(); Subject mySubject = ctx.getSubject(); Subject oldSubject = null; try { // Save a ref to the current Subject on the thread oldSubject = WSSubject.getRunAsSubject(); // Make mySubject the current Subject on the thread WSSubject.setRunAsSubject(mySubject); // Do something useful here. Any authorization // required will be performed using mySubject } catch (WSSecurityException e) { // record some diagnostic info return; } finally { // Put the original Subject back on the thread context if (oldSubject != null) { try { WSSubject.setRunAsSubject(oldSubject); } catch (WSSecurityException e) { // record some diagnostic info } } } } catch (LoginException e) { // record some diagnostic info return; } }
- 例 3: スレッドの現行サブジェクトの情報を取得します。
- 次の例では、WSSecurityHelper、WSSubject、および WSCredential を使用して、スレッドの現行サブジェクトに関する情報を取得する方法を示します。注: 以下のコードでは、WSSecurityHelper を使用することによって、セキュリティーが有効かどうかを、セキュリティー処理を進める前にチェックしています。
import java.util.ArrayList; import java.util.Iterator; import java.util.Set; import javax.security.auth.Subject; import javax.security.auth.login.CredentialExpiredException; import com.ibm.websphere.security.WSSecurityException; import com.ibm.websphere.security.WSSecurityHelper; import com.ibm.websphere.security.auth.CredentialDestroyedException; import com.ibm.websphere.security.auth.WSSubject; import com.ibm.websphere.security.cred.WSCredential; ... if (WSSecurityHelper.isServerSecurityEnabled()) { // Get the caller's subject Subject callerSubject; try { callerSubject = WSSubject.getCallerSubject(); } catch (WSSecurityException e) { // record some diagnostic info return; } WSCredential wsCred = null; Set<WSCredential> wsCredentials = callerSubject.getPublicCredentials(WSCredential.class); Iterator<WSCredential> wsCredentialsIterator = wsCredentials.iterator(); if (wsCredentialsIterator.hasNext()) { wsCred = wsCredentialsIterator.next(); try { // Print out the groups ArrayList<String> groups = wsCred.getGroupIds(); for (String group : groups) { System.out.println("Group name: " + group); } } catch (CredentialExpiredException e) { // record some diagnostic info return; } catch (CredentialDestroyedException e) { // record some diagnostic info return; } } } }