验证用户凭证

@OverrideprotectedbooleanvalidateCredentials(Map<String,Object>credentials){if(credentials!=null&&credentials.containsKey("pin")){StringpinCode=credentials.get("pin").toString();if(pinCode.equals("1234")){returntrue;}else{errorMsg="The pin code is not valid.";}}else{errorMsg="The pin code was not provided.";}//In any other case, credentials are not validreturnfalse;}

publicPinCodeConfig(Propertiesproperties){//Make sure to load the parent propertiessuper(properties);//Load the pinCode propertypinCode=getStringProperty("pinCode",properties,"1234");//Check that the PIN code is at least 4 characters long. Triggers an error.if(pinCode.length()<4){addMessage(errors,"pinCode","pinCode needs to be at least 4 characters");}//Check that the PIN code is numeric. Triggers warning.try{inti=Integer.parseInt(pinCode);}catch(NumberFormatExceptionnfe){addMessage(warnings,"pinCode","PIN code contains non-numeric characters");}}

@OverrideprotectedbooleanvalidateCredentials(Map<String,Object>credentials){if(credentials!=null&&credentials.containsKey(PINCODE_FIELD)){StringpinCode=credentials.get(PINCODE_FIELD).toString();if(pinCode.equals(getConfiguration().pinCode)){returntrue;}else{errorMsg="Pin code is not valid. Hint: "+getConfiguration().pinCode;}}else{errorMsg="The pin code was not provided.";}//In any other case, credentials are not validreturnfalse;}

配置安全性检查

在 adapter.xml 中，添加 <securityCheckDefinition> 元素：

<securityCheckDefinitionname="PinCodeAttempts"class="com.sample.PinCodeAttempts"><propertyname="pinCode"defaultValue="1234"description="The valid PIN code"/><propertyname="maxAttempts"defaultValue="3"description="How many attempts are allowed"/><propertyname="blockedStateExpirationSec"defaultValue="60"description="How long before the client can try again (seconds)"/><propertyname="successStateExpirationSec"defaultValue="60"description="How long is a successful state valid for (seconds)"/></securityCheckDefinition>