java清除xss漏洞关键字

private static String cleanXSS(String value) {
    if (StringUtils.isNotBlank(value)) {
        value = value.replaceAll("<", "<");
        value = value.replaceAll(">", ">");
        value = value.replaceAll("'", "'");
        value = value.replaceAll("script", "");
        value = value.replaceAll("alert", "");
        value = value.replaceAll("javascript", "");
        value = value.replaceAll("javasc", "");
        value = value.replaceAll(";", "﹔");
        value = value.replaceAll("&", "&");
        value = value.replaceAll("#", "#");
        value = value.replaceAll("html", "");
        value = value.replaceAll("\\[", "");
        value = value.replaceAll("wget", "wg?t");// "c"→"?"
        value = value.replaceAll(".ini", "");// "c"→"?"
        value = value.replaceAll("powershell", "pow?rsh?ll");// "c"→"?"
        value = value.replaceAll("select", "sele?t");// "c"→"?"
        value = value.replaceAll("truncate", "trun?ate");// "c"→"?"
        value = value.replaceAll("exec", "exe?");// "c"→"?"
        value = value.replaceAll("join", "j?in");// "o"→"?"
        value = value.replaceAll("union", "uni?n");// "o"→"?"
        value = value.replaceAll("drop", "dr?p");// "o"→"?"
        value = value.replaceAll("count", "c?unt");// "o"→"?"
        value = value.replaceAll("insert", "ins?rt");// "e"→"?"
        value = value.replaceAll("update", "updat?");// "e"→"?"
        value = value.replaceAll("delete", "d?l?t?");// "e"→"?"
        value = value.replaceAll("script", "s?ript");// "c"→"?"
        value = value.replaceAll("cookie", "c??kie");// "o"→"?"
        value = value.replaceAll("iframe", "ifram?");// "e"→"?"
        value = value.replaceAll("onmouseover", "?nmous?ov?r");// "e"→"?"
        value = value.replaceAll("onmousemove", "?nmous?mov?");// "e"→"?"*/
        value = value.replaceAll("onclick", "");
        value = value.replaceAll("prompt", "");
        value = value.replaceAll("onmouseenter", "");
        value = value.replaceAll("confirm", "");
        value = value.replaceAll("eval", "");
        value = value.replaceAll("onerror", "");// "e"→"?"*/
    }
    return value;
}
### 防止和修复 Java 应用程序中的 XSS 跨站脚本攻击 #### 使用输入验证和清理 确保所有来自客户端的数据都经过严格的验证和清理。对于任何可能被显示到网页上的数据,应该移除或转义 HTML 和 JavaScript 特殊字符。这可以通过正则表达式或其他专门来实现[^1]。 ```java public String escapeHtml(String input) { if (input == null || input.isEmpty()) { return ""; } StringBuilder escapedString = new StringBuilder(); for (char c : input.toCharArray()) { switch(c){ case '<': escapedString.append("<"); break; case '>': escapedString.append(">"); break; case '&': escapedString.append("&"); break; default: escapedString.append(c); } } return escapedString.toString(); } ``` #### HTTP 响应头设置 通过适当配置HTTP响应头部可以有效减少XSS风险。例如,`Content-Security-Policy` 头部能够定义哪些资源可以从何处加载;而 `X-XSS-Protection` 则启用了浏览器内置的反射型XSS防护机制[^2]。 ```properties # application.properties 中添加如下配置项 server.servlet.session.cookie.http-only=true security.headers.contentSecurityPolicy=script-src 'self' security.headers.xssProtection.enabled=true ``` #### 输出编码 在渲染HTML之前对动态内容进行适当的编码处理非常重要。不同上下文中需要采用不同的编码方式——比如URL参数要用百分号编码(`%`),属性值要加双引号并转义其中的特殊字符等。许多模板引擎已经提供了自动化的输出编码功能[^3]。 ```jsp <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <c:out value="${userInput}" /> <!-- 或者 --> ${fn:escapeXml(userInput)} ``` #### Web 安全过滤器 部署一个全局的安全过滤器可以在请求到达业务逻辑层前就对其进行净化操作。此方法适用于整个应用程序范围内的保护措施实施[^4]。 ```xml <filter> <filter-name>xssFilter</filter-name> <filter-class>com.example.security.XssFilter</filter-class> </filter> <filter-mapping> <filter-name>xssFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拼命小孩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值