에지 장치 인증자 확장 예
아래 예에서는 에지에서 제출된 보안 요구를 사용하도록 에지 장치 인증자 확장을 개발할 수 있는 방법을 설명합니다.
이 예에는 다음 내용이 있습니다.
ThingWorx Platform API에서 SecurityClaimsAuthenticator 클래스를 확장하여 matchesAuthRequestauthenticate 메소드 내에서 사용자 정의 처리 로직을 구현합니다.
matchesAuthRequest 메소드 내:
수신 보안 요구를 확인하여 해당 요구에 secretTokenKey 요구가 포함되어 있는지 확인합니다.
요구에 secretTokenKey 요구가 포함된 경우 메소드는 플랫폼에 참을 반환합니다. 이는 이 인증자를 보안 요구의 유효성을 검사하는 데 사용해야 함을 나타냅니다.
authenticate 메소드 내:
secretTokenKey를 가져옵니다.
secretTokenKey가 null이 아니고 비어 있지 않는 경우 및 해당 값이 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");
}
}
도움이 되셨나요?