Edge 裝置驗證器延伸功能範例
以下範例說明了如何開發 Edge 裝置驗證器延伸功能來使用從 Edge 提交的安全性宣告。
此範例會執行下列操作:
延伸來自 ThingWorx Platform API 的 SecurityClaimsAuthenticator 類別來實行 matchesAuthRequestauthenticate 方法內的自訂處理邏輯。
matchesAuthRequest 方法內:
系統會檢查傳入安全性宣告來查看它們是否包含 secretTokenKey 宣告。
如果宣告的確包含 secretTokenKey 宣告,此方法會將 true 傳回平台,指出應使用此驗證器來驗證安全性宣告。
authenticate 方法內:
取得 secretTokenKey
secretTokenKey 不是空值且非空白,且若其值符合 MySecretKey 的預期值,此方法會傳回 whoTheySaidTheyWere (使用者) 的值。
如果提供的使用者存在,它們會在 ThingWorx Platform 上接受驗證。
package your.company.thingworx.authenticator;

import com.thingworx.communications.common.SecurityClaims;
import com.thingworx.security.authentication.AuthenticatorException;
import com.thingworx.security.authentication.SecurityClaimsAuthenticator;

/**
* Custom edge security authenticators are based around the "SecurityClaims"
* class. This class is at its essence a wrapper around a map. To use the
* map, one provides
*/
public class CustomEdgeAuthenticator extends SecurityClaimsAuthenticator {

private static final long serialVersionUID = 1L;

/**
* The matchesAuthRequest method is the indicator that this
* authenticator should be used to verify the SecurityClaims object
* presented. This method allows the author to make a determination
* about the claims provided by inspecting the information therein and
* decide if this authenticator
* should be used to try to verify the claims provided or not.
*
* @param SecurityClaims object containing the corresponding security
* information @return boolean indicating the authenticator should be
* used to verify the SecurityClaims provided
*/
@Override
public boolean matchesAuthRequest(SecurityClaims securityClaims) throws
AuthenticatorException {
//in this simple implementation we are looking for a 'secretTokenKey'
//claim. If one is present in the securityClaims object supplied -
//this SecurityClaimsAuthenticator should be used
return securityClaims.getClaims().containsKey("secretTokenKey");
}

/**
* This is the method that must be implemented to ensure the presented
* SecurityClaims are valid.
*
* @param SecurityClaims object containing the corresponding security
* information @return String indicating the corresponding user identity
* which the SecurityClaims represent.
*/
@Override
public String authenticate(SecurityClaims securityClaims) throws
AuthenticatorException {
String secretTokenValue = securityClaims.getClaims().get
("secretTokenKey");

if(secretTokenValue != null && secretTokenValue.trim().length() > 0)
{
//the secretTokenValue was provided - verify it is the proper
value!
if("MySecretKey".equals(secretTokenValue)) {
//Only when all these checks are complete will we consider
// the auth request to be valid. Here we have permitted the
// user to tell the platform "who" they are via the
// SecurityClaims
//This is a naieve example - and is for illustrative
// purposes only.
return securityClaims.getClaims().get("whoTheySaidTheyWere");
}
}

//throw a generic exception - and do not leak important information
//to the caller
throw new AuthenticatorException("The claims provided are incorrect");
}
}
這是否有幫助?