使用正则解决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;
    }
相关推荐
parafeeee6 小时前
程序人生-Hello’s P2P
数据库·后端·asp.net
欲买桂花同载酒5826 小时前
程序人生-Hello’s P2P
运维·服务器·数据库
iPadiPhone6 小时前
流量洪峰下的数据守护者:InnoDB MVCC 全实现深度解析
java·数据库·mysql·面试
NineData6 小时前
AI时代的数据对比:DBA还需要盯着屏幕看差异吗?
运维·数据库
Javatutouhouduan6 小时前
SpringBoot整合reids:JSON序列化文件夹操作实录
java·数据库·redis·html·springboot·java编程·java程序员
QWQ___qwq7 小时前
Spring Security + MyBatis-Plus 实现自定义数据库用户认证
数据库·spring·mybatis
Filotimo_8 小时前
Java后端开发标准流程:从数据库到接口的完整实现
数据库·oracle
泯仲8 小时前
从零起步学习MySQL 第一章:初识MySQL及深入理解内部数据类型
数据库·mysql
有想法的py工程师8 小时前
PostgreSQL 触发器性能评估实战(pg_stat_user_functions)
数据库·postgresql
御坂10101号8 小时前
「2>&1」是什么意思?半个世纪的 Unix 谜题
java·数据库·bash·unix