昨晚,在做一個自己感興趣的東西時,發現瀏覽器報警告,Failed to decode downloaded font以及OTS parsing error: Failed to convert *** font to ***。
但是這個問題從來沒有遇到過,在網上找了一會兒答案后,發現了類似的問題回答下有網友猜測是攔截器沒通過,所以我檢查了shiro的攔截配置:
<property name="filterChainDefinitions">
<value>
/=authc
/index=authc
/logout=logout
/assets/** = anon
/css/** = anon
/fonts/**=anon
/img/**=anon
/**= user
</value>
</property>
但是這個配置理論上是沒問題的,所以不是這個問題。
今天在查閱了一些資料后,發現了可能的原因:
在maven的filter解析font文件時,它破壞了font文件的二進制文件格式,導致瀏覽器解析出錯。
解決的思路是:font文件不需要filter,因此我們需要在配置中設置一下(SpringBoot在pom.xml中寫入):
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<!-- fonts file cannot use filter as the data structure of byte file will be changed via filter -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>static/fonts/**</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>static/fonts/**</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
在經如上所示的配置后,maven在編譯項目時不解析font文件,瀏覽器就不會報警告,字體也可以正常顯示。
