這個端對端範例有助您熟悉在 Liberty 中配置 OpenID Connect (OIDC),因為它會引導您安裝 Liberty,一直到配置
OpenID Connect 為止。其結果會是用戶端應用程式可以倚賴來自「OpenID Connect 提供者」的鑑別,來驗證使用者的身分。
關於這項作業
您可以完成下列程序中的步驟,來設定「OpenID Connect 提供者」和「OpenID Connect 依賴方」:
- 安裝 Liberty。
- 建立兩個伺服器。
- 下載並安裝 IBM® 提供的測試應用程式,此應用程式含有 Snoop Servlet。
- 使用 localhost 主機名稱,將「OpenID Connect 提供者」和「OpenID Connect 依賴方」配置成彼此通訊。
- 登入。
- 編輯「OpenID Connect 提供者」和「OpenID Connect 依賴方」配置,以使用您實際的主機名稱。
- 登入。
下列各點說明程序中的一些過程:
- 「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 檔。
程序
- 安裝 WebSphere® Liberty。
- 將 WebSphere Liberty
.zip 檔從 WASdev 下載到您安裝架構的新目錄。
- 解壓縮所下載的檔案。
unzip wlp-webProfile7-17.0.0.4.zip
本例中遵循的任何路徑,都是您針對安裝架構所建立之目錄的相對路徑。
- 下載範例 Liberty 預設應用程式。
此預設應用程式位於 libertyDefaultApplication.ear 檔,並且含有您在這個範例程序中使用的 Snoop Servlet。
- 安裝「Liberty OpenID Connect 用戶端」和「Liberty OpenID Connect 提供者」特性。
從
wlp/bin 目錄執行下列指令:
installUtility install openidConnectClient-1.0
installUtility install openidConnectServer-1.0
- 建立 Liberty 伺服器。
- 建立「OpenID Connect 依賴方」伺服器。
從 wlp/bin 目錄執行下列指令:
server create Liberty_RP
- 建立「OpenID Connect 提供者」伺服器。
從 wlp/bin 目錄執行下列指令:
server create Liberty_OP
- 為「OpenID Connect 依賴方」伺服器建立 server.xml 檔。
- 切換至 wlp/usr/servers/Liberty_RP 目錄。
- 以下列內容取代 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.cli.e.nt.*=all" traceFileName="trace.log" maxFileSize="20" maxFiles="10" traceFormat="BASIC" />
</server>
- 為「OpenID Connect 提供者」建立 server.xml 檔。
- 切換至 wlp/usr/servers/Liberty_OP 目錄。
- 以下列內容取代 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>
- 建立預設金鑰儲存庫和金鑰。
從
wlp/bin 目錄執行下列指令:
server start Liberty_RP
server stop Liberty_RP
server start Liberty_OP
server stop Liberty_OP
- 新增 Liberty_OP 伺服器的簽章者憑證至 Liberty_RP 伺服器,以作為授信憑證。
- 從 Liberty_OP 伺服器的金鑰儲存庫匯出個人憑證。
- 將目錄切換至 wlp/usr/servers/Liberty_OP/resources/security。
- 匯出 Liberty_OP 伺服器的預設個人憑證:
keytool -exportcert -keystore key.jks -storepass Password -alias default -file libertyOP.cer
Liberty_OP 伺服器的這份預設個人憑證是簽章者憑證。
- 將簽章者憑證從 Liberty_OP 伺服器匯入至 Liberty_RP 伺服器的金鑰儲存庫。
- 將 libertyOP.cer 檔複製到 wlp/usr/servers/Liberty_RP/resources/security 目錄。
- 將目錄切換至 wlp/usr/servers/Liberty_RP/resources/security。
- 執行下列指令以執行匯入:
keytool -importcert -keystore key.jks -storepass Password -alias libertyop -file libertyOP.cer -noprompt
- 選擇性的: 檢視 Liberty_RP 伺服器的金鑰儲存庫:
keytool -list -v -keystore key.jks -storepass Password
- 將 Liberty 預設應用程式安裝到 Liberty_RP 伺服器。
將您在先前步驟中下載的 libertyDefaultApplication.ear 檔複製到
wlp/usr/servers/Liberty_RP/dropins 目錄。
- 啟動 Liberty_OP 伺服器和 Liberty_RP 伺服器。
從
wlp/bin 目錄執行下列指令:
server start Liberty_RP
server start Liberty_OP
- 開啟 Liberty_RP 伺服器的登入頁面。
- 在瀏覽器的位址列中,輸入 Liberty_RP 伺服器的 URL:
http://localhost:9081/snoop
- 按 Enter 鍵。
會顯示登入視窗。
重要: 如果您收到瀏覽器警告,指出連線不安全,請在瀏覽器中逐一執行各個步驟,以容許連線。
- 登入 Liberty_OP 伺服器,並授權 Liberty_RP 伺服器。
- 讓使用者接受 Liberty_OP 伺服器的鑑別。
- 使用 Jackson 使用者名稱和 Password 密碼來登入。
- 按一下登入。
會顯示「容許用戶端存取下列資料」頁面,且其中勾選了 openid 項目。
- 授權 Liberty_OP 伺服器容許 Liberty_RP 伺服器存取使用者的個人資訊。
按一下容許一次或容許,請記住我的決定。
重要: 如果您收到瀏覽器警告,指出連線不安全,請在瀏覽器中逐一執行各個步驟,以容許連線。您可能會收到這則警告,因為 Liberty_RP 伺服器和 Liberty_OP 伺服器兩者上的
httpEndpoint 元素都只包含自簽憑證。
如果您登入成功,Snoop Servlet 會顯示在「Snoop Servlet -
要求/用戶端資訊」頁面上。重要: 如果要再次登入,請至少完成下列其中一個選項:
- 從不同瀏覽器產品登入。
- 清除您在現行瀏覽器中的 Cookie,並遵循上述有關登入的步驟。
- 關閉瀏覽器中您用來登入的所有視窗,重新開啟瀏覽器,然後登入。
- 重新啟動 Liberty_RP 伺服器,然後遵循上述有關登入的步驟。
- 提供完整的伺服器名稱。
- 將 Liberty_RP 伺服器的 server.xml 檔中,將 localhost 主機名稱變更為其完整名稱。
- 編輯 wlp/usr/servers/Liberty_RP/server.xml 檔。
- 新增下列屬性至 httpEndpoint 元素:host="*"。
- 變更 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>
- 儲存檔案。
當您儲存檔案時,就會觸發 Liberty 配置變更,因而您不必重新啟動伺服器。
- 將 Liberty_OP 伺服器的 server.xml 檔中,將 localhost 主機名稱變更為其完整名稱。
- 編輯 wlp/usr/servers/Liberty_OP/server.xml 檔。
- 新增下列屬性至 httpEndpoint 元素:host="*"。
- 變更用戶端元素 (oauthProvider/localStore/client) 的 redirect 屬性,以使用完整的伺服器主機名稱,而非
localhost 主機名稱。
下列範例程式碼指定完整的伺服器主機名稱:
<client name="oidcclient" secret="password" scope="openid" redirect="https://wks1.acme.com:9444/oidcclient/redirect/RP" />
- 儲存檔案。
當您儲存檔案時,就會觸發 Liberty 配置變更,因而您不必重新啟動伺服器。
- 使用完整的主機名稱來開啟登入頁面。
- 在瀏覽器的位址列中,輸入包含 Liberty_RP 伺服器的 URL:
http://<host_name>:9081/snoop
- 按 Enter 鍵。
會顯示登入視窗。
重要: 如果您收到瀏覽器警告,指出連線不安全,請在瀏覽器中逐一執行各個步驟,以容許連線。
- 登入 Liberty_OP 伺服器,並授權 Liberty_RP 伺服器。
- 讓使用者接受 Liberty_OP 伺服器的鑑別。
- 使用 Jackson 使用者名稱和 Password 密碼來登入。
- 按一下登入。
會顯示「容許用戶端存取下列資料」頁面,且其中勾選了 openid 項目。
- 授權 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 上具有安全限制,因此「依賴方」會授權要求。 |