開發 WS-Security 的密碼回呼處理常式
在 Liberty 中,許多 WS-Security 實務都可以利用密碼回呼處理常式來擷取密碼。例如,可以擷取密碼來產生 UsernameToken、開啟金鑰儲存庫或存取私密金鑰。 當您使用 PasswordDigest 時,提供者會需要密碼回呼處理常式。密碼回呼處理常式必須包裝成 Liberty 中的一項使用者特性。
關於這項作業
這個作業說明如何開發密碼回呼處理常式來擷取使用者名稱和金鑰儲存庫金鑰密碼。
程序
- 開發密碼回呼處理常式。 下列範例顯示回呼處理常式:
package com.ibm.ws.wssecurity.example.cbh; import java.util.HashMap; import java.util.Map; import javax.security.auth.callback.Callback; import javax.security.auth.callback.CallbackHandler; import org.apache.ws.security.WSPasswordCallback; public class SamplePasswordCallback implements CallbackHandler { private Map<String, String> userPasswords = new HashMap<String, String>(); private Map<String, String> keyPasswords = new HashMap<String, String>(); public SamplePasswordCallback() { // 一些使用者密碼範例 userPasswords.put("user1", "user1pswd"); userPasswords.put("admin", "adminpswd"); // 一些金鑰密碼範例 keyPasswords.put("alice", "keypwsd"); keyPasswords.put("bob", "keypswd"); } public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { for (int i = 0; i < callbacks.length; i++) { WSPasswordCallback pwcb = (WSPasswordCallback)callbacks[i]; String id = pwcb.getIdentifier(); String pass = null; switch (pwcb.getUsage()) { case WSPasswordCallback.USERNAME_TOKEN_UNKNOWN: case WSPasswordCallback.USERNAME_TOKEN: pass = userPasswords.get(id); pwcb.setPassword(pass); break; case WSPasswordCallback.SIGNATURE: case WSPasswordCallback.DECRYPT: pass = keyPasswords.get(id); pwcb.setPassword(pass); break; } } } }
- 建立回呼處理常式的 MANIFEST.MF 檔。 下列範例顯示 MANIFEST.MF 檔:
Manifest-Version: 1.0 Bnd-LastModified: 1359415594428 Build-Identifier: SNAPSHOT-Mon Jan 28 17:26:34 CST 2013 Bundle-Copyright: The Program materials contained in this file are IBM copyright materials. 5724-I63, 5724-J08, 5724-H89, 5724-H88, 5655-W65 Copyright International Business Machines Corp. 1999, 2012 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. Bundle-Description: An PasswordCallbackHandler; version=1.0.0 Bundle-ManifestVersion: 2 Bundle-Name: wssecuritycbh Bundle-SymbolicName: com.ibm.ws.wssecurity.example.cbh Bundle-Vendor: IBM Bundle-Version: 1.0.0 Created-By: 1.6.0 (IBM Corporation) Export-Package: com.ibm.ws.wssecurity.example.cbh;uses:="com.ibm.websphe re.ras.annotation,javax.security.auth.callback";version="1.0.0" Import-Package: com.ibm.websphere.ras,com.ibm.websphere.ras.annotation,c om.ibm.ws.ffdc,javax.security.auth.callback,org.apache.ws.security;version="[1.6,2)" Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version>=1.6))" Tool: Bnd-2.1.0.20120920-170235 WS-TraceGroup: WSSecurity
- 將回呼處理常式包裝成 JAR 檔。 建立一個含有回呼處理常式類別及上述步驟所建立之 MANIFEST.MF 檔的 JAR 檔。 下列範例顯示稱為 SampleCbh.jar 之 JAR 檔範例的內容:
META-INF/MANIFEST.MF com/ibm/ws/wssecurity/example/cbh/SamplePasswordCallback.class
- 建立一個特性資訊清單檔。 下列範例顯示稱為 wsseccbh-1.0.mf 特性資訊清單檔範例:
Subsystem-ManifestVersion: 1 Subsystem-SymbolicName: wsseccbh-1.0; visibility:=public Subsystem-Version: 1.0.0 Subsystem-Content: com.ibm.ws.wssecurity.example.cbh; version="[1,1.0.100)"; location:="lib/"; type="osgi.bundle"; start-phase:=APPLICATION_EARLY Subsystem-Type: osgi.subsystem.feature IBM-Feature-Version: 2 IBM-API-Package: com.ibm.ws.wssecurity.example.cbh; version="1.0"; type="internal"
- 將回呼處理常式安裝成 Liberty 中的一項使用者特性。 將回呼處理常式 JAR 檔和特性資訊清單檔複製到 Liberty 使用者目錄中。 下列範例顯示檔案的複製位置:
build.image/wlp/usr/extension/lib/SampleCbh.jar build.image/wlp/usr/extension/lib/features/wsseccbh-1.0.mf
- 在 server.xml 檔中,將回呼處理常式配置為一項使用者特性。 在 server.xml 檔中,將回呼處理常式定義為一項使用者特性。 下列範例顯示如何將回呼處理常式定義為一項使用者特性:
<featureManager> <feature>usr:wsseccbh-1.0</feature> <feature>servlet-3.0</feature> <feature>appSecurity-2.0</feature> <feature>jaxws-2.2</feature> <feature>wsSecurity-1.1</feature> </featureManager>
結果
下一步
上層主題: Web 服務訊息層次安全


http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=twlp_wssec_callbackhandler
檔名:twlp_wssec_callbackhandler.html