使用正则解决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;
    }
相关推荐
飞天红猪侠c2 分钟前
MySQL-逻辑架构
数据库·mysql
文牧之38 分钟前
AutoVACUUM (PostgreSQL) 与 DBMS_STATS.GATHER_DATABASE_STATS_JOB_PROC (Oracle) 对比
运维·数据库·postgresql·oracle
{⌐■_■}1 小时前
【redis】redis常见数据结构及其底层,redis单线程读写效率高于多线程的理解,
数据结构·数据库·redis
南风与鱼2 小时前
MySQL表的操作
数据库·mysql·表操作
banzhenfei3 小时前
xp_cmdshell bcp 导出文件
java·数据库·sql
CodeJourney.3 小时前
ChemBlender:科研绘图创新解决方案
数据库·人工智能·信息可视化·excel
笨蛋不要掉眼泪3 小时前
SpringAOP
java·数据库·spring·log4j
IvanCodes3 小时前
九、HQL DQL七大查询子句
大数据·数据库·hive
Channing Lewis4 小时前
如何判断一个网站后端是用什么语言写的
前端·数据库·python
難釋懷4 小时前
Android开发-数据库SQLite
android·数据库·sqlite