主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

身份验证

注意

独立的 MATLAB® Web App Server™ 产品支持身份验证,而 MATLAB Compiler™ 中包含的开发版本则不支持。有关详细信息,请参阅MATLAB Web App Server 差异

前提条件

  • 在服务器上启用 SSL。有关详细信息,请参阅启用 SSL

创建 webapps_authn.json 文件

通过身份验证,您可以验证用户的凭据,并帮助您控制哪些用户可以访问服务器上部署的 Web App。

MATLAB Web App Server 支持使用轻量级目录访问协议 (LDAP) 和 OpenID Connect (OIDC) 进行身份验证。

要启用身份验证,请执行以下操作:

  1. 检查 SSL 是否启用。有关详细信息,请参阅启用 SSL

  2. 创建一个名为 webapps_authn.json 的文件并将其放在 config 文件夹内的 webapps_private 文件夹中。webapps_authn.json 的格式取决于您是使用 LDAP 还是 OIDC 进行身份验证。

    webapps_private 文件夹位于以下位置:

    操作系统文件夹位置

    Windows®

    %ProgramData%\MathWorks\webapps\R2025a\config\webapps_private

    Linux®

    /local/MathWorks/webapps/R2025a/config/webapps_private

    macOS

    /Library/Application Support/MathWorks/webapps/R2025a/config/webapps_private

LDAP 身份验证

LDAP 目录服务器存储有关用户、组和应用程序的信息。目录中的每个条目由三部分组成:一个专有名称 (DN)、一个属性集合和一个对象类集合。

要使用 LDAP 身份验证,请使用以下 JSON 架构创建一个名为 webapps_authn.json 的文件并将其放在 webapps_private 文件夹中。

