

**引入全新的主机体验 AWS WAF**

现在，您可以使用更新的体验访问控制台中任意位置的 AWS WAF 功能。有关更多详细信息，请参阅[使用控制台](https://docs.aws.amazon.com/waf/latest/developerguide/working-with-console.html)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 如何显示验证码拼图
<a name="waf-js-captcha-api-render"></a>

本节提供了 `renderCaptcha` 实现的一个示例。

你可以在客户端界面中随心所欲地使用该 AWS WAF `renderCaptcha`呼叫。该呼叫从中检索验证码拼图 AWS WAF，对其进行渲染，然后将结果发送到进行验证。 AWS WAF 调用时，您需要提供拼图显示配置以及最终用户完成拼图时要运行的回调。有关选项的更多信息，请参阅上一部分 [验证码 API 规范 JavaScript](waf-js-captcha-api-specification.md)。

将此调用与智能威胁集成的令牌管理功能结合使用 APIs。该调用将为您的客户端提供一个令牌，用于验证验证码拼图是否成功完成。使用智能威胁集成 APIs 来管理令牌，并在客户端对受 AWS WAF 保护包 (Web ACLs) 保护的端点的调用中提供令牌。有关智能威胁的信息 APIs，请参阅[使用智能威胁 JavaScript API](waf-js-challenge-api.md)。

**实施示例**  
以下示例列表显示了标准的 CAPTCHA 实现，包括 AWS WAF 集成 URL 在该部分中的`<head>`位置。

此列表使用成功回调配置该`renderCaptcha`函数，该回调使用智能威胁集成的`AwsWafIntegration.fetch`封装器。 APIs有关此函数的信息，请参阅 [如何使用集成 `fetch` 包装程序](waf-js-challenge-api-fetch-wrapper.md)。

```
<head>
    <script type="text/javascript" src="<Integration URL>/jsapi.js" defer></script>
</head>

<script type="text/javascript">
    function showMyCaptcha() {
        var container = document.querySelector("#my-captcha-container");
        
        AwsWafCaptcha.renderCaptcha(container, {
            apiKey: "...API key goes here...",
            onSuccess: captchaExampleSuccessFunction,
            onError: captchaExampleErrorFunction,
            ...other configuration parameters as needed...
        });
    }
    
    function captchaExampleSuccessFunction(wafToken) {
        // Captcha completed. wafToken contains a valid WAF token. Store it for
        // use later or call AwsWafIntegration.fetch() to use it easily.
        // It will expire after a time, so calling AwsWafIntegration.getToken()
        // again is advised if the token is needed later on, outside of using the
        // fetch wrapper.
        
        // Use WAF token to access protected resources
        AwsWafIntegration.fetch("...WAF-protected URL...", {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: "{ ... }" /* body content */
        });
    }
    
    function captchaExampleErrorFunction(error) {
        /* Do something with the error */
    }
</script>

<div id="my-captcha-container">
    <!-- The contents of this container will be replaced by the captcha widget -->
</div>
```

**配置设置示例**  
以下示例列表显示了宽度和标题选项的非默认设置 `renderCaptcha`。

```
        AwsWafCaptcha.renderCaptcha(container, {
            apiKey: "...API key goes here...",
            onSuccess: captchaExampleSuccessFunction,
            onError: captchaExampleErrorFunction,
            dynamicWidth: true, 
            skipTitle: true
        });
```

有关配置选项的全部信息，请参阅 [验证码 API 规范 JavaScript](waf-js-captcha-api-specification.md)。