Integrate the Java server-side SDK
IClientProfile is the interface for interacting with the Alibaba Cloud Captcha server-side API. All SDK operations require an IClientProfile instance. This topic describes how to integrate the Java server-side SDK.
Prerequisites
An AccessKey is created for your Alibaba Cloud account. For more information, see Create an AccessKey.
The Java server-side SDK package is downloaded from the Alibaba Cloud Captcha console.
The server-side development environment is J2SE Development Kit (JDK) 1.5 or later.
Install the SDK
In the decompressed SDK package, find the aliyun-java-sdk-core-2.2.5.jar and aliyun-java-sdk-afs.jar files.
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 add the two JAR packages.
After the packages are added, 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 the 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
Classes related to the Alibaba Cloud Captcha server-side SDK are in the com.aliyuncs.IAcsClient package.
The default connection timeout for SDK API calls is 3 seconds, and the default read timeout is 80 seconds. To customize the connection timeout and read timeout for SDK API calls, you can call the constructor with the connectTimeout and readTimeout parameters.
Initialize IClientProfile
An IClientProfile instance can be reused. We recommend that you set it as a globally unique instance for the application.
String regionid = "cn-hangzhou";
String accessKeyId = "*** Provide your AccessKeyId ***";
String accessKeySecret = "*** Provide your AccessKeySecret ***";
// Create a new IClientProfile instance
IClientProfile profile= DefaultProfile.getProfile(regionid, accessKeyId, accessKeySecret);
IAcsClient client = new DefaultAcsClient(profile);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "afs", "afs.aliyuncs.com");
Call the Alibaba Cloud Captcha server-side API
After the IClientProfile instance is initialized, you can call the APIs provided by the Alibaba Cloud Captcha service. You also need to develop a handler class for frontend page requests and a method to process the results based on your requirements.
For more information about the Alibaba Cloud Captcha service APIs, see Alibaba Cloud Captcha service APIs for web and HTML5 applications.
public void test(){
AuthenticateSigRequest request = new AuthenticateSigRequest();
request.setSessionId("xxx");// Session ID. Required. Get from the frontend success callback. Do not change.
request.setSig("xxx");// Signature string. Required. Get from the frontend success callback. Do not change.
request.setToken("xxx");// Unique request ID. Required. Get from the frontend success callback. Do not change.
request.setScene("xxx");// Scenario ID. Required. Must be the same as on the frontend page. Do not change.
request.setAppKey("xxx");// Application type ID. Required. Specify on the backend.
request.setRemoteIp("xxx");// Client IP address. Required. Specify on the backend.
try {
// Response code: 100 indicates that the signature verification passed. 900 indicates that the signature verification failed.
AuthenticateSigResponse response = client.getAcsResponse(request);
} catch (Exception e) {
e.printStackTrace();
}
}