parent
1ac546abd9
commit
b34345b0a4
13 changed files with 308 additions and 17 deletions
@ -0,0 +1,64 @@ |
||||
package com.nov.KgLowDurable.util; |
||||
|
||||
import java.text.ParseException; |
||||
import java.text.SimpleDateFormat; |
||||
import java.util.Date; |
||||
import java.util.Locale; |
||||
import java.util.Queue; |
||||
import java.util.TimeZone; |
||||
import java.util.concurrent.ConcurrentLinkedQueue; |
||||
|
||||
public class ConcurrentDateFormat { |
||||
private final String format; |
||||
private final Locale locale; |
||||
private final TimeZone timezone; |
||||
private final Queue<SimpleDateFormat> queue = new ConcurrentLinkedQueue(); |
||||
|
||||
private ConcurrentDateFormat(String format, Locale locale, TimeZone timezone) { |
||||
this.format = format; |
||||
this.locale = locale; |
||||
this.timezone = timezone; |
||||
SimpleDateFormat initial = this.createInstance(); |
||||
this.queue.add(initial); |
||||
} |
||||
|
||||
public static ConcurrentDateFormat of(String format) { |
||||
return new ConcurrentDateFormat(format, Locale.getDefault(), TimeZone.getDefault()); |
||||
} |
||||
|
||||
public static ConcurrentDateFormat of(String format, TimeZone timezone) { |
||||
return new ConcurrentDateFormat(format, Locale.getDefault(), timezone); |
||||
} |
||||
|
||||
public static ConcurrentDateFormat of(String format, Locale locale, TimeZone timezone) { |
||||
return new ConcurrentDateFormat(format, locale, timezone); |
||||
} |
||||
|
||||
public String format(Date date) { |
||||
SimpleDateFormat sdf = (SimpleDateFormat)this.queue.poll(); |
||||
if (sdf == null) { |
||||
sdf = this.createInstance(); |
||||
} |
||||
|
||||
String result = sdf.format(date); |
||||
this.queue.add(sdf); |
||||
return result; |
||||
} |
||||
|
||||
public Date parse(String source) throws ParseException { |
||||
SimpleDateFormat sdf = (SimpleDateFormat)this.queue.poll(); |
||||
if (sdf == null) { |
||||
sdf = this.createInstance(); |
||||
} |
||||
|
||||
Date result = sdf.parse(source); |
||||
this.queue.add(sdf); |
||||
return result; |
||||
} |
||||
|
||||
private SimpleDateFormat createInstance() { |
||||
SimpleDateFormat sdf = new SimpleDateFormat(this.format, this.locale); |
||||
sdf.setTimeZone(this.timezone); |
||||
return sdf; |
||||
} |
||||
} |
||||
@ -0,0 +1,25 @@ |
||||
package com.nov.KgLowDurable.util; |
||||
|
||||
import com.nov.KgLowDurable.exception.CustomerException; |
||||
|
||||
import java.text.ParseException; |
||||
import java.time.format.DateTimeFormatter; |
||||
import java.util.Date; |
||||
|
||||
public class DateUtil { |
||||
|
||||
public static final ConcurrentDateFormat DATETIME_FORMAT = ConcurrentDateFormat.of("yyyy-MM-dd HH:mm:ss"); |
||||
public static final ConcurrentDateFormat DATETIME_MINI_FORMAT = ConcurrentDateFormat.of("yyyyMMddHHmmss"); |
||||
public static final ConcurrentDateFormat DATE_FORMAT = ConcurrentDateFormat.of("yyyy-MM-dd"); |
||||
public static final ConcurrentDateFormat TIME_FORMAT = ConcurrentDateFormat.of("HH:mm:ss"); |
||||
|
||||
public static Date parse(String dateStr, String pattern) { |
||||
ConcurrentDateFormat format = ConcurrentDateFormat.of(pattern); |
||||
|
||||
try { |
||||
return format.parse(dateStr); |
||||
} catch (ParseException var4) { |
||||
throw new CustomerException("系统错误"); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,14 @@ |
||||
package com.nov.KgLowDurable.util; |
||||
|
||||
import org.springframework.lang.Nullable; |
||||
import org.springframework.util.ObjectUtils; |
||||
|
||||
public class ObjectUtil extends ObjectUtils { |
||||
public ObjectUtil() { |
||||
} |
||||
|
||||
public static boolean isNotEmpty(@Nullable Object obj) { |
||||
return !isEmpty(obj); |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,109 @@ |
||||
package com.nov.KgLowDurable.util; |
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||
import com.nov.KgLowDurable.exception.CustomerException; |
||||
|
||||
import java.sql.SQLException; |
||||
import java.time.format.DateTimeFormatter; |
||||
import java.util.Map; |
||||
import java.util.regex.Pattern; |
||||
|
||||
public class SqlKeyword { |
||||
private static final String SQL_REGEX = "(?i)(?<![a-z])('|%|--|insert|delete|select|sleep|count|update|updatexml|group|union|drop|truncate|alter|grant|execute|exec|xp_cmdshell|call|declare|sql)(?![a-z])"; |
||||
private static final Pattern PATTERN = Pattern.compile("(?:--|[\"';%]|\\binsert\\b|\\bdelete\\b|\\bselect\\b|\\bcount\\b|\\bupdate\\b|\\bupdatexml\\b|\\bsleep\\b|group\\s+by|\\bunion\\b|\\bdrop\\b|\\btruncate\\b|\\balter\\b|\\bgrant\\b|\\bexecute\\b|\\bxp_cmdshell\\b|\\bcall\\b|\\bdeclare\\b|\\bsql\\b)"); |
||||
private static final String SQL_INJECTION_MESSAGE = "SQL keyword injection prevention processing!"; |
||||
private static final String SQL_EMPTY_MESSAGE = "SQL keyword is empty!"; |
||||
private static final String EQUAL = "_equal"; |
||||
private static final String NOT_EQUAL = "_notequal"; |
||||
private static final String LIKE = "_like"; |
||||
private static final String LIKE_LEFT = "_likeleft"; |
||||
private static final String LIKE_RIGHT = "_likeright"; |
||||
private static final String NOT_LIKE = "_notlike"; |
||||
private static final String GE = "_ge"; |
||||
private static final String LE = "_le"; |
||||
private static final String GT = "_gt"; |
||||
private static final String LT = "_lt"; |
||||
private static final String DATE_GE = "_datege"; |
||||
private static final String DATE_GT = "_dategt"; |
||||
private static final String DATE_EQUAL = "_dateequal"; |
||||
private static final String DATE_LT = "_datelt"; |
||||
private static final String DATE_LE = "_datele"; |
||||
private static final String IS_NULL = "_null"; |
||||
private static final String NOT_NULL = "_notnull"; |
||||
private static final String IGNORE = "_ignore"; |
||||
|
||||
public SqlKeyword() { |
||||
} |
||||
|
||||
public static void buildCondition(Map<String, Object> query, QueryWrapper<?> qw) { |
||||
if (!Func.isEmpty(query)) { |
||||
query.forEach((k, v) -> { |
||||
if (!Func.hasEmpty(new Object[]{k, v}) && !k.endsWith("_ignore")) { |
||||
k = filter(k); |
||||
if (k.endsWith("_equal")) { |
||||
qw.eq(getColumn(k, "_equal"), v); |
||||
} else if (k.endsWith("_notequal")) { |
||||
qw.ne(getColumn(k, "_notequal"), v); |
||||
} else if (k.endsWith("_likeleft")) { |
||||
qw.likeLeft(getColumn(k, "_likeleft"), v); |
||||
} else if (k.endsWith("_likeright")) { |
||||
qw.likeRight(getColumn(k, "_likeright"), v); |
||||
} else if (k.endsWith("_notlike")) { |
||||
qw.notLike(getColumn(k, "_notlike"), v); |
||||
} else if (k.endsWith("_ge")) { |
||||
qw.ge(getColumn(k, "_ge"), v); |
||||
} else if (k.endsWith("_le")) { |
||||
qw.le(getColumn(k, "_le"), v); |
||||
} else if (k.endsWith("_gt")) { |
||||
qw.gt(getColumn(k, "_gt"), v); |
||||
} else if (k.endsWith("_lt")) { |
||||
qw.lt(getColumn(k, "_lt"), v); |
||||
} else if (k.endsWith("_datege")) { |
||||
qw.ge(getColumn(k, "_datege"), DateUtil.parse(String.valueOf(v), "yyyy-MM-dd HH:mm:ss")); |
||||
} else if (k.endsWith("_dategt")) { |
||||
qw.gt(getColumn(k, "_dategt"), DateUtil.parse(String.valueOf(v), "yyyy-MM-dd HH:mm:ss")); |
||||
} else if (k.endsWith("_dateequal")) { |
||||
qw.eq(getColumn(k, "_dateequal"), DateUtil.parse(String.valueOf(v), "yyyy-MM-dd HH:mm:ss")); |
||||
} else if (k.endsWith("_datele")) { |
||||
qw.le(getColumn(k, "_datele"), DateUtil.parse(String.valueOf(v), "yyyy-MM-dd HH:mm:ss")); |
||||
} else if (k.endsWith("_datelt")) { |
||||
qw.lt(getColumn(k, "_datelt"), DateUtil.parse(String.valueOf(v), "yyyy-MM-dd HH:mm:ss")); |
||||
} else if (k.endsWith("_null")) { |
||||
qw.isNull(getColumn(k, "_null")); |
||||
} else if (k.endsWith("_notnull")) { |
||||
qw.isNotNull(getColumn(k, "_notnull")); |
||||
} else { |
||||
qw.like(getColumn(k, "_like"), v); |
||||
} |
||||
|
||||
} |
||||
}); |
||||
} |
||||
} |
||||
|
||||
private static String getColumn(String column, String keyword) { |
||||
return StringUtil.humpToUnderline(StringUtil.removeSuffix(column, keyword)); |
||||
} |
||||
|
||||
public static String filter(String param) { |
||||
try { |
||||
String cleaned = StringUtil.cleanIdentifier(param); |
||||
if (cleaned == null) { |
||||
throw new SQLException("SQL keyword is empty!"); |
||||
} else { |
||||
String sql = cleaned.replaceAll("(?i)(?<![a-z])('|%|--|insert|delete|select|sleep|count|update|updatexml|group|union|drop|truncate|alter|grant|execute|exec|xp_cmdshell|call|declare|sql)(?![a-z])", ""); |
||||
if (match(sql)) { |
||||
throw new SQLException("SQL keyword injection prevention processing!"); |
||||
} else { |
||||
return sql; |
||||
} |
||||
} |
||||
} catch (SQLException var3) { |
||||
throw new CustomerException("系统错误"); |
||||
} |
||||
} |
||||
|
||||
public static Boolean match(String param) { |
||||
return Func.isNotEmpty(param) && PATTERN.matcher(param).find(); |
||||
} |
||||
} |
||||
@ -0,0 +1,53 @@ |
||||
package com.nov.KgLowDurable.util; |
||||
|
||||
import org.springframework.lang.Nullable; |
||||
|
||||
import static com.baomidou.mybatisplus.core.toolkit.StringUtils.firstCharToLower; |
||||
import static com.nov.KgLowDurable.util.Condition.subPre; |
||||
import static com.nov.KgLowDurable.util.StringUtils.isBlank; |
||||
|
||||
public class StringUtil { |
||||
public static String humpToUnderline(String para) { |
||||
if (isBlank(para)) { |
||||
return ""; |
||||
} else { |
||||
para = firstCharToLower(para); |
||||
StringBuilder sb = new StringBuilder(para); |
||||
int temp = 0; |
||||
|
||||
for(int i = 0; i < para.length(); ++i) { |
||||
if (Character.isUpperCase(para.charAt(i))) { |
||||
sb.insert(i + temp, "_"); |
||||
++temp; |
||||
} |
||||
} |
||||
|
||||
return sb.toString().toLowerCase(); |
||||
} |
||||
} |
||||
public static String removeSuffix(CharSequence str, CharSequence suffix) { |
||||
if (!ObjectUtil.isEmpty(str) && !ObjectUtil.isEmpty(suffix)) { |
||||
String str2 = str.toString(); |
||||
return str2.endsWith(suffix.toString()) ? subPre(str2, str2.length() - suffix.length()) : str2; |
||||
} else { |
||||
return ""; |
||||
} |
||||
} |
||||
@Nullable |
||||
public static String cleanIdentifier(@Nullable String param) { |
||||
if (param == null) { |
||||
return null; |
||||
} else { |
||||
StringBuilder paramBuilder = new StringBuilder(); |
||||
|
||||
for(int i = 0; i < param.length(); ++i) { |
||||
char c = param.charAt(i); |
||||
if (Character.isJavaIdentifierPart(c)) { |
||||
paramBuilder.append(c); |
||||
} |
||||
} |
||||
|
||||
return paramBuilder.toString(); |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue