使用正则解决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;
    }
相关推荐
清风66666613 小时前
基于单片机的PID调节脉动真空灭菌器上位机远程监控设计
数据库·单片机·毕业设计·nosql·课程设计·期末大作业
酩酊仙人13 小时前
ABP将ExtraProperties作为查询条件
数据库·postgresql·asp.net
在风中的意志13 小时前
[数据库SQL] [leetcode] 614. 二级关注者
数据库·sql
·云扬·13 小时前
MySQL Group Replication(MGR)核心特性全解析:从事务流程到一致性配置
数据库·mysql
陌路2013 小时前
MYSQL事务篇--事务隔离机制的实现
数据库·mysql
oMcLin14 小时前
CentOS 7.9 高负载导致 MySQL 数据库性能下降:内存泄漏与配置优化
数据库·mysql·centos
auspicious航14 小时前
数据库同步技术演进:从备份转储到实时CDC的DBA实战指南
数据库·ffmpeg·dba
SmartRadio14 小时前
物联网云平台数据库选型与搭建全指南(LoRaWAN)
数据库·物联网·lora·lorawan
bst@微胖子14 小时前
CrewAI+FastAPI实现营销战略协助智能体项目
android·数据库·fastapi
小鸡脚来咯14 小时前
MySQL面试题
数据库·mysql