You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
package com.xydl.cac.util ;
import org.apache.commons.lang3.StringUtils ;
public class SqlEscapeUtil {
/**
* 转义字符
**/
public static final char ESCAPE = '!' ;
/**
* 转义方法
*
* @param param 待转义字符串
* @return String
*/
public static String escape ( String param ) {
if ( StringUtils . isNotEmpty ( param ) ) {
String temp = param . replaceAll ( "/" , ESCAPE + "/" ) ;
temp = temp . replaceAll ( "%" , ESCAPE + "%" ) ;
temp = temp . replaceAll ( "_" , ESCAPE + "_" ) ;
temp = temp . replaceAll ( "'" , "''" ) ;
return temp . trim ( ) ;
}
return "" ;
}
public static boolean sqlValidate ( String str ) {
if ( StringUtils . isBlank ( str ) ) {
return false ;
}
str = str . toLowerCase ( ) ; // 统一转为小写
String badStr = "'|exec|and|or|execute|insert|select|delete|update|drop|%|master|truncate|"
+ "declare|sitename|net user|xp_cmdshell|like'|exec|execute|insert|create|drop|"
+ "table|grant|use|group_concat|column_name|information_schema.columns|table_schema|"
+ "select|delete|update|master|truncate|declare|-- |like|//|%" ; // 过滤掉的sql关键字, 可以手动添加
String [ ] badStrs = badStr . split ( "\\|" ) ;
for ( String s : badStrs ) {
if ( str . contains ( s ) ) {
return true ;
}
}
return false ;
}
}