SpringBoot2.0+shiro 读取yml文件

springboot2.0 读取配置文件是通过org.springframework.boot.context.properties.bind.JavaBeanBinder 进行参数的绑定。

       参数的处理是在org.springframework.boot.context.properties.source.ConfigurationPropertyName进行处理。此类springboot2.0新加的类。

        下列方法对yml文件中的key进行处理的过程

static ConfigurationPropertyName adapt(CharSequence name, char separator,
			Function<CharSequence, CharSequence> elementValueProcessor) {
		Assert.notNull(name, "Name must not be null");
		Assert.notNull(elementValueProcessor, "ElementValueProcessor must not be null");
		if (name.length() == 0) {
			return EMPTY;
		}
		List<CharSequence> elements = new ArrayList<>();
		process(name, separator, (elementValue, start, end, indexed) -> {
			elementValue = elementValueProcessor.apply(elementValue);
			if (!isIndexed(elementValue)) {
				elementValue = cleanupCharSequence(elementValue,
						(ch, index) -> ch != '_' && !ElementValidator
								.isValidChar(Character.toLowerCase(ch), index),
						CharProcessor.NONE);
			}
			if (elementValue.length() > 0) {
				elements.add(elementValue);
			}
		});
		return new ConfigurationPropertyName(elements.toArray(new CharSequence[0]));
	}

	private static void process(CharSequence name, char separator,
			ElementProcessor processor) {
		int start = 0;
		boolean indexed = false;
		int length = name.length();
		int openBracketCount = 0;
		for (int i = 0; i < length; i++) {
			char ch = name.charAt(i);
			if (ch == ']') {
				openBracketCount--;
				if (openBracketCount == 0) {
					processElement(processor, name, start, i + 1, indexed);
					start = i + 1;
					indexed = false;
				}
			}
			else if (ch == '[') {
				openBracketCount++;
				if (!indexed) {
					processElement(processor, name, start, i, indexed);
					start = i;
					indexed = true;
				}
			}
			else if (!indexed && ch == separator) {
				processElement(processor, name, start, i, indexed);
				start = i + 1;
			}
		}
		processElement(processor, name, start, length, false);
	}

	private static void processElement(ElementProcessor processor, CharSequence name,
			int start, int end, boolean indexed) {
		if ((end - start) >= 1) {
			processor.process(name.subSequence(start, end), start, end, indexed);
		}
	}

	private static CharSequence cleanupCharSequence(CharSequence name, CharFilter filter,
			CharProcessor processor) {
		for (int i = 0; i < name.length(); i++) {
			char ch = name.charAt(i);
			char processed = processor.process(ch, i);
			if (filter.isExcluded(processed, i) || processed != ch) {
				// We save memory by only creating the new result if necessary
				StringBuilder result = new StringBuilder(name.length());
				result.append(name.subSequence(0, i));
				for (int j = i; j < name.length(); j++) {
					processed = processor.process(name.charAt(j), j);
					if (!filter.isExcluded(processed, j)) {
						result.append(processed);
					}
				}
				return result;
			}
		}
		return name;
	}
processElement方法会调用adapt方法的  
process(name, separator, (elementValue, start, end, indexed) -> {
elementValue = elementValueProcessor.apply(elementValue);
			if (!isIndexed(elementValue)) {
				elementValue = cleanupCharSequence(elementValue,
						(ch, index) -> ch != '_' && !ElementValidator
								.isValidChar(Character.toLowerCase(ch), index),
						CharProcessor.NONE);
			}
			if (elementValue.length() > 0) {
				elements.add(elementValue);
			}
		});这里对key进行处理如果字符不是数字 字母将会被剔除掉			
elementValue = cleanupCharSequence(elementValue,(ch, index) -> ch != '_' && !ElementValidator
    .isValidChar(Character.toLowerCase(ch), index),CharProcessor.NONE);	

shiro在进行filterChainDefinitions配置时,如果使用map读取yml配置文件时,

spring:
  shiro: 
    filterChainDefinitions: 

      /static/**: anon

会变成 static:anon 

出现此类问题可以通过

spring:
  shiro: 
    filterChainDefinitions: | 
      /static/**: anon

      /login: anon

字符串换行的形式进行数据接受。

factoryBean.setFilterChainDefinitions(filterChainDefinitions);

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值