我为什么要介绍StringUtils
在shiro源码阅读的过程中,发现在做ShiroFilterFactoryBean对象的配置时,会配置filterChainDefinitionMap
filterChainDefinitionMap.put("api/**", "anon");
想知道这个value的配置遵循了什么原则,于是跟踪源码。
源码跟踪
//filterChainDefinitionMap
protected FilterChainManager createFilterChainManager() {
DefaultFilterChainManager manager = new DefaultFilterChainManager();
// ......省略不必要的代码
Map<String, String> chains = this.getFilterChainDefinitionMap();
if (!CollectionUtils.isEmpty(chains)) {
Iterator var12 = chains.entrySet().iterator();
while(var12.hasNext()) {
Entry<String, String> entry = (Entry)var12.next();
String url = (String)entry.getKey();
String chainDefinition = (String)entry.getValue();
// 重点,重点,重点------url就是那个key,chainDefinition就是那个value,在这里进去
manager.createChain(url, chainDefinition);
}
}
return manager;
}
public void createChain(String chainName, String chainDefinition) {
if (!StringUtils.hasText(chainName)) {
throw new NullPointerException("chainName cannot be null or empty.");
} else if (!StringUtils.hasText(chainDefinition)) {
throw new NullPointerException("chainDefinition cannot be null or empty.");
} else {
if (log.isDebugEnabled()) {
log.debug("Creating chain [" + chainName + "] from String definition [" + chainDefinition + "]");
}
// 重点,重点,重点------进入
String[] filterTokens = this.splitChainDefinition(chainDefinition);
String[] var4 = filterTokens;
int var5 = filterTokens.length;
for(int var6 = 0; var6 < var5; ++var6) {
String token = var4[var6];
String[] nameConfigPair = this.toNameConfigPair(token);
this.addToChain(chainName, nameConfigPair[0], nameConfigPair[1]);
}
}
}
protected String[] splitChainDefinition(String chainDefinition) {
// 看到是使用StringUtils的静态方法
return StringUtils.split(chainDefinition, ',', '[', ']', true, true);
}
一探究竟
StringUtils这个类是shiro框架自己的工具类,这里只对split方法进行分析,其它的方法有兴趣可以自己去研究
public static String[] split(String aLine, char delimiter, char beginQuoteChar, char endQuoteChar, boolean retainQuotes, boolean trimTokens) {
String line = clean(aLine);
if (line == null) {
return null;
} else {
List<String> tokens = new ArrayList();
StringBuilder sb = new StringBuilder();
boolean inQuotes = false;
for(int i = 0; i < line.length(); ++i) {
char c = line.charAt(i);
if (c == beginQuoteChar) {
if (inQuotes && line.length() > i + 1 && line.charAt(i + 1) == beginQuoteChar) {
sb.append(line.charAt(i + 1));
++i;
} else {
inQuotes = !inQuotes;
if (retainQuotes) {
sb.append(c);
}
}
} else if (c == endQuoteChar) {
inQuotes = !inQuotes;
if (retainQuotes) {
sb.append(c);
}
} else if (c == delimiter && !inQuotes) {
String s = sb.toString();
if (trimTokens) {
s = s.trim();
}
tokens.add(s);
sb = new StringBuilder();
} else {
sb.append(c);
}
}
String s = sb.toString();
if (trimTokens) {
s = s.trim();
}
tokens.add(s);
return (String[])tokens.toArray(new String[tokens.size()]);
}
}
参数讲解:
- aLine:需要分割的字符串
- delimiter:定界符
- beginQuoteChar:起始引号
- endQuoteChar:结束引号
- retainQuotes:分割结果是否保留引号
- trimTokens:分割结果是否删除两端的空格
方法使用示例:
String[] ss = StringUtils.split("perm[jj,kk],authc,role[ll,iuy]", ',', '[', ']', true, true);
for (String s : ss) {
System.out.println(s);
}
运行结果:
perm[jj, kk]
authc
role[ ll,iuy]
如有本文哪里有错误欢迎评论私信指导!!!