Cached.checkName

This commit is contained in:
redkale
2024-09-11 17:23:32 +08:00
parent 5aa3e61686
commit 20518259aa
2 changed files with 32 additions and 4 deletions

View File

@@ -17,6 +17,7 @@ import org.redkale.convert.json.JsonConvert;
import org.redkale.inject.ResourceFactory; import org.redkale.inject.ResourceFactory;
import org.redkale.util.Environment; import org.redkale.util.Environment;
import org.redkale.util.MultiHashKey; import org.redkale.util.MultiHashKey;
import org.redkale.util.RedkaleException;
import org.redkale.util.ThrowSupplier; import org.redkale.util.ThrowSupplier;
import org.redkale.util.TypeToken; import org.redkale.util.TypeToken;
@@ -100,7 +101,7 @@ public class CachedAction {
String init(ResourceFactory resourceFactory, Object service) { String init(ResourceFactory resourceFactory, Object service) {
this.manager = resourceFactory.load(environment.getPropertyValue(cached.getManager()), CachedManager.class); this.manager = resourceFactory.load(environment.getPropertyValue(cached.getManager()), CachedManager.class);
this.name = environment.getPropertyValue(cached.getName()); this.name = checkName(environment.getPropertyValue(cached.getName()));
this.key = environment.getPropertyValue(cached.getKey()); this.key = environment.getPropertyValue(cached.getKey());
if (key.startsWith("@")) { // 动态加载缓存key生成器 if (key.startsWith("@")) { // 动态加载缓存key生成器
String generatorName = key.substring(1); String generatorName = key.substring(1);
@@ -116,6 +117,28 @@ public class CachedAction {
return key; return key;
} }
/**
* 检查name是否含特殊字符
*
* @param value 参数
* @return value
*/
protected String checkName(String value) {
if (value != null && !value.isEmpty()) {
for (char ch : value.toCharArray()) {
if (!((ch >= '0' && ch <= '9')
|| (ch >= 'a' && ch <= 'z')
|| (ch >= 'A' && ch <= 'Z')
|| ch == '-'
|| ch == '_'
|| ch == '.')) { // 不能含特殊字符: # @
throw new RedkaleException("name only contains 0-9 a-z A-Z . - _");
}
}
}
return value;
}
@ClassDepends @ClassDepends
public <T> T get(ThrowSupplier<T> supplier, Object... args) { public <T> T get(ThrowSupplier<T> supplier, Object... args) {
if (async) { if (async) {

View File

@@ -137,7 +137,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
} }
/** /**
* 检查name是否含特殊字符 * 检查schema是否含特殊字符
* *
* @param value 参数 * @param value 参数
* @return value * @return value
@@ -145,8 +145,13 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
protected String checkSchema(String value) { protected String checkSchema(String value) {
if (value != null && !value.isEmpty()) { if (value != null && !value.isEmpty()) {
for (char ch : value.toCharArray()) { for (char ch : value.toCharArray()) {
if (ch == ':' || ch == '#' || ch == '@') { // 不能含特殊字符 if (!((ch >= '0' && ch <= '9')
throw new RedkaleException("schema cannot contains : # @"); || (ch >= 'a' && ch <= 'z')
|| (ch >= 'A' && ch <= 'Z')
|| ch == '-'
|| ch == '_'
|| ch == '.')) { // 不能含特殊字符: # @
throw new RedkaleException("schema only contains 0-9 a-z A-Z . - _");
} }
} }
} }