{
    "version": "1.2.0",
    "type": "ldap",
    "authnConfig": {
        "host": "<LDAP server host name>",
        "port": "<LDAP server port number>",
        "searcherDN": "",
        "searcherPassword": "",
        "baseDN": "<Point in LDAP from where to start search for a user>",
        "userFilter": "<Filter syntax>"
    },
    "appConfig": {
        "checkSSLCA": "<Boolean indicating whether to check for trusted SSL certificate>",
        "trustedSSLCA": "<Path to trusted SSL certificate>",
        "displayName": "<Identifier to display on MATLAB Web App Server home page>",
        "tokenExpirationMin": "<Token expiration duration in minutes>",
        "userAttributeName": "<Attribute name to identify user objects>",
        "groupAttributeName": "<Attribute name to identify group objects>"
        "useSSL": "<Optional: Use SSL for connection. Default: true on Windows, false on Linux/macOS>",
        "useStartTLS": "<Optional: Use STARTTLS for connection. Default: true on Linux/macOS, false on Windows>"
    }
}

  • version:指定 JSON 架构的版本。R2025a 的默认值为:1.2.0

  • type:指定要使用的身份验证类型。将此值设置为 ldap

  • host:指定 LDAP 目录服务器主机名。例如:myldap.example.com

  • port(可选)指定 LDAP 目录服务器端口号。例如:389。如果未指定端口号,则将使用默认端口。MATLAB Web App Server 使用 SSL/STARTTLS 来确保与 LDAP 服务器的安全通信。这可确保用户名和密码在 MATLAB Web App Server 和 LDAP 服务器之间通过加密通道传输。默认情况下,服务器在 Windows 上使用标准端口 636 进行 SSL 连接,在 Linux 和 macOS 上使用端口 389 进行 STARTTLS 连接。您必须配置 LDAP 服务器以允许通过指定(或默认)LDAP 端口进行 SSL/STARTTLS 连接;否则身份验证将失败。

  • searcherDN:指定目录中搜索者帐户的 DN。默认值为 ""。搜索者 DN 指的是允许搜索 LDAP 目录服务器的帐户。例如:"cn=admin,dc=example,dc=com"

  • searcherPassword:搜索者帐户的密码。默认值为 ""

    MATLAB Web App Server 使用 searcherDNsearcherPassword 的值,通过 userFilter 搜索用户的 DN。随后通过 LDAP 使用输入的密码来验证发现的 DN。如果 LDAP 服务器提供匿名身份验证访问权限,则不需要 searcherDNsearcherPassword 的值。

    由于 webapps_authn.json 文件位于 webapps_private 文件夹中,该文件夹只有服务器帐户可读,因此搜索者的凭据受到保护,App 或其他登录服务器的用户无法获取。

  • baseDN:指定目录中的基本 DN。基本 DN 是目录中应用程序开始搜索用户的位置。例如:dc=myldap,dc=example,dc=com

  • useFilter:指定过滤器来查找用户的 DN。MATLAB Web App Server 使用 userFilter 来查找与输入的用户名匹配的用户 DN,在过滤器中表示为 {username}。如果未找到匹配项或找到多个匹配项,则身份验证失败。过滤器可以使用标准 LDAP 过滤器语法指定。例如:(&(objectClass=User)(sAMAccountName={username}))

  • checkSSLCA:检查 LDAP 服务器的 SSL 证书是否由公认的证书颁发机构 (CA) 签名。将此属性设置为 "true" 可检查是否存在有效的 SSL 证书,将其设置为 "false" 则会跳过检查。如果设置为 "true",则需要指定 trustedSSLCA 的值。如果设置为 "false",用户名和密码仍通过加密通道在 MATLAB Web App Server 和 LDAP 服务器之间传输。然而,为了增加安全性,建议进行此项检查。

  • trustedSSLCA:在 Linux 和 macOS 系统上,指定签署站点证书的证书颁发机构 (CA) 颁发的根证书的路径。在 Windows 系统上,您不需要指定路径。只要根证书位于受信任的根证书颁发机构证书存储区中,MATLAB Web App Server 就会自动找到它。

  • displayName:通过指定用户 LDAP 条目的属性来配置如何在 MATLAB Web App Server 主页上显示用户身份。例如,将此属性设置为 uid 将显示用户 ID。默认值是在身份验证过程中输入的用户名。

  • tokenExpirationMin:指定令牌的有效期(以分钟为单位)。例如:60。默认值为 "",这表示令牌不会过期。

  • userAttributeName:指定属性名称来识别用户对象。例如:uid。如果您使用基于策略的访问,则必须为此属性设置一个值。否则,请不要将此属性包含在您的 JSON 文件中。有关详细信息,请参阅基于策略的访问

  • groupAttributeName:指定属性名称来识别组对象。例如:member。如果您使用基于策略的访问,则必须为此属性设置一个值。否则,请不要将此属性包含在您的 JSON 文件中。有关详细信息,请参阅基于策略的访问

  • useSSL(可选) 通过将值设置为 "true""false" 来指定 LDAP 服务器是否要求使用 SSL 进行连接。在 Windows 系统上,默认值为 "true",在 Linux 和 macOS 系统上,默认值为 "false" (自 R2025a 起)

  • useStartTLS(可选) 通过将值设置为 "true""false" 来指定 LDAP 服务器是否要求使用 STARTTLS 进行连接。在 Linux 和 macOS 系统上,默认值为 "true";在 Windows 系统上,默认值为 "false" (自 R2025a 起)

用于 LDAP 的 webapps_authn.json 示例文件

{
    "version": "1.2.0",
    "type": "ldap",
    "authnConfig": {
        "host": "myldap.example.com",
        "port": "",
        "searcherDN": "",
        "searcherPassword": "",
        "baseDN": "DC=myldap,DC=example,DC=com",
        "userFilter": "(&(objectClass=User)(sAMAccountName={username}))"
    },
    "appConfig": {
        "checkSSLCA": "false",
        "trustedSSLCA": "",
        "displayName": "uid",
        "tokenExpirationMin": "60",
        "useSSL": false,
        "useStartTLS": true
    }
}

使用基于策略的访问时用于 LDAP 的 webapps_authn.json 示例文件

{
    "version": "1.2.0",
    "type": "ldap",
    "authnConfig": {
        "host": "myldap.example.com",
        "port": "",
        "searcherDN": "",
        "searcherPassword": "",
        "baseDN": "DC=myldap,DC=example,DC=com",
        "userFilter": "(&(objectClass=User)(sAMAccountName={username}))"
    },
    "appConfig": {
        "checkSSLCA": "false",
        "trustedSSLCA": "",
        "displayName": "uid",
        "tokenExpirationMin": "60",
        "userAttributeName": "uid",
        "groupAttributeName": "memberOf"
    }
}

