博客
关于我
java防止XSS、SQL注入攻击
阅读量:532 次
发布时间:2019-03-08

本文共 4483 字,大约阅读时间需要 14 分钟。

XSS和SQL注入防护技术

XSS(Cross-Site Scripting)和SQL注入是Web应用中的两大安全威胁。作为开发者,我们需要了解它们的本质、危害以及如何有效防护。

XSS简介

XSS攻击的核心在于攻击者通过未经过滤或转换的用户输入,嵌入恶意脚本至Web页面。这些脚本会在其他用户的浏览器中执行,导致数据泄露、 Session劫持甚至远程控制等安全问题。主要攻击场景包括:

  • 用户信息窃取(如密码、验证码)
  • 数据库信息窃取或修改
  • 网站内容替换或挂马
  • 非法转账或收款

XSS防护方法

防止XSS的关键在于对用户输入数据进行适当的脱敏处理。常见措施包括:

  • _marginally sanitize输入数据,移除或转换危险字符(如脚本标签)
  • 使用HTML编码转换特殊字符
  • 限制用户提交的数据类型
  • 强制设置HTTP.repositorieswhose的cookie属性(防止JavaScript获取cookie)
  • 过滤URL参数中的无效或危险字符

SQL注入简介

SQL注入攻击通过将恶意SQL语句注入Web应用的查询参数中,绕过应用的安全控制,执行除用户预期外的数据库操作。当攻击者掌握数据库访问权限时,这种威胁尤其严重。常见攻击效果包括:

  • 数据库信息暴露
  • Web页面篡改
  • 数据库服务器被远程控制
  • 用户数据删除或修改

SQL注入防护方法

防止SQL注入的核心是确保敏感操作使用参数化查询或预编译语句。具体措施包括:

  • 对用户输入的查询参数进行严格的校验和过滤
  • 避免拼接SQL语句(如使用string concatenation)
  • 使用ORM框架或安全数据库连接(防止SQL注入特定语法的执行)
  • 配置数据库只允许特定的访问权限
  • 使用安全存储过程代替直接执行用户输入的SQL

以Spring Boot为例的代码示例

下面是Spring Boot项目中常用的防XSS和SQL注入的实现方案。

XssFilter类

public class XssFilter implements Filter {    private FilterConfig filterConfig;    private List
urlExclusion; public XssFilter() { urlExclusion = new ArrayList<>(); } public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; } public void destroy() { this.filterConfig = null; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; String servletPath = httpRequest.getServletPath(); if (urlExclusion != null && urlExclusion.contains(servletPath)) { chain.doFilter(request, response); } else { chain.doFilter(new XssHttpServletRequestWrapper(httpRequest), response); } } public List
getUrlExclusion() { return urlExclusion; } public void setUrlExclusion(List
urlExclusion) { this.urlExclusion = urlExclusion; }}

XssHttpServletRequestWrapper类

public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {    private boolean filterXSS = true;    private boolean filterSQL = true;    public XssHttpServletRequestWrapper(HttpServletRequest servletRequest) {        super(servletRequest);        this.filterXSS = true;        this.filterSQL = true;    }    public String[] getParameterValues(String parameter) {        String[] values = super.getParameterValues(parameter);        if (values == null) {            return null;        }        String[] encodedValues = new String[values.length];        for (int i = 0; i < values.length; i++) {            encodedValues[i] = filterParamString(values[i]);        }        return encodedValues;    }    public String getParameter(String parameter) {        return filterParamString(super.getParameter(parameter));    }    public String getHeader(String name) {        return filterParamString(super.getHeader(name));    }    public Cookie[] getCookies() {        Cookie[] cookies = super.getCookies();        if (cookies != null) {            for (Cookie cookie : cookies) {                cookie.setValue(filterParamString(cookie.getValue()));            }        }        return cookies;    }    protected String filterParamString(String rawValue) {        if (filterXSS) {            rawValue = stripXSS(rawValue);        }        if (filterSQL) {            rawValue = stripSQLInjection(rawValue);        }        return rawValue;    }    private static String stripXSS(String value) {        if (value == null) {            return null;        }        // 替换需要去除的字符或者脚本标签        value = value.replaceAll("<", "&lt;")                     .replaceAll(">", "&gt;")                     .replaceAll(""", "&quot;")                     .replaceAll("'", "&39;")                     .replaceAll("({)(.*)(})", "")                     .replaceAll("วง Precisely beams.                    return value;                } الح"}

WafRequestWrapper类

public class WafRequestWrapper extends HttpServletRequestWrapper {    private boolean filterXSS = true;    private boolean filterSQL = true;    public WafRequestWrapper(HttpServletRequest request, boolean filterXSS, boolean filterSQL) {        super(request);        this.filterXSS = filterXSS;        this.filterSQL = filterSQL;    }    public WafRequestWrapper(HttpServletRequest request) {        super(request);        this(new WafRequestWrapper(request, true, true));    }    ...}

总结

XSS和SQL注入是Web安全中的常见威胁。通过合理的输入数据过滤、脱敏处理及使用安全框架,我们可以有效防范这些攻击。代码中的XssFilter和WafRequestWrapper提供了Spring Boot项目中常用的防护方法,确保用户输入的安全性,保护Web应用免受攻击。

转载地址:http://dasiz.baihongyu.com/

你可能感兴趣的文章
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>