package xiaozhi.common.xss; import org.apache.commons.lang3.StringUtils; import xiaozhi.common.exception.ErrorCode; import xiaozhi.common.exception.RenException; /** * SQL过滤 * Copyright (c) 人人开源 All rights reserved. * Website: https://www.renren.io */ public class SqlFilter { /** * SQL注入过滤 * * @param str 待验证的字符串 */ public static String sqlInject(String str) { if (StringUtils.isBlank(str)) { return null; } // 去掉'|"|;|\字符 str = str.replace("'", ""); str = str.replace("\"", ""); str = str.replace(";", ""); str = str.replace("\\", ""); // 转换成小写 str = str.toLowerCase(); // 非法字符 String[] keywords = { "master", "truncate", "insert", "select", "delete", "update", "declare", "alter", "drop" }; // 判断是否包含非法字符 for (String keyword : keywords) { if (str.contains(keyword)) { throw new RenException(ErrorCode.INVALID_SYMBOL); } } return str; } }