身份验证器示例扩展配置
遵循以下步骤构建、创建扩展内容清单,并采用身份验证器扩展回调方法实现身份验证策略算法。有关在 ThingWorx 中创建自定义扩展的其他信息,请参阅 ThingWorx 扩展性
构建
可以使用 Ant 或 Gradle 等构建工具。您需要在构建脚本中编译您的 Java 类以实现身份验证器方法回调 (将在稍后讨论)。确保您要编译的 Thingworx-Extension-SDK-x.x.x 库 (JAR 文件) 与运行时使用的 Thingworx-Platform-x.x.x 版本相兼容。请记住,在导入扩展 ZIP 时,还要包括用于在 ThingWorx 中注册身份验证器扩展的 metadata.xml 文件。
创建内容清单
1. 打开 configfiles/metadata.xml
2. 在身份验证器集合元素 (如下所示) 中,根据需要配置以下参数:
<Entities>
<ExtensionPackages>
<ExtensionPackage name="Authenticators_Sample_BasicUser_ExtensionPackage"
description="Authenticators Sample BasicUser Extension Package"
vendor="ThingWorx"
packageVersion="1.0"
minimumThingWorxVersion="5.1.1">
<JarResources>
<FileResource type="JAR" file="auth-sample-basicuser-extension.jar" description="Authenticators Sample BasicUser JAR file." />
</JarResources>
</ExtensionPackage>
</ExtensionPackages>
<Authenticators>
<Authenticator name="SampleBasicUserAuthenticator"
className="com.thingworx.security.authentication.samples.SampleBasicUserAuthenticator"
description="Sample Authenticator that validates against the Thingworx User"
aspect.isEditableExtensionObject="true">
</Authenticator>
</Authenticators>
</Entities>
a. Authenticator name - 在 Composer 中显示。必须是唯一名称。
b. className - ThingWorx 用于实例化身份验证器实现实例的实现类名称。
c. description - 在 Composer 中显示。
d. aspect.isEditableExtensionObject - 设置为 true 以允许启用,优先级设置将保存在 Composer 中。此参数是必需的。
3. 配置 FileResource 类型的 jar 文件名 (见下文):
<FileResource type="JAR" file="auth-sample-basicuser-extension.jar"
description="Authenticators Sample BasicUser JAR file." /> name of jar file
4. 保存并关闭该文件。
实现
要实现,请遵循以下步骤获得适当的身份验证器 JAR 文件:
1. CustomAuthenticator 类衍生/扩展。
* 
SupportsSession 参数允许请求之间的会话支持,且默认情况下设置为 true。如果不想支持请求之间的会话,则将其设置为 false
2. 实现默认构造器。
3. 根据您实现的需要,覆盖身份验证器类型方法:
方法
说明
public boolean matchesAuthRequest(HttpServletRequest httpRequest)
throws AuthenticatorException,
允许用户从作为 httpRequest 参数提供的入站身份验证请求获取信息。如需指定:用户可以处理请求,请返回 true,否则返回 false。如果返回值为 true,则 ThingWorx 运行时将调用身份验证方法 (见下文)。

//… make SSO call or other algorithm to determine userName
public void authenticate(HttpServletRequest httpRequest, HttpServletResponse httpResponse)
{
// use validateThingworxUser to make sure the user is enabled and a Thingworx user
AuthenticationUtilities.validateThingworxUser(userName);
// Call setCredentials, so the Thingworx platform can provide the correct permissions and access to allowed content
this.setCredentials(userName);
}

// … or if not using SSO, but providing username and password
public void authenticate(HttpServletRequest httpRequest, HttpServletResponse httpResponse)
{
AuthenticationUtilities.validateCredentials(user, password);
this.setCredentials(user, password);
}
对于仅提供用户名的身份验证策略 (例如,在单独的应用程序中完成用户身份验证的 SSO),请使用左侧列所示的第一组 "validate" 和 "setCredentials"。
对于提供用户名和密码的身份验证策略,请使用左侧列所示的第二组 "validate" 和 "setCredentials"。
public void issueAuthenticationChallenge(HttpServletRequest httpRequest, HttpServletResponse httpResponse)
引发 AuthenticatorException
此方法不是始终必需的。但是,如果从身份验证方法引发异常,或者将 RequiresChallenge 标志设置为 true,则将调用 issueAuthenticationchallenge 方法。
默认身份验证器优先级
系统身份验证器具有以下默认优先级。如果需要,此分布式范围将允许身份验证器扩展插入到处理链之间。这些系统对象身份验证器不能以任何方式重新排序、重新划分优先级、删除或更改。
ThingworxBasicAuthenticator - 优先级 100。对提供的用户名和密码进行身份验证。
ThingworxMobileTokenAuthenticator - 优先级 150。对 ThingWorx 移动令牌进行验证的 Mobile App Builder 身份验证器。
* 
此身份验证器是为未来版本而设计的,目前已被禁用。
ThingworxApplicationKeyAuthenticator - 优先级 200。对提供的应用程序密钥进行身份验证。
ThingworxFormAuthenticator - 优先级 300。通过表单登录进行身份验证。
ThingworxMobileAuthorizationAuthenticator - 优先级 350。用于对 ThingWorx 用户/密码进行验证的 Mobile App Builder 身份验证器。
* 
此身份验证器是为未来版本而设计的,目前已被禁用。
ThingworxHttpBasicAuthenticator - 优先级 400。基本 HTTP 身份验证。
在 Composer 中部署自定义身份验证器
1. 在 Composer 中,单击“导入/导出”>“导入”
2. 查找 <YourAuthenticatorExtensionZipName>.zip
3. 安装扩展。
4. 在资源管理器中,单击“身份验证器”
5. 打开适当的身份验证器。
6. “常规信息”中,单击“已启用”
* 
默认情况下,所有身份验证器均处于禁用状态。
7. 单击“保存”
运行时身份验证器
可以在运行时访问身份验证器扩展。通常情况下,首先处理最低值。如果有两个具有相同优先级值的身份验证器,并且都对 matchAuthenticationRequest 回调响应 true,那么非确定性的顺序效应将在运行时引发非确定性的身份验证器请求处理。为此,我们建议不要存在两个具有相同优先级值的自定义身份验证器,以保证已知的请求处理顺序。如果身份验证器未能处理请求,则链中的下一个身份验证器将有机会处理该请求等,直到发生有效的身份验证。如果没有自定义身份验证器可以处理该请求,则 ThingWorx 系统身份验证器会处理该请求,且可能显示需要用户名和密码的表单登录。
在运行时访问身份验证器
要在运行时访问身份验证器,请向以下 URL 提出请求,并提供查询、标题参数或消息正文,以便在前面讨论的三种自定义身份验证器回调方法期间,自定义身份验证器可以从转发的 httpRequest 对象中检索和处理这些内容。
Server:Port/Thingworx/Home
<host:port>/Thingworx/Home?YourParameter=something
相关链接