入门:在 Liberty 中配置 OpenID Connect 提供者和 OpenID Connect 客户机

此端到端示例可帮助您熟悉如何在 Liberty 中配置 OpenID Connect (OIDC),因为它包含从安装 Liberty 到配置 OpenID Connect 的全过程。结果是,客户机应用程序可依赖于 OpenID Connect 提供者所进行的认证来验证用户身份。

关于此任务

您可以通过完成以下过程中的这些步骤,设置 OpenID Connect 提供者和 OpenID Connect 依赖方:
  1. 安装 Liberty。
  2. 创建两个服务器。
  3. 下载并安装 IBM® 提供的包含 Snoop Servlet 的测试应用程序。
  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。在生产环境中,您可能会改为使用其他注册表(例如 LDAP 用户注册表),因为用户必须向 OpenID Connect 提供者进行认证。OpenID Connect 提供者需要具有注册表。OpenID Connect 依赖方不需要注册表,因为它信任自己从 OpenID Connect 提供者获取的用户工件(例如令牌)。
  • 缺省密钥库使用明文密码。您可以使用 securityUtility 命令生成已编码的加密密码。
  • 为简单起见,使用 HS256 签名算法。OpenID Connect 规范中并未强制要求支持 HS256 签名算法。许多 OpenID 提供者不支持 HS256 签名算法。
  • 权宜之计是,将测试应用程序放置在 dropins 目录中,但在生产环境中要避免这样做。
  • 已启用 OIDC 跟踪,以便您可以在遇到错误时查看服务器的 trace.log 文件。

过程

  1. 安装 WebSphere® Liberty
    1. WASdev 下载 WebSphere Liberty .zip 文件,并将其保存到一个新目录中以用于您的安装。
    2. 解压缩下载的文件。
      unzip wlp-webProfile7-17.0.0.4.zip

      本示例中采用的所有路径都与为您的安装创建的目录相关。

  2. 下载样本 Liberty 缺省应用程序。

    此缺省应用程序位于 libertyDefaultApplication.ear 文件中,且包含本示例过程中使用的 Snoop Servlet。

  3. 安装 Liberty OpenID Connect 客户机和 Liberty OpenID Connect 提供者功能部件。
    wlp/bin 目录运行以下命令:
    installUtility install openidConnectClient-1.0
    installUtility install openidConnectServer-1.0
  4. 创建 Liberty 服务器。
    1. 创建 OpenID Connect 依赖方服务器。
      从 wlp/bin 目录运行以下命令:
      server create Liberty_RP
    2. 创建 OpenID Connect 提供者服务器。
      从 wlp/bin 目录运行以下命令:
      server create Liberty_OP
  5. 为 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.cli.e.nt.*=all" traceFileName="trace.log" maxFileSize="20" maxFiles="10" traceFormat="BASIC" />
      </server>
  6. 为 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>
  7. 创建缺省密钥库和密钥。
    wlp/bin 目录运行以下命令:
    server start Liberty_RP
    server stop Liberty_RP
    server start Liberty_OP
    server stop Liberty_OP
  8. 将 Liberty_OP 服务器的签署者证书添加到 Liberty_RP 服务器中作为可信证书。
    1. 从 Liberty_OP 服务器的密钥库中导出个人证书。
      1. 切换到 wlp/usr/servers/Liberty_OP/resources/security 目录。
      2. 导出 Liberty_OP 服务器的缺省个人证书:
        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.jks -storepass Password -alias libertyop -file libertyOP.cer -noprompt
    3. 可选: 查看 Liberty_RP 服务器的密钥库:
      keytool -list -v -keystore key.jks -storepass Password
  9. 在 Liberty_RP 服务器上安装 Liberty 缺省应用程序。

    将先前步骤中下载的 libertyDefaultApplication.ear 文件复制到 wlp/usr/servers/Liberty_RP/dropins 目录。

  10. 启动 Liberty_OP 服务器和 Liberty_RP 服务器。
    wlp/bin 目录运行以下命令:
    server start Liberty_RP
    server start Liberty_OP
  11. 打开 Liberty_RP 服务器的登录页面。
    1. 在浏览器的地址栏中,输入 Liberty_RP 服务器的 URL:
      http://localhost:9081/snoop
    2. Enter 键。

      此时会显示登录窗口。

      要点: 如果系统显示有关此连接不安全的浏览器警告,请在浏览器中完成相应步骤以允许此连接。
  12. 登录 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 服务器,然后执行前面关于登录的步骤。
  13. 使服务器名称标准化。
    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. 更改 client 元素 (oauthProvider/localStore/client) 的 redirect 属性,以使用您的标准服务器主机名代替 localhost 主机名。
        以下示例代码指定标准服务器主机名:
        <client name="oidcclient" secret="password" scope="openid" redirect="https://wks1.acme.com:9444/oidcclient/redirect/RP" />
      4. 保存该文件。

        保存该文件时,将会触发 Liberty 配置更改,因此您不必重新启动服务器。

  14. 通过使用标准主机名打开登录页面。
    1. 在浏览器的地址栏中,输入包含 Liberty_RP 服务器的 URL:
      http://<host_name>:9081/snoop
    2. Enter 键。

      此时会显示登录窗口。

      要点: 如果系统显示有关此连接不安全的浏览器警告,请在浏览器中完成相应步骤以允许此连接。
  15. 登录 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 - 请求/客户机信息”页面上。

结果

您拥有一个 Liberty 服务器,它包含受 OpenID Connect 依赖方保护的 Snoop Servlet。OpenID Connect 依赖方使用 OpenID Connect 提供者进行认证。

表 1. 关于结果的问题
问题 答案
为什么测试应用程序受 OpenID Connect 依赖方保护,即使在 OpenID Connect 配置中未提及 Snoop Servlet 和 libertyDefaultApplication 测试应用程序时也是如此? 测试应用程序受保护是因为,其 web.xml 文件包含所有启用了 Java™ EE 安全性的容器都支持的安全性约束。当 OpenID Connect 依赖方正在 Liberty 上运行时,缺省情况下,依赖方会尝试对所有发往任何具有安全性约束的 URL 的请求进行授权。因为测试应用程序对所使用的 URL 具有安全性约束,所以依赖方对请求进行授权。

用于指示主题类型的图标 任务主题

文件名:twlp_config_oidc_pc_examp_beginner.html