定義 OAuth 服務提供者

OAuth 服務提供者是 OAuth 的一組具名配置選項。 提供者的 id 或名稱指定在授權和記號端點之入埠要求的 URL 中。 當處理要求時,會使用這個提供者的配置選項集。 這個程序使含有單一端點 Servlet 的伺服器能夠有效提供多重 OAuth 配置。 比方說,https://my.company.com:8021/oauth2/endpoint/photoShare/authorize URL 就是利用定義給名稱為 photoShare 之 OAuth 提供者的該組 OAuth 配置選項來進行處理。 https://my.company.com:8021/oauth2/endpoint/calendarAuthz/authorize URL 是使用針對名稱為 calendarAuthz的 OAuth 提供者所定義的 OAuth 配置選項集來處理。

關於這項作業

OAuth 服務提供者是利用 server.xml 檔中的 oauthProvider 元素來定義。 您可以編輯 server.xml 檔,或使用 WebSphere® Application Server Development Tools for Liberty,來定義 OAuth 服務提供者。 這項作業說明如何定義最小的 OAuth 配置。

程序

  1. 新增 oauth-2.0transportSecurity-1.0 特性。
    OAuth 是一個安全通訊協定,因此需要 SSL。 在 Liberty上,您必須使用 keyStore 元素來提供 SSL 的金鑰儲存庫密碼。 金鑰儲存庫沒有預設的密碼。
    <featureManager>
      <feature>oauth-2.0</feature>
      <feature>transportSecurity-1.0</feature>
    </featureManager>
  2. 使用 oauth-roles 元素來設定 OAuth Web 應用程式的角色對映。
    OAuth 是一個 HTTP 型通訊協定,另外還提供一個 Web 應用程式來處理授權和記號端點。 Web 應用程式是內建的,當您指定 oauth-2.0 特性時,會自動啟動。 不過,您必須將 authenticated 角色對映至一或多個使用者、群組或特殊主體。 另外還提供一個用來管理用戶端配置的 clientManager 角色,但 OAuth 授權的運作並不需要對映這個角色。
    <oauth-roles>
      <authenticated>
        <user name="testuser"/>
      </authenticated>
    </oauth-roles>
  3. 使用 oauthProvider 元素來定義一或多個提供者。
    提供者必須定義至少一個用戶端。 您可以透過下列方式來定義用戶端:
    • 在本端使用 localStoreclient 元素
    • 在關聯式資料庫中使用 databaseStore 元素
    • 在自訂 OAuthStore 實作中使用 customStore 元素
    <oauthProvider id="SampleProvider" filter="request-url%=ssodemo">
      <localStore>
        <client name="client01" secret="{xor}LDo8LTor"
                displayname="Test client number 1"
                redirect="http://localhost:1234/oauthclient/redirect.jsp"
                enabled="true" />
      </localStore>
    </oauthProvider>
  4. 指定 ldapRegistry-3.0 特性和 ldapRegistry 配置元素來定義使用者登錄,或指定 basicRegistry 配置元素來定義基本登錄。
    <basicRegistry id="basic" realm="BasicRealm">
      <user name="testuser" password="testuserpwd" />
    </basicRegistry>
  5. allowFailOverToBasicAuth Web 應用程式安全內容設為 true
    <webAppSecurity allowFailOverToBasicAuth="true" />

結果

您已定義最小 OAuth 配置。

範例

下列範例顯示 server.xml 檔範例,其中定義一個具有一個用戶端的簡式 OAuth 提供者:
<server>

  <featureManager>
    <feature>oauth-2.0</feature>
    <feature>transportSecurity-1.0</feature>
  </featureManager>

  <keyStore password="keyspass" />

  <oauth-roles>
    <authenticated>
      <user name="testuser"/>
    </authenticated>
  </oauth-roles>

  <oauthProvider id="SampleProvider" filter="request-url%=ssodemo">
    <localStore>
      <client name="client01" secret="{xor}LDo8LTor"
              displayname="Test client number 1"
              redirect="http://localhost:1234/oauthclient/redirect.jsp"
              enabled="true" />
    </localStore>
  </oauthProvider>

  <webAppSecurity allowFailOverToBasicAuth="true" />

  <basicRegistry id="basic" realm="BasicRealm">
    <user name="testuser" password="testuserpwd" />
  </basicRegistry>

</server>