使用正则解决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;
    }
相关推荐
q***816434 分钟前
MySQL:数据查询-limit
数据库·mysql
p***924835 分钟前
DBeaver连接本地MySQL、创建数据库表的基础操作
数据库·mysql
JIngJaneIL2 小时前
社区互助|社区交易|基于springboot+vue的社区互助交易系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·社区互助
晚风吹人醒.2 小时前
缓存中间件Redis安装及功能演示、企业案例
linux·数据库·redis·ubuntu·缓存·中间件
Y***98512 小时前
DVWA靶场通关——SQL Injection篇
数据库·sql
Yawesh_best2 小时前
告别系统壁垒!WSL+cpolar 让跨平台开发效率翻倍
运维·服务器·数据库·笔记·web安全
蒋士峰DBA修行之路2 小时前
实验二十八 SQL PATCH调优
数据库·sql·gaussdb
I***t7163 小时前
一条sql 在MySQL中是如何执行的
数据库·sql·mysql
一 乐3 小时前
应急知识学习|基于springboot+vue的应急知识学习系统(源码+数据库+文档)
数据库·vue.js·spring boot
微学AI4 小时前
内网穿透的应用-突破局域网束缚,MongoDB 远程访问使用cpolar原来可以这么简单
数据库·mongodb