Integrate the Java server-side SDK
This topic describes how to integrate the Java server-side SDK.
Prerequisites
Create an AccessKey for your Alibaba Cloud account. For more information, see Create an AccessKey.
You have downloaded the Java server-side SDK package from the Alibaba Cloud Captcha console.
Your server-side development environment is J2SE Development Kit (JDK) 1.5 or later.
Install the SDK
Find the aliyun-java-sdk-core-2.2.5.jar and aliyun-java-sdk-afs.jar files in the extracted SDK package.
Import these two JAR packages into your server-side project.
For example, in Eclipse, right-click Project, select Properties > Java Build Path > Libraries > Add External JARs, and then add the two JAR packages.
After you add the packages, you can use the Alibaba Cloud Captcha Java SDK in your server-side project.
To import the packages using Maven, add the following dependencies to your pom.xml file.
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-afs</artifactId>
<version>1.0.1</version>
</dependency>Notes
All classes for the Alibaba Cloud Captcha server-side SDK are in the com.aliyuncs.IAcsClient package.
The default connection timeout for SDK API calls is 3 s, and the read timeout is 80 s. To customize these timeout periods, you can call the constructor that includes the `connectTimeout` and `readTimeout` parameters.
Initialize IClientProfile
IClientProfile is the interface used to interact with the Alibaba Cloud Captcha server. All SDK operations are performed using this interface.
IClientProfile can be reused. We recommend that you set it as a globally unique object for your application.
// Make sure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are set in your runtime environment.
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
IAcsClient client = new DefaultAcsClient(profile);
DefaultProfile.addEndpoint("cn-hangzhou", "afs", "afs.aliyuncs.com");Call the Alibaba Cloud Captcha server-side API
After you initialize IClientProfile, call the AnalyzeNvcRequest API. You also need to develop a handler class for frontend page requests and a method for processing the results.
public void test(){
AnalyzeNvcRequest request = new AnalyzeNvcRequest();
request.setData("xxx");// Required. The value is obtained from the frontend by calling the getNVCVal method.
// Use the setScoreJsonStr method to declare the mapping between the results returned by the Captcha server and the actions to be performed on the frontend. This also notifies the Captcha server to grant authorization for secondary authentication.
// Note: The frontend page must strictly follow this mapping. Otherwise, the call will fail.
// For example, if you declare "400":"NC" in the setScoreJsonStr method, your frontend must trigger slide verification (NC) when the server returns a 400 code. Triggering any other type of verification will result in failure.
request.setScoreJsonStr("{\"200\":\"PASS\",\"400\":\"NC\",\"800\":\"BLOCK\"}");// Set the client-side actions corresponding to each result code as needed.
try {
AnalyzeNvcResponse response = client.getAcsResponse(request);
if(response.getBizCode() == 100) {
System.out.println("Signature verification passed");
} else if (response.getBizCode() == 200) {
System.out.println("Request passed directly");
} else if (response.getBizCode() == 400) {
System.out.println("Trigger NC on the frontend");
} else if (response.getBizCode() == 800) {
System.out.println("Request blocked directly");
} else if (response.getBizCode() == 900) {
System.out.println("Signature verification failed");
}
// TODO
} catch (Exception e) {
e.printStackTrace();
}
}