You can use a WebView component in your app to integrate with Alibaba Cloud Captcha 2.0 for HTML5 mobile applications. This method uses mature hybrid app development technology to provide human-machine verification for your app services. This topic describes how to integrate with Captcha 2.0 in an Android app.
Background information
Captcha 2.0 for HTML5 mobile applications provides benefits such as rapid iteration and strong compatibility. By integrating the Captcha 2.0 service in this manner, you can avoid compatibility issues caused by dependencies and references in native app components. The rapid iteration of HTML5 Captcha 2.0 also helps your app better respond to sophisticated attacks.
Integration overview
If your service uses the V2 architecture, see Integrate with the V2 architecture on Android.
Integrating Captcha 2.0 for HTML5 applications into an Android app involves the following steps:
HTML5 service client: Integrate Captcha 2.0 into the HTML5 service client and initiate a verification.
Server for the HTML5 service: Integrate the Captcha 2.0 SDK into the server and call the
VerifyIntelligentCaptchaAPI operation to initiate signature verification for your service.Android app client: Use a WebView component to enable and deploy the service page that requires Captcha 2.0 integration in your app.
Prerequisites
The Captcha 2.0 service is activated.
A verification scenario is created where the Integration Method is set to Webview+H5 (for Apps And WeChat Mini Programs).
Android 7.0 or later is used.
Procedure
Step 1: Integrate Alibaba Cloud Captcha 2.0 into the HTML5 service client
In your HTML5 service client code, integrate the client-side integration code provided by Captcha 2.0. For more information, see Integrate with the V3 architecture for web and HTML5 clients.
NoteIf your service uses the V2 architecture, see Integrate with the V2 architecture for web and HTML5 clients.
In the success callback function, add a JavaScript function to return the signature verification parameters to the custom Java interface
testJsInterface, as described in Step 5 and Step 6. After the Java interface obtains the signature verification parameters, the Android app can send service and signature verification requests and process the verification results.// success callback function function success(captchaVerifyParam) { // The input parameter is captchaVerifyParam for signature verification. // Return the signature verification parameter to the custom Java interface. window.testInterface && window.testInterface.getCaptchaVerifyParam(captchaVerifyParam); }
Returned data
In the V3 client architecture for Captcha 2.0, the Captcha server performs behavioral verification by validating the user's response and determining if the request is from a machine. The server then returns the Captcha code and other parameters to the client. You can view the returned data using the Network feature of your browser. For more information, see Data returned by the V3 client architecture.

Step 2: Integrate Alibaba Cloud Captcha 2.0 into the server for the HTML5 page
On the server for the HTML5 service page, integrate the server-side software development kit (SDK) provided by Captcha 2.0. Then, call the VerifyIntelligentCaptcha API operation to initiate signature verification for your service. For more information, see Server-side integration.
Step 3: Enable and deploy the service page on the Android app client
In the
Activity.javafile of your Android app project, import the dependency libraries for the WebView component.import android.webkit.WebView; import android.webkit.WebSettings; import android.webkit.WebViewClient; import android.webkit.WebChromeClient; // Or // import android.webkit.*;In the
AndroidManifest.xmlconfiguration file, set the permissions for loading web pages.NoteIf other HTTP resources are called, you must also add the corresponding configurations.
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <application ... android:usesCleartextTraffic="true" ... >In the
activity_main.xmllayout file, add the WebView component.<WebView android:id="@+id/webview" android:layout_height="match_parent" android:layout_width="match_parent" />In the
MainActivity.javafile, load the HTML5 service page.public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { // Page layout WebView webview = findViewById(R.id.webview); // WebView settings WebSettings webSettings = webview.getSettings(); // Optional. Enable Chrome debugging. WebView.setWebContentsDebuggingEnabled(true); // Make the screen responsive. webSettings.setUseWideViewPort(true); webSettings.setLoadWithOverviewMode(true); // Disable caching to ensure that the latest version of Captcha 2.0 can be quickly retrieved to defend against attacks. webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); // Allow the WebView component to load JavaScript. webSettings.setJavaScriptEnabled(true); webSettings.setDomStorageEnabled(true); // Use the WebView component to load the page instead of the default browser. webview.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); // Load the service page. webview.loadUrl("http://x.x.x.x/demo/"); } }In the
MainActivity.javafile, add the custom Java interfacetestJsInterfaceand define thegetCaptchaVerifyParammethod to obtain the Captcha signature verification parameters.import android.webkit.JavascriptInterface; public class testJsInterface { @JavascriptInterface public void getCaptchaVerifyParam(String captchaVerifyParam) { // After you obtain the signature verification parameters, you can send service and signature verification requests and process the verification results. System.out.println(captchaVerifyParam); } }In the
initView()method ofMainActivity.java, bind the custom Java interface to the JavaScript function.// Build a bridge for JavaScript to call the Java interface. webview.addJavascriptInterface(new testJsInterface(), "testInterface");
After you complete these configurations, your Android app is integrated with Captcha 2.0. You can use Captcha in your Android app to test the verification. If Captcha works as expected, the integration is successful.