使用正则解决SQL注入问题

在处理一些老接口的时候,发现之前很多接口的写法是前端传参后端直接拼写的方式,这种方式很容易产生sql注入,目前解决失去了注入的方式有

1.使用#符号做占位符,在动态解析时会被解析成?,而$会是""

sql 复制代码
-- 使用#{}时?会是占位符就算 name = "小米 or 1=1"也查不出来
select * from b where name = "?"


--使用${}是会变成 name = 小米 or 1=1
select * from b where name = 

2.使用PreparedStatement,PreparedStatement具有预编译功能

3.对敏感参数过滤

4.nginx反向代理防止SQL注入

因为我做的是老接口以后可能不维护,所以这里简单的用第三种:

复制代码
//算是一种穷尽出可能出现的关键字,校验input中是否包含某些关键字
public static boolean containsWord(String input) {
        if (StringUtils.isEmpty(input)){
            return false;
        }
        String badStr =
                "select|update|and|or|delete|insert|truncate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute|table|"+
                        "char|declare|sitename|xp_cmdshell|like|from|grant|use|group_concat|column_name|" +
                        "information_schema.columns|table_schema|union|where|order|by|" +
                        "*|;|+|,|//|/|%|#";
       String[] arr = badStr.split("\\|");
        for (String s : arr) {
            String regex = "\\b" + Pattern.quote(s) + "\\b";
           Boolean isTure =  Pattern.compile(regex).matcher(input).find();
           if (isTure){
               return true;
           }
        }
       return false;
    }
相关推荐
minebmw76 小时前
Oracle 19.29 中 ORA-00600 [4193] 错误完全解析与恢复指南
数据库·oracle
m0_377618236 小时前
Golang怎么连接MySQL数据库_Golang MySQL连接教程【总结】
jvm·数据库·python
weixin_586061467 小时前
C#怎么通过反射获取类属性_C#如何动态读取元数据【进阶】
jvm·数据库·python
Pluto_CSND7 小时前
PostgreSQL 聚合函数总览
数据库·postgresql
资深数据库专家7 小时前
总账EBS 应用服务器1 的监控分析
java·网络·数据库
m0_678485457 小时前
CSS如何控制表格单元格边框合并_通过border-collapse实现
jvm·数据库·python
m0_748839498 小时前
如何用组合继承模式实现父类方法复用与子类属性独立
jvm·数据库·python
qq_334563558 小时前
PHP源码是否依赖特定芯片组_Intel与AMD平台差异【操作】
jvm·数据库·python
qq_206901398 小时前
如何使用C#调用Oracle存储过程_OracleCommand配置CommandType.StoredProcedure
jvm·数据库·python
m0_748839498 小时前
CSS如何实现元素平滑滚动_使用scroll-behavior属性设置
jvm·数据库·python