Liberty のセキュリティー・パブリック API
Liberty のセキュリティー・パブリック API は、セキュリティー・インフラストラクチャーを拡張する方法を提供します。
Liberty には、セキュリティー機能の実装に使用できるパブリック API が含まれています。Liberty のセキュリティー・パブリック API は、WebSphere® Application Server traditionalのセキュリティー・パブリック API のサブセットです。主なクラスは WSSecurityHelper、WSSubject、および RegistryHelper です。これらのクラスには、WebSphere Application Server traditional の各バージョンで使用可能なメソッドのサブセットが含まれています。また、新しいクラス WebSecurityHelper もあります。
以下のセクションでは、これらの主なクラスについて説明します。 このほかに UserRegistry、WSCredential などのクラスや、その他の例外クラスもあります。
Liberty でサポートされるすべてのセキュリティー・パブリック API は、Java™ API 文書に記載されています。各 Liberty API の Java API ドキュメンテーションは、${wlp.install.dir}/dev ディレクトリーのいずれかの javadoc サブディレクトリー内の個別 .zip ファイル内にあります。
- WSSecurityHelper
- このクラスには、isServerSecurityEnabled() と isGlobalSecurityEnabled() のメソッドのみが含まれます。 特に appSecurity-2.0 または zosSecurity-1.0 が使用可能の場合、これらの呼び出しは true を戻します。 それ以外の場合、これらのメソッドは false を返します。これらのメソッドは、 互換性のために WebSphere Application Server traditionalの WSSecurityHelper クラスから引き継がれたものです。
- 注:
- Liberty にセルは存在しないため、Liberty ではグローバル・セキュリティーとサーバー・セキュリティーの違いはありません。そのため、両方のメソッドで同じ値が返されます。
- メソッド revokeSSOCookies(javax.servlet.http.HttpServletRequest req,javax.servlet.http.HttpServletResponse res) は、Liberty でサポートされません。代わりに、Servlet 3.0の logout 関数を使用できます。
- メソッド getLTPACookieFromSSOToken() は、新しいパブリック API クラス WebSecurityHelper に名前が変更されました。
- WSSubject
- このクラスでは、セキュリティー・スレッド・コンテキストの照会と設定を行うユーティリティー・メソッドが提供されます。
WebSphere Application Server traditionalの WSSubject のすべてのメソッドが、Liberty でサポートされます。注: Java 2 セキュリティーはサポートされていますが、Liberty ではデフォルトで使用可能になっていません。そのため、デフォルトでは、WSSubject での Java 2 セキュリティー検査は実行されません。
- RegistryHelper
- このクラスでは、UserRegistry オブジェクトおよびトラステッド・レルムの情報へのアクセスを提供します。 Liberty では、WebSphere Application Server traditional メソッドの以下のサブセットが含まれています。
- 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)
- 注: このメソッドには、OSGI 動的サービス変更として変更される可能性のある動的情報が関係します。 取得される値が不整合になる可能性があります。UserRegistry 参照は決してキャッシュしないようにしてください。
- WebSecurityHelper
- このクラスには、WSSecurityHelper から移動され、名前が変更された getLTPACookieFromSSOToken() メソッドが含まれています。
- public static Cookie getSSOCookieFromSSOToken() throws Exception
- PasswordUtil
- このクラスは、機密情報を保護するためのエンコード方式およびデコード方式を提供します。このクラスを API として使用可能にするには、server.xml ファイル内で passwordUtilities-1.0 フィーチャーを構成します。注: このクラスは、WebSphere Application Server traditionalにも存在します。ただし Liberty のほうが、より多くの機能が加わります。したがって、より多くの方式が導入されます。
セキュリティー・パブリック 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; } } } }