【已解决】启动项目后提示缺少:A component required a bean of type 'org.springframework.web.filter.CorsFilter' that could not be found.
背景描述:Springboot项目在未配置使用SpringSecurity前可以正常启动,但是使用SpringSecurity之后启动就报错了。产生问题原因分析及解决方案:根据错误提示,应用缺少CorsFilter的Bean定义,以下是解决方案: 1.添加全局CORS配置类创建一个配置类以显式定义CorsFilter的Bean:@Configurationpublic clas

背景描述:
Spring boot项目在未配置使用Spring Security前可以正常启动,但是使用Spring Security之后启动就报错了。
产生问题原因分析及解决方案:
根据错误提示,应用缺少CorsFilter的Bean定义,以下是解决方案:
1. 添加全局CORS配置类
创建一个配置类以显式定义CorsFilter的Bean:
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // 允许所有接口跨域
.allowedOrigins("*") // 允许所有来源
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的方法
.allowedHeaders("*") // 允许所有请求头
.exposedHeaders("Authorization"); // 暴露给前端的头部
}
}2. 若使用Spring Security,需额外配置
如果项目中引入了Spring Security,默认的CORS配置会被覆盖
原创不易,完成人机校验,阅读全文
相关推荐
学习笔录-SPRING BOOTspring boot 启动的时候required a bean of type 'XXX' that could not be经验分享spring boot 启动的时候required a bean of type 'XXX' not be (二)SPRING CLOUD【以解决】项目使用feign时候提示bean不能注入feign经验分享SpringMVC 访问controller层出错:No converter found for return value of type: class java.util.HashMap学习笔录-SPRING BOOTspring boot template might not exist or might not be accessible by any of the configured Template Resolvers错误工作小总结【已解决】从git上面下载了一个项目,启动时候一直提示连接不上数据库:Could not create connection to database server ,怎么解决?经验分享【已解决】项目启动,报错:java.sql :Public Key Retrieval is not allowed工作小总结【已解决】Spring Boot多数据源的时候,mybatis报错提示:Invalid bound statement (not found)