From b728f9a4b8fe532b132670a1ad03ff00e7284225 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A2=A7=E8=BF=9C?= Date: Mon, 18 Nov 2019 20:31:40 +0800 Subject: [PATCH] feat:reform the name black list --- .../alipay/hessian/NameBlackListFilter.java | 17 ++++++----- .../hessian/NameBlackListFilterManager.java | 30 +++++++++++++++++++ .../stc/bl/MockNameBlacklistFilter.java | 3 +- 3 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 src/test/java/com/alipay/hessian/NameBlackListFilterManager.java diff --git a/src/main/java/com/alipay/hessian/NameBlackListFilter.java b/src/main/java/com/alipay/hessian/NameBlackListFilter.java index fcad2a4..c8de670 100644 --- a/src/main/java/com/alipay/hessian/NameBlackListFilter.java +++ b/src/main/java/com/alipay/hessian/NameBlackListFilter.java @@ -32,12 +32,12 @@ public class NameBlackListFilter implements ClassNameFilter { /** * 黑名单 包名前缀 */ - protected List blackPrefixList; + protected static List blackPrefixList; /** * 类名是否在黑名单中结果缓存。{className:true/false} */ - protected ConcurrentMap resultOfInBlackList; + protected static ConcurrentMap resultOfInBlackList; /** * 指定黑名单前缀 @@ -55,8 +55,8 @@ public NameBlackListFilter(List blackPrefixList) { * @param maxCacheSize 最大缓存大小 */ public NameBlackListFilter(List blackPrefixList, int maxCacheSize) { - this.blackPrefixList = blackPrefixList; - buildCache(maxCacheSize); + NameBlackListFilter.blackPrefixList = blackPrefixList; + buildCache(blackPrefixList, maxCacheSize); } /** @@ -64,15 +64,16 @@ public NameBlackListFilter(List blackPrefixList, int maxCacheSize) { * * @param maxCacheSize 最大缓存 */ - protected void buildCache(int maxCacheSize) { + public static void buildCache(List blackPrefixList, int maxCacheSize) { if (blackPrefixList != null && !blackPrefixList.isEmpty()) { + NameBlackListFilter.blackPrefixList = blackPrefixList; int min = Math.min(256, maxCacheSize); int max = Math.min(10240, maxCacheSize); ConcurrentLinkedHashMap.Builder builder = new ConcurrentLinkedHashMap.Builder() .initialCapacity(min).maximumWeightedCapacity(max); - this.resultOfInBlackList = builder.build(); + NameBlackListFilter.resultOfInBlackList = builder.build(); } else { - this.resultOfInBlackList = null; + NameBlackListFilter.resultOfInBlackList = null; } } @@ -102,7 +103,7 @@ public String resolve(String className) throws IOException { * @param className * @return 是否在黑名单中 */ - private boolean inBlackList(String className) { + protected boolean inBlackList(String className) { for (String prefix : blackPrefixList) { if (className.startsWith(prefix)) { return Boolean.TRUE; diff --git a/src/test/java/com/alipay/hessian/NameBlackListFilterManager.java b/src/test/java/com/alipay/hessian/NameBlackListFilterManager.java new file mode 100644 index 0000000..1879e77 --- /dev/null +++ b/src/test/java/com/alipay/hessian/NameBlackListFilterManager.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.hessian; + +import java.util.List; + +public class NameBlackListFilterManager { + /** + * rebuild cache. + * @param blackList + */ + public static void rebuildBlackList(List blackList) { + + NameBlackListFilter.buildCache(blackList, 4096); + } +} diff --git a/src/test/java/com/alipay/stc/bl/MockNameBlacklistFilter.java b/src/test/java/com/alipay/stc/bl/MockNameBlacklistFilter.java index 71797ae..6c11bc5 100644 --- a/src/test/java/com/alipay/stc/bl/MockNameBlacklistFilter.java +++ b/src/test/java/com/alipay/stc/bl/MockNameBlacklistFilter.java @@ -33,7 +33,8 @@ public MockNameBlacklistFilter() { super(INTERNAL_BLACK_LIST); } - protected Boolean inBlackList(String className) { + @Override + protected boolean inBlackList(String className) { for (String prefix : INTERNAL_BLACK_LIST) { if (className.startsWith(prefix)) { return Boolean.TRUE;