使用Spring Boot并希望通过html文件加载字体并获取我不知道如何修复的CORS错误 .
出现以下错误:因此不允许访问“https://example.com/fonts/webfont.woff2?v=4.6.3 ' from origin ' http://localhost:8083 ' has been blocked by CORS policy: No ' Access-Control-Allow-Origin ' header is present on the requested resource. Origin ' http://localhost:8083”中的字体 .
的index.html
...
<style> @font-face {
font-family: 'Webfont';
src: url("https://example.com/fonts/webfont.woff2");
}
</style>
...
Spring 季启动
DemoApplication.java
@SpringBootApplication公共类DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping(".(eot|otf|svg|ttf|woff2?)$").allowedOrigins("*");
registry.addMapping("/").allowedOrigins("*");
registry.addMapping("/**").allowedOrigins("*");
}
};
}
}