開發 customPasswordEncryption 提供者

您可以實作 Liberty 伺服器中提供的 com.ibm.wsspi.security.crypto.CustomPasswordEncryption 介面,將 customPasswordEncryption 提供者開發成具備 Java™ Platform Enterprise Edition (J2EE) 應用程式的自訂授權決策。

關於這項作業

註: 下列選用步驟用來支援指令行公用程式。如果提供者打算只支援伺服器執行時期環境,則可以跳過這些步驟。
customPassworedEncryption 提供者由下列三個檔案組成:
  • 「OSGi 軟體組檔案」,含有執行檔。
  • 「特性資訊清單檔」,用來定義 customPasswordEncryption 提供者的使用者特性。這個檔案含有必要軟體組的位置和特性的屬性,以及延伸資訊清單檔(若有包裝的話)。此資訊供伺服器執行時期使用。
  • 「延伸資訊清單檔」,用來定義 customPasswordEncryption 提供者的必要軟體組檔案和實作類別名稱。這個檔案是選用的,如果要支援指令行公用程式,則需要這個檔案。

程序

  1. 建立一個 OSGi 元件,以提供用來實作 com.ibm.wsspi.security.crypto.CustomPasswordEncryption 介面的服務。

    CustomPasswordEncryption 介面定義下列三個方法:decryptLiberty 伺服器執行時期呼叫它來進行字串解密;encryptLiberty 伺服器執行時期呼叫它來進行字串加密;以及 initialize,保留供未來使用。

    下列範例使用 OSGi 宣告式服務註釋:
    package com.mycompany.custom;
    
    import org.osgi.service.component.ComponentContext;
    import org.osgi.service.component.annotations.Activate;
    import org.osgi.service.component.annotations.Component;
    import org.osgi.service.component.annotations.ConfigurationPolicy;
    import org.osgi.service.component.annotations.Deactivate;
    import org.osgi.service.component.annotations.Modified;
    
    import com.ibm.wsspi.security.crypto.CustomPasswordEncryption;
    import com.ibm.wsspi.security.crypto.EncryptedInfo;
    import com.ibm.wsspi.security.crypto.PasswordDecryptException;
    import com.ibm.wsspi.security.crypto.PasswordEncryptException;
    
    /**
     */
    @Component(service = CustomPasswordEncryption.class,
                    immediate = true,
                    name = "com.mycompany.CustomPasswordEncryptionImpl",
                    configurationPolicy = ConfigurationPolicy.OPTIONAL,
                    property = { "someKey=someValue" })
    public class CustomPasswordEncryptionImpl implements CustomPasswordEncryption {
    
        @Activate
        protected synchronized void activate(ComponentContext cc, Map<String, Object> props) {
        }
    
        @Modified
        protected synchronized void modify(Map<String, Object> props) {
        }
    
        @Deactivate
        protected void deactivate(ComponentContext cc) {}
    
        /**
         * 加密作業以 byte[] 的形式來使用 UTF-8 編碼字串。
         * byte[] 是從 String.getBytes("UTF-8") 產生。加密的 byte[]
         * 會從 EncryptedInfo 物件中的實作傳回。
         * 此外,還會在 EncryptedInfo 中傳回邏輯金鑰別名,以便
         * 傳回解密方法來判斷是用哪個金鑰
         * 將這個密碼加密。WebSphere Application Server 執行時期並不
         * 知道用來進行資料加密的演算法或金鑰。
         * 
         * @param decrypted_bytes
         * @return com.ibm.wsspi.security.crypto.EncryptedInfo
         * @throws com.ibm.wsspi.security.crypto.PasswordEncryptException
         **/
        @Override
        public EncryptedInfo encrypt(byte[] input) throws PasswordEncryptException {
            byte[] output = null;
            String key = null;
            try {
                :
                <做一些加密>
                :
                return new EncryptedInfo(output, key);
            } catch (Exception e) {
                throw new PasswordEncryptException("Exception is caught", e);
            }
        }
    
        /**
         * 解密作業會使用包含 byte[] 的 EncryptedInfo 物件
         * 及邏輯金鑰別名,且會將它轉換成已解密的 byte[]。
         * WebSphere Application Server 執行時期會利用新的
         * String (byte[], "UTF-8");,將 byte[] 轉換成字串
         * 
         * @param info
         * @return byte[]
         * @throws PasswordEncryptException
         * @throws com.ibm.wsspi.security.crypto.PasswordDecryptException
         **/
        @Override
        public byte[] decrypt(EncryptedInfo info) throws PasswordDecryptException {
            byte[] input = info.getEncryptedBytes();
            String key = info.getKeyAlias();
            byte[] output = null;
            try {
                :
                <做一些解密>
                :
                return output;
            } catch (Exception e) {
                throw new PasswordEncryptException("Exception is caught", e);
            }
        }
    
        /**
         * 這個保留供未來使用,目前
         * WebSphere Application Server 執行時期不會呼叫它。
         * 
         * @param initialization_data
         **/
        @SuppressWarnings("rawtypes")
        @Override
        public void initialize(Map initialization_data) {}
    
    }
  2. 將元件包裝成在使用者特性中的 OSGi 軟體組。請確定軟體組包含 OSGI 服務資訊清單。
    下列範例顯示 OSGI 服務資訊清單的內容:
    <?xml version="1.0" encoding="UTF-8"?>
    <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="com.mycompany.custom.CustomPasswordEncryptionImpl" configuration-policy="optional" immediate="true" activate="activate" deactivate="deactivate" modified="modify">
      <implementation class="com.mycompany.custom.CusomPasswordEncryptionImpl"/>
      <service>
        <provide interface="com.ibm.wsspi.security.crypto.CustomPasswordEncryption"/>
      </service>
      <property name="<someKey>" type="String" value="<someValue>"/>
    </scr:component>
  3. 確定您的特性資訊清單檔包括含有 start-phase:="SERVICE_EARLY" 的 OSGI 子系統內容。如果完成選用步驟,則延伸資訊清單檔的位置位於 Subsystem-Content 標頭中。 例如:
    Manifest-Version: 1.0
    IBM-Feature-Version: 2
    IBM-ShortName: customPasswordEncryption-1.0
    Subsystem-Type: osgi.subsystem.feature
    Subsystem-Version: 1.0.0
    Subsystem-ManifestVersion: 1.0
    Subsystem-SymbolicName: customPasswordEncryption-1.0;visibility:=public
    Subsystem-Content:
    com.mycompany.custom; version="[1,1.0.100)"; start-phase:="SERVICE_EARLY", customEncryption.jar; type=file; \
    location:="bin/tools/extensions/ws-customPasswordEncryption/customEncryption.jar”
    Subsystem-Description: MyCompany custom password encryption
  4. 將特性安裝到使用者產品延伸位置之後,利用特性名稱來配置 server.xml 檔。
    <featureManager>
       ...
       <feature>usr:customPasswordEncryption-1.0</feature>
    </featureManager>

    如果要驗證伺服器執行時期環境安裝成功,請驗證 message.log 檔中有記載 CWWKS1850I 訊息。

  5. 選用:建立延伸資訊清單檔,如果要在指令行公用程式(例如 SecurityUtility)中外掛 customPasswordEncryption 提供者,則需要這個檔案。延伸資訊清單檔是一個 JAR 檔,內含 MANIFEST.MF 檔。延伸資訊清單的必要標頭如下:
    • Require-Bundle:定義需要載入的軟體組清單。其格式與 OSGi 軟體組資訊清單檔相同。
    • IBM-ImplementationClass:定義實作類別,以實作 com.ibm.wsspi.security.crypto.CustomPasswordEncryption 介面。

    以下是 MANIFEST.MF 檔範例

    Require-Bundle: com.mycompany.custom; version="[1,1.0.100)"; location="usr/extension/lib"
    IBM-ImplementationClass: com.mycompany.custom.CusomPasswordEncryptionImpl
    使用 jar 指令,來包裝延伸資訊清單檔。例如:
    jar cfm customEncryption.jar MANIFEST.MF

    將 JAR 檔放在 wlp/bin/tools/extensions/ws-customPasswordEncryption 中。 請確定延伸資訊清單檔的位置位於特性資訊清單檔的 Subsystem-Content 標頭區段中。

    這些檔案需要位於特定的目錄中。下列描述檔案的關係:
    圖 1. customPassworedEncryption 提供者檔案描述檔案與其目錄之關係的流程圖。
  6. 選用:如果完成步驟 5,您可以使用下列指令行公用程式指令,來驗證安裝成功。
    wlp/bin/securityUtility encode --listCustom
    預期的輸出是 customPasswordEncryption 的資訊。例如:
    [{"name":"custom","featurename":"usr:customPasswordEncryption-1.0","description":"MyCompany custom password encryption"}]

    請確定所有必要的檔案都存在,且檔案的內容精確。


指示主題類型的圖示 作業主題

檔名:twlp_developing_custom_passwd_encrypt.html