OIDC 身份验证

OpenID Connect (OIDC) 允许 MATLAB Web App Server 根据第三方身份提供方 (IdP) 执行的身份验证来验证最终用户的身份。要在服务器上使用 OIDC 身份验证,您需要使用 IdP 注册,例如 Microsoft® Azure® AD 或 Google® Identity Platform。

要使用 OIDC 身份验证,请使用以下 JSON 架构创建一个名为 webapps_authn.json 的文件并将其放在 webapps_private 文件夹中。

{
    "version": "1.3.0",
    "type": "oidc",
    "authnConfig": {
        "issuer": "<OIDC IdP issuer URI>",
        "clientId": "<Client ID from IdP>",
        "clientSecret": "<Client secret from IdP>",
        "redirectUrl": "<Redirection URL>",
        "scope": ["<scope1> <scope2>"]
    },
    "appConfig": {
        "port": "<OIDC authentication port number used by MATLAB Web App Server>",
        "displayName": "<Identity to display on MATLAB Web App Server home page>",
        "tokenExpirationMin": "<Token expiration duration in minutes>",
        "userAttributeName": "<Attribute name to identify user objects>",
        "groupAttributeName": "<Attribute name to identify group objects>",
        "prompt": <Boolean indicating whether to re-authenticate user>,
        "timeoutInSeconds": "<Default HTTP request timeout in seconds for OIDC authentication>", 
        "checkSSLCA": "<Boolean indicating whether to validate SSL certificate against a recognized CA>",
        "trustedSSLCA": "<Path to the root certificate on Linux/macOS>" 
    }
}
  • version:指定 JSON 架构的版本。R2025a 的默认值为:1.3.0 (自 R2023a 起)

  • type:指定要使用的身份验证类型。将此值设置为 oidc

  • issuer:指定 OIDC IdP 颁发者 URI。例如,如果使用的是 Google Identity Platform,则颁发者为:https://accounts.google.com/.well-known/openid-configuration

  • clientId:指定您使用 IdP 注册凭据时获取的客户端 ID。例如,如果使用的是 Google Identity Platform,则客户端 ID 为:1234567890-xxxxxxxxxxxx.apps.googleusercontent.com

  • clientSecret:指定您使用 IdP 注册凭据时获取的客户端机密。例如,如果使用的是 Google Identity Platform,则客户端机密为:_xxxxxxxxxxxxx_Xxxxxx_xX

    由于 webapps_authn.json 文件位于 webapps_private 文件夹中,该文件夹只有服务器帐户可读,因此 clientIdclientSecret 受到保护,App 或其他登录服务器的用户无法获取。

  • redirectUrl(可选)指定您在使用 IdP 配置 OIDC 身份验证时使用的 redirect URL。如果留空,则使用运行 MATLAB Web App Server 的计算机的主机名和端口号作为回调。URL 的格式为:https://<MATLABWebAppServer_hostname>:<port_server_is_running_on>/webapps/extauth/callback。例如,如果 MATLAB Web App Server 在端口 9988 上运行,则 redirect URL 为:https://example.com:9988/webapps/extauth/callback

    在 R2021a 之前,redirect URL 的格式为:https://<MATLABWebAppServer_hostname>:<port>/oidc/callback。例如:https://example.com:3000/oidc/callback

  • scope:指定管理员希望 MATLAB Web App Server 访问的资源的标识符。例如,如果使用的是 Google Identity Platform,则标识符为:openid profile email

  • port(可选)指定 MATLAB Web App Server 进程内部用于 OIDC 身份验证的端口号。例如:4000。该端口号必须与 redirectUrl 端口号不同。如果未指定端口号,MATLAB Web App Server 进程会自动选择一个端口进行 OIDC 身份验证。

  • displayName:通过指定经过身份验证的用户对象的属性名称,配置如何在 MATLAB Web App Server 主页上显示用户的身份。例如,如果使用的是 Google Identity Platform,given_name 将显示用户的名称。默认值为 sub 属性。

  • tokenExpirationMin:指定令牌的有效期(以分钟为单位)。例如:60。默认值为 "",这表示令牌不会过期。

  • userAttributeName:指定属性名称来识别用户对象。例如:uid。如果您使用基于策略的访问,则必须为此属性设置一个值。否则,请不要将此属性包含在您的 JSON 文件中。如果您不使用基于策略的访问但决定将此属性包含在 JSON 文件中,请将值设置为 ""。有关详细信息,请参阅基于策略的访问

  • groupAttributeName:指定属性名称来识别组对象。例如:member。如果您使用基于策略的访问,则必须为此属性设置一个值。否则,请不要将此属性包含在您的 JSON 文件中。如果您不使用基于策略的访问但决定将此属性包含在 JSON 文件中,请将值设置为 ""。有关详细信息,请参阅基于策略的访问

  • prompt:指定是否要对已登录到身份提供方 (IdP) 的用户重新进行身份验证。 (自 R2023a 起)

    在 R2024b 之前:

    • 如果将 "prompt" 设置为 "true",用户即使登录到 IdP,也必须重新进行身份验证。

    • 如果将 "prompt" 设置为 "false",并且用户登录到 IdP,则可以避免出现 IdP 登录屏幕。如果用户未登录,则必须通过登录重新进行身份验证。

    • 如果没有指定 "prompt" 字段和值,则 "prompt" 字段将被视为 "true"。这是默认设置。

    警告

    无论此参数是否存在或值为多少,服务器的行为将同 prompt 设置为 false 时一样。因此,系统不会提示已登录到 IdP 的用户重新进行身份验证。 (自 R2024b 起)

  • timeoutInSeconds(可选)通过设置以秒为单位的值,指定 MATLAB Web App Server 为所有 OIDC 身份验证请求设置的默认 HTTP 请求超时。默认值为 5 秒。 (自 R2025a 起)

  • checkSSLCA:检查 OIDC 服务器的 SSL 证书是否由公认的证书颁发机构 (CA) 签发。将此属性设置为 "true" 可检查是否存在有效的 SSL 证书,将其设置为 "false" 则会跳过检查。如果设置为 "true",则需要指定 trustedSSLCA 的值。如果设置为 "false",用户名和密码仍将通过加密通道在 MATLAB Web App Server 和 OIDC 服务器之间传输。然而,为了增加安全性,建议进行此项检查。 (自 R2025a 起)

  • trustedSSLCA:在 Linux 和 macOS 系统上,指定签署站点证书的证书颁发机构 (CA) 颁发的根证书的路径。在 Windows 系统上,您不需要指定路径。只要根证书位于受信任的根证书颁发机构证书存储区中,MATLAB Web App Server 就会自动找到它。 (自 R2025a 起)

