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");
}
}
这对您有帮助吗?