使用正则解决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;
    }
相关推荐
wang60212521819 小时前
阿里云存储的一些简要概述
数据库·阿里云·fastapi
小徐Chao努力20 小时前
【Langchain4j-Java AI开发】08-向量嵌入与向量数据库
java·数据库·人工智能
TG:@yunlaoda360 云老大20 小时前
华为云国际站代理商GSL主要有什么作用呢?
网络·数据库·华为云
TG:@yunlaoda360 云老大20 小时前
华为云国际站代理商GSL的流量用量与资费合规是如何实现的?
网络·数据库·华为云
冰冰菜的扣jio20 小时前
MySQL三大重要日志详解
数据库·mysql
l1t20 小时前
postgresql递归查询指定搜索顺序的方法
数据库·postgresql·dfs·递归·cte
java1234_小锋21 小时前
Redis的热Key问题如何解决?
数据库·redis·缓存
wang60212521821 小时前
FastAPI框架为什么在启动时建表
数据库
男孩李21 小时前
linux下如何执行postgres数据库的sql文件
数据库·sql·postgresql
zwjapple21 小时前
MySQL SQL 面试核心考点与注意事项总结
数据库·sql·mysql