# huaweicloud-UpdateWhiteblackipRule-java **Repository Path**: HuaweiCloudDeveloper/huaweicloud-update-whiteblackip-rule-java ## Basic Information - **Project Name**: huaweicloud-UpdateWhiteblackipRule-java - **Description**: 用于存放更新黑白名单防护规则的示例代码 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master-dev - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-07-19 - **Last Updated**: 2025-06-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 功能介绍 华为云提供了Web应用防火墙SDK,您可以直接集成使用服务端SDK来调用Web应用防火墙的相关API,从而实现对黑白名单防护规则的快速操作。 IP地址默认全部放行,您可以通过配置黑白名单规则,阻断、仅记录或放行指定IP地址/IP地址段的访问请求。配置黑白名单规则时,WAF支持单个添加或通过引用地址组批量导入黑白名单IP地址/IP地址段。 添加或修改防护规则后,规则生效需要等待几分钟。规则生效后,您可以在“防护事件”页面查看防护效果 该示例展示了如何通过java版SDK更新黑白名单防护规则。 ## 前置条件 - 1、获取华为云开发工具包(SDK),您也可以查看安装JAVA SDK。 - 2、要使用华为云 Java SDK,您需要拥有华为云账号以及该账号对应的 Access Key(AK)和 Secret Access Key(SK)。 - 3、华为云 Java SDK 支持 **Java JDK 1.8** 及其以上版本。 ### SDK获取和安装 您可以通过Maven配置所依赖的主机迁移服务SDK ```xml com.huaweicloud.sdk huaweicloud-sdk-all ${sdk-version} ``` ## 示例代码 ```java public class WhiteblackipDemo { private static final Logger logger = LoggerFactory.getLogger(WhiteblackipDemo.class); public static void main(String[] args) { ICredential auth = new BasicCredentials() .withAk("") .withSk(""); WafClient client = WafClient.newBuilder() .withCredential(auth) .withRegion(WafRegion.valueOf("cn-south-1")) .build(); try { updateWhiteblackip(client); } catch (ConnectionException e) { logger.error("A connect timeout exception occurs while the WAF performs some certificate-related operations, exception: {}", e); } catch (RequestTimeoutException e) { logger.error("A request timeout exception occurs while the WAF performs some certificate-related operations, exception: {}", e); } catch (ServiceResponseException e) { logger.error("there is service response error, exception: {}", e); logger.error("HttpStatusCode: {}", e.getHttpStatusCode()); logger.error("RequestId: {}", e.getRequestId()); logger.error("ErrorCode: {}", e.getErrorCode()); logger.error("ErrorMsg: {}", e.getErrorMsg()); } } private static UpdateWhiteblackipRuleResponse updateWhiteblackip(WafClient client) { // 初始化请求body UpdateWhiteBlackIpRuleRequestBody updateWhiteBlackIpRuleRequestBody = new UpdateWhiteBlackIpRuleRequestBody() .withName("demo1") .withWhite(0) .withDescription("demo1") .withAddr("2.2.2.2"); // 初始化黑白名单规则信息 UpdateWhiteblackipRuleRequest updateWhiteblackipRuleRequest = new UpdateWhiteblackipRuleRequest() .withPolicyId("PolicyId") .withRuleId("RuleId") .withBody(updateWhiteBlackIpRuleRequestBody); // 创建黑白名单规则 UpdateWhiteblackipRuleResponse updateWhiteblackipRuleResponse = client.updateWhiteblackipRule(updateWhiteblackipRuleRequest); logger.info(updateWhiteblackipRuleResponse.toString()); return updateWhiteblackipRuleResponse; } } ``` ## 参考 更多信息请参考[API Explorer](https://console.huaweicloud.com/apiexplorer/#/openapi/WAF/doc?api=UpdateWhiteblackipRule) ## 修订记录 | 发布日期 | 文档版本 | 修订说明 | | :--------: | :------: | :----------: | | 2023-07-20 | 1.0 | 文档首次发布 |