解決Properties屬性文件中的值有等號和換行的小問題
Properties屬性文件中的值有等號和換行
Spring配置Shiro的過濾器時,有個filterChainDefinitions屬性,值中有等號有換行,嘗試寫到Properties屬性文件中遇到問題
<!-- 配置shiro過濾器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 表示現(xiàn)在要配置的是一個安全管理器 -->
<property name="securityManager" ref="securityManager"/>
<!-- 出現(xiàn)錯誤之后的跳轉(zhuǎn)路徑的配置 -->
<property name="loginUrl" value="/login.html"/>
<!-- 認證失敗之后的跳轉(zhuǎn)路徑頁面 -->
<property name="unauthorizedUrl" value="/login.html"/>
<!-- 登錄成功之后的跳轉(zhuǎn)訪問路徑 -->
<property name="successUrl" value="/pages/welcome.jsp"/>
<property name="filterChainDefinitions">
<value>
/admin=authc
/logout=logout
/xxxxxx=user
</value>
</property>
</bean>
Properties屬性文件可以這樣寫
shiro.loginUrl=/login.html
shiro.unauthorizedUrl=/login.html
shiro.successUrl=/pages/welcome.jsp
shiro.filterChainDefinitions=/admin=authc \n\
/logout=logout \n\
/info=authc
后面的等號不需要轉(zhuǎn)義,\n表示值中的換行,再加個轉(zhuǎn)義符\表示值還沒結(jié)束,這樣就沒問題了
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="${shiro.loginUrl}"/>
<property name="unauthorizedUrl" value="${shiro.unauthorizedUrl}"/>
<property name="successUrl" value="${shiro.successUrl}"/>
<property name="filterChainDefinitions">
<value>${shiro.filterChainDefinitions}</value>
</property>
</bean>
處理properties文件中key包含空格和等號的情況
在properties文件中都是以key=value的方式存儲的,在java代碼中用java.util.Properties的load方法,存儲在一個map中,當key中有空格和等號的時候,要用\(斜杠)進行轉(zhuǎn)義,而用xml的話,就沒有轉(zhuǎn)義這么麻煩了,所以推薦使用xml了。
處理方案
Spike.java
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
public class Spike {
public static void main(String[] args) throws Exception {
readProperties();
System.out.println("==================================================");
readXml();
}
private static void readProperties() throws IOException {
Properties props = new Properties();
InputStream inStream = Spike.class.getResourceAsStream("Mock.properties");
props.load(inStream);
Enumeration enums = props.propertyNames();
while (enums.hasMoreElements()) {
String key = (String) enums.nextElement();
System.out.println("Property--->>>>[" + key + "] " + "Value--->>>>" + props.getProperty(key));
}
}
private static void readXml() throws IOException {
Properties props = new Properties();
InputStream inStream = Spike.class.getResourceAsStream("Mock.xml");
props.loadFromXML(inStream);
Enumeration enums = props.propertyNames();
while (enums.hasMoreElements()) {
String key = (String) enums.nextElement();
System.out.println("Property--->>>>[" + key + "] " + "Value--->>>>" + props.getProperty(key));
}
}
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring注解@RestControllerAdvice原理解析
這篇文章主要介紹了Spring注解@RestControllerAdvice原理解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下2019-11-11
簡單了解java中靜態(tài)初始化塊的執(zhí)行順序
這篇文章主要介紹了簡單了解java中靜態(tài)初始化塊的執(zhí)行順序,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下2019-10-10
SpringBoot獲取客戶端的IP地址的實現(xiàn)示例
在Web應(yīng)用程序中,獲取客戶端的IP地址是一項非常常見的需求,本文主要介紹了SpringBoot獲取客戶端的IP地址的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2023-09-09

