Java server-side SDK integration

更新时间:
复制 MD 格式

IClientProfile is the interface for interacting with the Alibaba Cloud Captcha server-side API. All software development kit (SDK) operations are performed through this interface.

Prerequisites

  • An AccessKey has been created for your Alibaba Cloud account.

  • You have downloaded the Java server-side SDK package from the Alibaba Cloud Captcha console.

  • Your server-side development environment uses J2SE Development Kit (JDK) 1.5 or later.

Install the SDK

  1. Find the aliyun-java-sdk-core-2.2.5.jar and aliyun-java-sdk-afs.jar files in the decompressed SDK package.

  2. Import these two JAR packages into your server-side project.

    For example, in Eclipse, right-click the project, select Properties > Java Build Path > Libraries > Add External JARs, and then add the two JAR packages.

  3. After you add the packages, you can use the Alibaba Cloud Captcha Java SDK in your server-side project.

  4. To import the SDK 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

  • The 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 seconds, and the read timeout is 80 seconds. To customize these timeouts, call the constructor that includes the connectTimeout and readTimeout parameters.

Initialize IClientProfile

Note

You can reuse IClientProfile. Set it as a globally unique object for your 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 you initialize IClientProfile, 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.

For more information, see Alibaba Cloud Captcha service APIs for web and HTML5 applications.

public void test(){        
      AuthenticateSigRequest request = new AuthenticateSigRequest();
      request.setSessionId("xxx");// The session ID. This is a required parameter. Obtain it from the frontend success callback. Do not change it.
      request.setSig("xxx");// The signature string. This is a required parameter. Obtain it from the frontend success callback. Do not change it.
      request.setToken("xxx");// The unique request ID. This is a required parameter. Obtain it from the frontend success callback. Do not change it.
      request.setScene("xxx");// The scene ID. This is a required parameter. It must be the same as the data on the frontend page. Do not change it.
      request.setAppKey("xxx");// The application type ID. This is a required parameter. Fill it in on the backend.
      request.setRemoteIp("xxx");// The client IP address. This is a required parameter. Fill it in on the backend.
      try {
      // The enumeration for the response code: 100 indicates signature verification passed, and 900 indicates signature verification failed.
          AuthenticateSigResponse response = client.getAcsResponse(request);
                  if(response.getCode() == 100) {
                  System.out.println("Signature verification passed");
          } else {
                  System.out.println("Signature verification failed");
      // TODO
      } catch (Exception e) {
          e.printStackTrace();            
      }        
  }