入門: 在 Liberty 中配置 OpenID Connect 提供者和用戶端

這個端對端範例有助您熟悉在 Liberty 中配置 OpenID Connect (OIDC),因為它會引導您安裝 Liberty,一直到配置 OpenID Connect 為止。 其結果會是用戶端應用程式可以倚賴來自「OpenID Connect 提供者」的鑑別,來驗證使用者的身分。

關於這項作業

您可以完成下列程序中的步驟,來設定「OpenID Connect 提供者」和「OpenID Connect 依賴方」:
  1. 安裝 Liberty。
  2. 建立兩個伺服器。
  3. 下載並安裝包含 Snoop Servlet 的 IBM®提供測試應用程式。
  4. 使用 localhost 主機名稱,將「OpenID Connect 提供者」和「OpenID Connect 依賴方」配置成彼此通訊。
  5. 登入。
  6. 編輯「OpenID Connect 提供者」和「OpenID Connect 依賴方」配置,以使用您實際的主機名稱。
  7. 登入。
下列各點說明程序中的一些過程:
  • 「OpenID Connect 依賴方」伺服器和「OpenID Connect 提供者」伺服器兩者都在相同系統上執行。
  • 「OpenID Connect 依賴方」伺服器和「OpenID Connect 提供者」伺服器一開始都使用 localhost 主機名稱。 當主機容許外來連線指向它時,這樣做反而可克服您的系統可能因而遇到的問題。
  • 檔案型登錄會寫在「OpenID Connect 提供者」伺服器的 server.xml 檔中。 對於使用者名稱與密碼,它含有兩筆項目。 一筆針對 Jackson 使用者名稱和 Password 密碼。 另一筆針對 Andrea 使用者名稱和 Password 密碼。 在正式作業環境中,由於使用者會接受「OpenID Connect 提供者」的鑑別,您可能改用 LDAP 使用者登錄(舉例來說)等。 「OpenID Connect 提供者」需要有一筆登錄。 「OpenID Connect 依賴方」就不需要有登錄,因為它信任從「OpenID Connect 提供者」取得的使用者構件(例如,記號)。
  • 預設金鑰儲存庫使用純文字密碼。 您可以使用 securityUtility 指令來產生已編碼的加密密碼。
  • 為求簡便,會使用 HS256 簽章演算法。 在 OpenID Connect 規格中,不一定要支援 HS256 簽章演算法。 很多「OpenID 提供者」不支援 HS256 簽章演算法。
  • 為了方便起見,您將測試應用程式放置在 dropins 目錄中,但在正式作業環境中避免這種作法。
  • OIDC 追蹤已啟用,以便您可以在發生錯誤時檢視伺服器的 trace.log 檔案。

