2.2. IDP 和 SP 设置和配置


本节介绍将应用设置为 SP 或 IDP,以及设置 JBoss EAP 实例以托管这些应用。

2.2.1. 设置 IDP

要将应用程序设置为 IDP,必须执行以下步骤:

注意

在创建和部署应用之前,应创建和配置安全域。

  1. 为 IDP 创建安全域。

    IDP 处理为其凭证带来挑战的主体,处理该主体的验证和授权,并根据结果发出适当的 SAML v2 安全断言。这需要使用安全域配置身份存储。创建此安全域和身份存储的唯一要求是其已正确定义了身份验证和授权机制。这意味着,可以使用许多不同的身份存储 SAS-svc(如属性文件、数据库或 LDAP全部)及其关联的登录模块来支持 IDP 应用。有关安全域的更多信息,请参阅 JBoss EAP 安全架构指南中的安全 https://access.redhat.com/documentation/en-us/red_hat_jboss_enterprise_application_platform/7.3/html-single/security_architecture/#security_domains 域部分

    以下示例通过属性文件使用简单的 UsersRoles 登录模块。

    用于创建安全域的管理 CLI 命令

    /subsystem=security/security-domain=idp:add(cache-type=default)
    
    /subsystem=security/security-domain=idp/authentication=classic:add
    
    /subsystem=security/security-domain=idp/authentication=classic/login-module=UsersRoles:add(code=UsersRoles,flag=required,module-options=[usersProperties=${jboss.server.config.dir}/idp-users.properties,rolesProperties=${jboss.server.config.dir}/idp-roles.properties])
    
    reload

    UsersRoles login 模块利用属性文件来存储用户/密码和用户/角色信息。有关 UsersRoles 模块的更多信息,请参阅 JBoss EAP 登录模块参考。在本例中,属性文件包含以下内容:

    idp-users.properties

    Eric=samplePass
    Alan=samplePass

    idp-roles.properties

    Eric=All
    Alan=

  2. 为 IDP 配置 web.xml 文件。

    IDP 的 web.xml 文件必须包含以下内容:

    • 带有 < web-resource-collection> 的 <security-constraint >,其中包含一个 <url-pattern>,它映射到安全区域的 URL 模式。另外,<security-constraint> 也可以包含 <auth-constraint> 来 替代允许的角色。
    • 为 FORM 身份验证 配置 <login-config>
    • 如果在 <auth-constraint> 中指定了任何角色,则应在 <security-role> 中定义这些角色。
    • (可选)登录表单使用的资源(如映像和样式)可以通过不被保护的额外安全性约束来指定,以便在身份验证之前访问这些资源,例如在登录页面上。

    <security-constraint><security-role> 元素可让管理员根据 URL 模式和角色设置受限或不受限制的区域。这允许保护或不受保护的资源。

    <login-config> 标签定义 IDP 在验证用户时使用的登录和错误页面。

    web.xml 文件示例:

    <web-app>
      <display-name>IDP</display-name>
      <description>IDP</description>
      <!-- Define a security constraint that gives unlimited access to images -->
      <security-constraint>
        <web-resource-collection>
          <web-resource-name>Images</web-resource-name>
          <url-pattern>/images/*</url-pattern>
        </web-resource-collection>
      </security-constraint>
      <!-- Define a security constraint that requires the All role to access resources -->
      <security-constraint>
        <web-resource-collection>
          <web-resource-name>IDP</web-resource-name>
          <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
          <role-name>All</role-name>
        </auth-constraint>
      </security-constraint>
      <!-- Define the Login Configuration for this Application -->
      <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>IDP Application</realm-name>
        <form-login-config>
          <form-login-page>/jsp/login.jsp</form-login-page>
          <form-error-page>/jsp/error.jsp</form-error-page>
        </form-login-config>
      </login-config>
      <!-- Security roles referenced by this web application -->
      <security-role>
        <description>The role that is required to log in to the IDP Application</description>
        <role-name>All</role-name>
      </security-role>
    </web-app>

    注意

    建议应用中定义欢迎页面。默认情况下,JBoss EAP 将查找名为 index.jsp 的文件,但这可以在 web.xml 中使用 <welcome-file-list> 进行配置。

    login.jsp 文件示例:

    <html>
      <head></head>
      <body>
        <form id="login_form" name="login_form" method="post" action="j_security_check" enctype="application/x-www-form-urlencoded">
          <center> <p>Welcome to the <b>IDP</b></p> <p>Please login to proceed.</p> </center>
          <div style="margin-left: 15px;">
            <p> <label for="username">Username</label> <br /> <input id="username" type="text" name="j_username"/> </p>
            <p> <label for="password">Password</label> <br /> <input id="password" type="password" name="j_password" value=""/> </p>
            <center> <input id="submit" type="submit" name="submit" value="Login"/> </center>
          </div>
        </form>
      </body>
    </html>

    error.jsp 文件示例:

    <html>
      <head></head>
      <body>
        <p>Login failed, please go back and try again.</p>
      </body>
    </html>

  3. 为 IDP 配置身份验证器。

    身份验证器负责用户身份验证,以及发布和验证 SAML v2 安全断言。通过定义要在验证和授权主体时使用的安全域(请参阅第 1 步),验证器的一部分已在 jboss-web.xml 文件中配置。您还必须确保 web.xml 中指定了 <login-config> ,并且已声明必要的依赖项

    jboss-web.xml 文件必须具有以下内容:

    • <security-domain>,用于指定用于身份验证和授权的安全域。

    jboss-web.xml 文件示例

    <jboss-web>
      <security-domain>idp</security-domain>
      <context-root>identity</context-root>
    </jboss-web>

  4. 声明 IDP 所需的依赖关系。

    Web 应用充当 IDP 要求在 jboss-deployment-structure.xml 中定义依赖项,以便能够找到 org.picketlink 类。JBoss EAP 提供所有必需的 org.picketlink 和相关类,应用程序只需要将其声明为使用它们的依赖项。

    使用 jboss-deployment-structure.xml 进行 Declare 依赖项

    <jboss-deployment-structure>
      <deployment>
        <dependencies>
          <module name="org.picketlink" services="import"/>
        </dependencies>
      </deployment>
    </jboss-deployment-structure>

    注意

    在之前的 JBoss EAP 版本中,您已声明过同样的依赖关系,但会声明一个 valve 以安装 SAML 身份验证器。随着 JBoss EAP 7 中 Undertow 的引入,您现在使用 services="import" 来安装 SAML 身份验证器。

  5. 为 IDP 创建 和配置 picketlink.xml 文件。

    该文件必须至少包含以下元素:

    • <PicketLinkIDP> 定义 IDP 的 URL、使用 <IdentityURL> 和 IDP 信任的任何主机。
    • <Handlers> 定义处理 SAML 请求和响应所需的一组处理程序。

    picketlink.xml 文件示例

    <PicketLink xmlns="urn:picketlink:identity-federation:config:2.1">
      <PicketLinkIDP xmlns="urn:picketlink:identity-federation:config:2.1">
        <IdentityURL>${idp.url::http://localhost:8080/identity/}</IdentityURL>
        <Trust>
          <Domains>localhost,example.com</Domains>
        </Trust>
      </PicketLinkIDP>
      <Handlers xmlns="urn:picketlink:identity-federation:handler:config:2.1">
        <Handler class="org.picketlink.identity.federation.web.handlers.saml2.SAML2IssuerTrustHandler" />
        <Handler class="org.picketlink.identity.federation.web.handlers.saml2.SAML2LogOutHandler" />
        <Handler class="org.picketlink.identity.federation.web.handlers.saml2.SAML2AuthenticationHandler" />
        <Handler class="org.picketlink.identity.federation.web.handlers.saml2.RolesGenerationHandler" />
      </Handlers>
    </PicketLink>

    警告

    处理程序通过责任链来实施,每个处理程序按照 selectet link.xml 定义的顺序执行请求和响应的逻辑。务必要注意处理程序的配置顺序。

    默认情况下,optetlink.xml 位于 IDP Web 应用的 WEB-INF 目录中。不过,可以配置应用程序外部的 picket link.xml 的自定义路径。当一个或多个 JBoss EAP 实例的多个应用共享相同的 picket link.xml 配置时,这很有用。

  6. 可选: 为 picket link.xml设置自定义位置

    您可以使用 CONFIG_FILE 参数为 picketlink.xml 指定自定义位置。这可以通过在 web.xml 中添加 <context-param> 元素来完成。

    使用 CONFIG_FILE 参数

    <context-param>
      <param-name>CONFIG_FILE</param-name>
      <param-value>/path/to/picketlink.xml</param-value>
    </context-param>

    您还可以使用 org.picketlink.federation.saml.CONFIG_PROVIDER 参数来指定自定义配置供应商。这可让您创建一个扩展 org.picketlink.identity.federation.web.util.SAMLConfigurationProvider 的自定义实现,以提供自己的配置逻辑。

    使用 org.picketlink.federation.saml.CONFIG_PROVIDER Parameter

    <context-param>
      <param-name>org.picketlink.federation.saml.CONFIG_PROVIDER</param-name>
      <param-value>MyConfigurationProvider</param-value>
    </context-param>

    注意

    显示的管理 CLI 命令假定您在运行 JBoss EAP 单机服务器。有关将管理 CLI 用于 JBoss EAP 受管域的更多详细信息,请参见 JBoss EAP管理 CLI 指南

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat
返回顶部