驗證器範例延伸功能組態
請使用下列步驟來建構和建立延伸功能資訊清單,並以驗證器延伸功能回撥方法來實行您的驗證策略演算法。如需有關在 ThingWorx 中建立自訂延伸功能的其他資訊,請參閱 ThingWorx 延伸功能性
建構
可使用如 Ant 或 Gradle 之類的建構工具。在您的建構指令集中,您需要編譯實行驗證器方法回撥的 Java 類別 (將於稍後討論)。請確定針對與您在執行時間使用的 Thingworx-Platform-x.x.x 版本相容的 Thingworx-Extension-SDK-x.x.x 程式庫 (JAR 檔案) 進行編譯。請記得還要包括 metadata.xml 檔案,在匯入延伸功能 ZIP 時,會用這個檔案來向 ThingWorx 註冊您的驗證器延伸功能。
建立資訊清單
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。若傳回值為 trueThingWorx 執行時間會呼叫驗證方法 (如下所示)。

//… 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
相關連結