# huaweicloud-reateIgnoreRule-java **Repository Path**: HuaweiCloudDeveloper/huaweicloud-reate-ignore-rule-java ## Basic Information - **Project Name**: huaweicloud-reateIgnoreRule-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,从而实现对全局白名单(原误报屏蔽)规则的快速操作。 当WAF根据您配置的Web基础防护规则或自定义规则检测到符合规则的恶意攻击时,会按照规则中的防护动作对攻击事件进行处理。 对于误报情况,您可以添加白名单对误报进行忽略,对某些规则ID或者事件类别进行忽略设置(例如,某URL不进行XSS的检查,可设置屏蔽规则,屏蔽XSS检查)。 - “**不检测模块**”选择“所有检测模块”时:通过WAF配置的其他所有的规则都不会生效,WAF将放行该域名下的所有请求流量。 - “**不检测模块**”选择“Web基础防护模块”时:可根据选择的“**不检测规则类型**”,对某些规则ID或者事件类别进行忽略设置(例如,某URL不进行XSS的检查,可设置屏蔽规则,屏蔽XSS检查)。 添加或修改防护规则后,规则生效需要等待几分钟。规则生效后,您可以在“防护事件”页面查看防护效果。 该示例展示了如何通过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 IgnoreDemo { private static final Logger logger = LoggerFactory.getLogger(IgnoreDemo.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 { createIgnore(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 CreateIgnoreRuleResponse createIgnore(WafClient client) { // 初始化防护域名或防护网站 List domains = new ArrayList<>(); domains.add("www.demo.com"); // 初始化防护ip List contents = new ArrayList<>(); contents.add("1.1.1.1"); // 初始化 CreateCondition createCondition = new CreateCondition() .withCategory("url") .withLogicOperation("contain") .withContents(contents) .withIndex(null); List createConditions = new ArrayList<>(); createConditions.add(createCondition); // 初始化条件列表 CreateIgnoreRuleRequestBody createIgnoreRuleRequestBody = new CreateIgnoreRuleRequestBody() .withDomain(domains) .withMode(1) .withDescription("demo") .withRule("bypass") .withConditions(createConditions); //初始化防护规则 CreateIgnoreRuleRequest createIgnoreRuleRequest = new CreateIgnoreRuleRequest() .withPolicyId("PolicyId") .withBody(createIgnoreRuleRequestBody); //创建全局白名单 CreateIgnoreRuleResponse createIgnoreRuleResponse = client.createIgnoreRule(createIgnoreRuleRequest); logger.info(createIgnoreRuleResponse.toString()); return createIgnoreRuleResponse; } } ``` ## 参考 更多信息请参考[API Explorer](https://console.huaweicloud.com/apiexplorer/#/openapi/WAF/doc?api=CreateIgnoreRule) ## 修订记录 | 发布日期 | 文档版本 | 修订说明 | | :--------: | :------: | :----------: | | 2023-07-20 | 1.0 | 文档首次发布 |