安全工具类
| 8 | * 安全工具类 |
| 9 | */ |
| 10 | public class Security { |
| 11 | /** |
| 12 | * sql注入检测 |
| 13 | */ |
| 14 | public static boolean checkSql(String content) { |
| 15 | String[] black_list = {"'", ";", "and", "exec", "insert", "select", "delete", "update", "count", "*", "chr", "mid", "master", "truncate", "char", "declare", "or"}; |
| 16 | for (String str : black_list) { |
| 17 | if (content.toLowerCase().contains(str)) { |
| 18 | return true; |
| 19 | } |
| 20 | } |
| 21 | return false; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * xss恶意字符过滤 |
| 26 | */ |
| 27 | public static String xssFilter(String content) { |
| 28 | content = StringUtils.replace(content, "&", "&"); |
| 29 | content = StringUtils.replace(content, "<", "<"); |
| 30 | content = StringUtils.replace(content, ">", ">"); |
| 31 | content = StringUtils.replace(content, "\"", """); |
| 32 | content = StringUtils.replace(content, "'", "'"); |
| 33 | content = StringUtils.replace(content, "/", "/"); |
| 34 | return content; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * 命令执行恶意字符检测 |
| 39 | */ |
| 40 | public static boolean checkCommand(String content) { |
| 41 | String[] black_list = {";", "&&", "||", "`", "$", "(", ")", ">", "<", "|", "\\", "[", "]", "{", "}", "echo", "exec", "system", "passthru", "popen", "proc_open", "shell_exec", "eval", "assert"}; |
| 42 | for (String str : black_list) { |
| 43 | if (content.toLowerCase().contains(str)) { |
| 44 | return true; |
| 45 | } |
| 46 | } |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * 合法IP地址检测 |
| 52 | */ |
| 53 | public static boolean checkIp(String ip) { |
| 54 | String[] ipArr = ip.split("\\."); |
| 55 | if (ipArr.length != 4) { |
| 56 | return false; |
| 57 | } |
| 58 | for (String ipSegment : ipArr) { |
| 59 | //需要进行异常判断,万一不是数字 |
| 60 | try { |
| 61 | int ipSegmentInt = Integer.parseInt(ipSegment); |
| 62 | if (ipSegmentInt < 0 || ipSegmentInt > 255) { |
| 63 | return false; |
| 64 | } |
| 65 | } catch (NumberFormatException e) { |
| 66 | return false; |
| 67 | } |
nothing calls this directly
no outgoing calls
no test coverage detected