@@ -213,7 +213,7 @@ private void handleDataMasking(SQLQueryResult result) {
213213 var maskRules = new HashMap <Integer , Common .DataMaskingRule >();
214214 for (var i = 0 ; i < result .getFields ().size (); i ++) {
215215 for (Common .DataMaskingRule rule : rules ) {
216- if (matchFiled (result .getFields ().get (i ), rule .getFieldsPattern ())) {
216+ if (this . matchField (result .getFields ().get (i ), rule .getFieldsPattern ())) {
217217 maskIndexes .add (i );
218218 maskRules .put (i , rule );
219219 }
@@ -236,21 +236,32 @@ private void handleDataMasking(SQLQueryResult result) {
236236 }
237237 }
238238
239- private boolean matchFiled (Field field , String pattern ) {
240- var names = List .of (field .getColumnName (), field .getLabel ());
241- var ps = pattern .split ("," );
239+ private boolean matchField (Field field , String pattern ) {
240+ List < String > names = List .of (field .getColumnName (), field .getLabel ());
241+ String [] ps = pattern .split ("," );
242242
243243 for (String name : names ) {
244244 for (String p : ps ) {
245+ p = p .trim ();
246+ if (p .isEmpty ()) continue ;
245247
246248 try {
247- int flags = Pattern .UNICODE_CASE | Pattern .CASE_INSENSITIVE ;
248- var pa = Pattern .compile (p , flags );
249- if (pa .matcher (name ).find ()) {
249+ // 先整体转义,避免用户写的正则符号被误解释
250+ String regex = Pattern .quote (p );
251+ // 把被转义的 \* 恢复为 .*
252+ regex = regex .replace ("\\ *" , ".*" );
253+ // 加上锚点,实现整串匹配
254+ regex = "^" + regex + "$" ;
255+
256+ int flags = Pattern .CASE_INSENSITIVE | Pattern .UNICODE_CASE ;
257+ Pattern pa = Pattern .compile (regex , flags );
258+
259+ if (pa .matcher (name ).matches ()) { // 注意:用 matches() 而不是 find()
250260 return true ;
251261 }
252262 } catch (PatternSyntaxException e ) {
253- return false ;
263+ // 忽略坏模式,继续下一个
264+ continue ;
254265 }
255266 }
256267 }
0 commit comments