注意

  1. 如果您使用 OIDC 身份验证,则需要使用 IdP 注册 MATLAB Web App Server 作为应用程序

  2. 在注册过程中,您需要提供 MATLAB Web App Server 的 redirect URL。URL 的格式为:https://<MATLABWebAppServer_hostname>:<port_server_is_running_on>/webapps/extauth/callback。例如:https://example.com:9988/webapps/extauth/callback

使用 Google Identity Platform 时用于 OIDC 的 webapps_authn.json 示例文件

{
    "version": "1.2.0",
    "type": "oidc",
    "authnConfig": {
        "issuer": "https://accounts.google.com/.well-known/openid-configuration",
        "clientId": "1234567890-xxxxxxxxxxxx.apps.googleusercontent.com",
        "clientSecret": "_xxxxxxxxxxxxx_Xxxxxx_xX",
        "redirectUrl": "https://example.com:9988/webapps/extauth/callback",
        "scope": ["openid profile email"]
    },
    "appConfig": {
        "port": "4000",
        "displayName": "given_name",
        "tokenExpirationMin": "60"
    }
}

提示

设置身份验证后,如果您无法从浏览器登录,请尝试清除浏览器的缓存和 cookie,或尝试使用其他浏览器。

小心

请严格执行 webapps_authn.json 的 JSON 架构语法。如果架构语法中存在错误,则可能会导致服务器无法启动,或者您在尝试登录时访问服务器遭拒。

另请参阅

主题

外部网站