程序

  1. 安裝 Liberty 伺服器。
    1. 下載 WebSphere® Application Server Liberty .zip 檔案。
      如果您具有 IBM 登入 具有授權,則可以從 IBM Fix Central下載 Liberty 。 或者,您也可以下載適用於開發人員的 Liberty 。 選擇下列其中一個選項:
      選項 1: 透過 IBM 登入,從 IBM Fix Central 取得 Liberty
      1. 導覽至 Fix Central
      2. 過濾內容 區段中,在 And Fix type 過濾準則下,選取 修正套件。 本節包括 修正套件(小寫) 及 Fix Pack的選項。 選取小寫 修正套件 選項,而不是 修正套件
      3. 並套用至 過濾準則下,捲動至清單結尾,然後按一下最高修正套件號碼。 例如:23.0.0.3
      4. 按一下提交
      5. 過濾器修正程式詳細資料 欄位中,指定下列內容。
        wlp-webprofile8
      6. 從產生的套件清單中,選取並下載適當的工作站套件,例如 wlp-webProfile8-23.0.0.3wlp-webProfile8-java8-win-x86_64-23.0.0.3

        如果您尚未登入,系統會提示您輸入 IBM ID 認證。

      選項 2: 為開發人員取得 Liberty
      1. 導覽至 適用於開發人員的WebSphere Liberty
      2. 在頁面尾端尋找 下載套件 區段。
      3. 下載最符合您系統的套件。
    2. 解壓縮所下載的檔案。
      例如,執行下列指令:
      unzip wlp-webProfile8-23.0.0.3
    3. wlp/bin 目錄執行下列指令:
      featureManager install openidConnectClient-1.0
      featureManager install openidConnectServer-1.0
  2. 下載 範例 Liberty 預設應用程式。

    此預設應用程式位於 libertyDefaultApplication.ear 檔案中,並包含您在此範例程序中使用的 Snoop Servlet。

    如果在此步驟中按一下下載鏈結未將檔案下載至您的電腦,請在鏈結上按一下滑鼠右鍵,然後選取瀏覽器的 下載儲存 選項。 或者,導覽至 上層目錄,用滑鼠右鍵按一下 libertyDefaultApplication.ear 檔案,然後選取瀏覽器的 下載儲存 選項。

  3. 建立 Liberty 伺服器。
    1. 建立「OpenID Connect 依賴方」伺服器。
      wlp/bin 目錄執行下列指令:
      server create Liberty_RP
    2. 建立「OpenID Connect 提供者」伺服器。
      wlp/bin 目錄執行下列指令:
      server create Liberty_OP
  4. 建立 OpenID Connect 依賴方伺服器的 server.xml 檔。
    1. 切換至 wlp/usr/servers/Liberty_RP 目錄。
    2. server.xml 檔案的內容取代為下列內容:
      <server description="Liberty_RP">
          <featureManager>
              <feature>openidConnectClient-1.0</feature>
              <feature>appSecurity-2.0</feature>
          </featureManager>
          <!-- To access this server from anything other than localhost or the loopback address, add a host attribute to the following element, e.g. host="*" -->
          <httpEndpoint httpPort="9081" httpsPort="9444" id="defaultHttpEndpoint"/>
          <keyStore id="defaultKeyStore" password="Password"></keyStore>
          <openidConnectClient id="RP"
              clientId="oidcclient"
              clientSecret="password"
              authorizationEndpointUrl="https://localhost:9443/oidc/endpoint/OP/authorize"
              tokenEndpointUrl="https://localhost:9443/oidc/endpoint/OP/token" >
          </openidConnectClient>
          <logging  traceSpecification="*=info:
      com.ibm.ws.security.*=all:com.ibm.ws.webcontainer.security.*=all:com.ibm.oauth.*=all:com.ibm.wsspi.security.oauth20.*=all:com.ibm.ws.transport.http.*=all:org.apache.http.client.*=all" traceFileName="trace.log" maxFileSize="20" maxFiles="10" traceFormat="BASIC" />
      </server>
  5. 建立 OpenID Connect 提供者的 server.xml 檔。
    1. 切換至 wlp/usr/servers/Liberty_OP 目錄。
    2. server.xml 檔案的內容取代為下列內容:
      <server description="Liberty_OP">
          <featureManager>
              <feature>openidConnectServer-1.0</feature>
              <feature>appSecurity-2.0</feature>
          </featureManager>
          <!-- To access this server from anything other than localhost or the loopback address, add a host attribute to the following element, e.g. host="*" -->
          <httpEndpoint httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint"/>
          <keyStore id="defaultKeyStore" password="Password"/>
          <basicRegistry>
              <user name="Jackson" password="Password"/>
              <user name="Andrea" password="Password"/>
          </basicRegistry>
          <openidConnectProvider id="OP" oauthProviderRef="oauth"/>
          <oauthProvider id="oauth">
              <localStore>
                  <!-- The default redirect URL pattern is: https://<hostname>:<sslport>/oidcclient/redirect/<openidConnectClientID> -->
                  <client name="oidcclient"  secret="password" scope="openid" redirect="https://localhost:9444/oidcclient/redirect/RP" />
              </localStore>
          </oauthProvider>
          <!-- To grant all authenticated users access to the OIDC protected resource, grant them the oauth-role authenticated -->
          <oauth-roles>
              <authenticated>
                  <special-subject type="ALL_AUTHENTICATED_USERS"/>
              </authenticated>
          </oauth-roles>
          <logging  traceSpecification="*=info:
      com.ibm.ws.security.*=all:com.ibm.ws.webcontainer.security.*=all:com.ibm.oauth.*=all:com.ibm.wsspi.security.oauth20.*=all:com.ibm.ws.transport.http.*=all:org.apache.http.client.*=all" traceFileName="trace.log" maxFileSize="20" maxFiles="10" traceFormat="BASIC" />
      </server>
  6. 建立預設金鑰儲存庫和金鑰。
    wlp/bin 目錄執行下列指令:
    server start Liberty_RP
    server stop Liberty_RP
    server start Liberty_OP
    server stop Liberty_OP
  7. 新增 Liberty_OP 伺服器的簽章者憑證至 Liberty_RP 伺服器,以作為授信憑證。
    1. 從 Liberty_OP 伺服器的金鑰儲存庫匯出個人憑證。
      1. 將目錄切換至 wlp/usr/servers/Liberty_OP/resources/security
      2. 匯出 Liberty_OP 伺服器的預設個人憑證:
        keytool -exportcert -keystore key.p12 -storepass Password -alias default -file libertyOP.cer
        如果您透過 19.0.0.2 使用 JKS 金鑰儲存庫,請使用預設金鑰儲存庫檔,例如 key.jks 檔。
        keytool -exportcert -keystore key.jks -storepass Password -alias default -file libertyOP.cer
        Liberty_OP 伺服器的這份預設個人憑證是簽章者憑證。
    2. 將簽章者憑證從 Liberty_OP 伺服器匯入至 Liberty_RP 伺服器的金鑰儲存庫。
      1. libertyOP.cer 檔案複製到 wlp/usr/servers/Liberty_RP/resources/security 目錄。
      2. 將目錄切換至 wlp/usr/servers/Liberty_RP/resources/security
      3. 執行下列指令以執行匯入:
        keytool -importcert -keystore key.p12 -storepass Password -alias libertyop -file libertyOP.cer -noprompt
        如果您透過 19.0.0.2 使用 JKS 金鑰儲存庫,請使用預設金鑰儲存庫檔,例如 key.jks 檔。
        keytool -importcert -keystore key.jks -storepass Password -alias libertyop -file libertyOP.cer -noprompt
    3. 選用: 檢視 Liberty_RP 伺服器的金鑰儲存庫:
      keytool -list -v -keystore key.p12 -storepass Password
      如果您透過 19.0.0.2 使用 JKS 金鑰儲存庫,請使用預設金鑰儲存庫檔,例如 key.jks 檔。
      keytool -list -v -keystore key.jks -storepass Password
  8. 將 Liberty 預設應用程式安裝到 Liberty_RP 伺服器。

    將您在前一個步驟中下載的 libertyDefaultApplication.ear 檔案複製到 wlp/usr/servers/Liberty_RP/dropins 目錄。

  9. 啟動 Liberty_OP 伺服器和 Liberty_RP 伺服器。
    wlp/bin 目錄執行下列指令:
    server start Liberty_RP
    server start Liberty_OP
  10. 開啟 Liberty_RP 伺服器的登入頁面。
    1. 在瀏覽器的位址列中,輸入 Liberty_RP 伺服器的 URL:
      http://localhost:9081/snoop
    2. Enter鍵。

      會顯示登入視窗。

      重要事項: 如果您收到瀏覽器警告,指出連線不安全,請在瀏覽器中執行步驟以容許連線。
  11. 登入 Liberty_OP 伺服器,並授權 Liberty_RP 伺服器。
    1. 讓使用者接受 Liberty_OP 伺服器的鑑別。
      1. 使用 Jackson 使用者名稱和 Password 密碼來登入。
      2. 按一下登入

        會顯示「容許用戶端存取下列資料」頁面,且其中勾選了 openid 項目。

    2. 授權 Liberty_OP 伺服器,以容許 Liberty_RP 伺服器存取使用者的個人資訊。

      按一下容許一次容許,請記住我的決定

      重要事項: 如果您收到瀏覽器警告,指出連線不安全,請在瀏覽器中執行步驟以容許連線。 您可能會收到這則警告,因為 Liberty_RP 伺服器和 Liberty_OP 伺服器兩者上的 httpEndpoint 元素都只包含自簽憑證。
    如果您登入成功,Snoop Servlet 會顯示在「Snoop Servlet - 要求/用戶端資訊」頁面上。
    重要事項: 若要重新登入,請至少完成下列其中一個選項:
    • 從不同瀏覽器產品登入。
    • 清除您在現行瀏覽器中的 Cookie,並遵循上述有關登入的步驟。
    • 關閉瀏覽器中您用來登入的所有視窗,重新開啟瀏覽器,然後登入。
    • 重新啟動 Liberty_RP 伺服器,然後遵循上述有關登入的步驟。
  12. 提供完整的伺服器名稱。
    1. 將 Liberty_RP 伺服器 server.xml 檔中的 localhost 主機名稱變更為其完整名稱。
      1. 編輯 wlp/usr/servers/Liberty_RP/server.xml 檔案。
      2. 新增下列屬性至 httpEndpoint 元素:host="*"
      3. 變更 openidConnectClient 元素的 authorizationEndpointUrl 和 tokenEndpointUrl 屬性,以使用您的伺服器主機名稱,而非 localhost 主機名稱。

        下列範例程式碼指定完整的伺服器主機名稱:

        <openidConnectClient id="RP"
            clientId="oidcclient"
            clientSecret="password"
            authorizationEndpointUrl="https://wks1.acme.com:9443/oidc/endpoint/OP/authorize"
            tokenEndpointUrl="https://wks1.acme.com:9443/oidc/endpoint/OP/token" >
        </openidConnectClient>
      4. 儲存檔案。

        當您儲存檔案時,就會觸發 Liberty 配置變更,因而您不必重新啟動伺服器。

    2. 將 Liberty_OP 伺服器 server.xml 檔中的 localhost 主機名稱變更為其完整名稱。
      1. 編輯 wlp/usr/servers/Liberty_OP/server.xml 檔案。
      2. 新增下列屬性至 httpEndpoint 元素:host="*"
      3. 變更用戶端元素 (oauthProvider/localStore/client) 的 redirect 屬性,以使用完整的伺服器主機名稱,而非 localhost 主機名稱。
        下列範例程式碼指定完整的伺服器主機名稱:
        <client name="oidcclient" secret="password" scope="openid" redirect="https://wks1.acme.com:9444/oidcclient/redirect/RP" />
      4. 儲存檔案。

        當您儲存檔案時,就會觸發 Liberty 配置變更,因而您不必重新啟動伺服器。

  13. 使用完整的主機名稱來開啟登入頁面。
    1. 在瀏覽器的位址列中,輸入包含 Liberty_RP 伺服器的 URL:
      http://<host_name>:9081/snoop
    2. Enter鍵。

      會顯示登入視窗。

      重要事項: 如果您收到瀏覽器警告,指出連線不安全,請在瀏覽器中執行步驟以容許連線。
  14. 登入 Liberty_OP 伺服器,並授權 Liberty_RP 伺服器。
    1. 讓使用者接受 Liberty_OP 伺服器的鑑別。
      1. 使用 Jackson 使用者名稱和 Password 密碼來登入。
      2. 按一下登入

        會顯示「容許用戶端存取下列資料」頁面,且其中勾選了 openid 項目。

    2. 授權 Liberty_OP 伺服器,以容許 Liberty_RP 伺服器存取使用者的個人資訊。

      按一下容許一次容許,請記住我的決定

      重要事項: 如果您收到瀏覽器警告,指出連線不安全,請在瀏覽器中執行步驟以容許連線。 您可能會收到這則警告,因為 Liberty_RP 伺服器和 Liberty_OP 伺服器兩者上的 httpEndpoint 元素都只包含自簽憑證。
    如果您登入成功,Snoop Servlet 會顯示在「Snoop Servlet - 要求/用戶端資訊」頁面上。

結果

您有了一部含有 Snoop Servlet 的 Liberty 伺服器,且其受到「OpenID Connect 依賴方」的保護。 「OpenID Connect 依賴方」會使用您的「OpenID Connect 提供者」來進行鑑別。

表 1. 關於結果的問題
問題 回答
儘管 OpenID Connect 配置中沒有提及 Snoop Servlet 和 libertyDefaultApplication 測試應用程式,為何測試應用程式還是會受到「OpenID Connect 依賴方」的保護? 測試應用程式受到保護,因為其 web.xml 檔包含任何已啟用 Java™ EE 安全之儲存器支援的安全限制。 當「OpenID Connect 依賴方」在 Liberty 上執行時,依預設,只要要求是發給具有安全限制的任何 URL,「依賴方」會嘗試授權該要求。 由於測試應用程式在所要使用的 URL 上具有安全限制,因此「依賴方」會授權要求。