在使用SpringBoot+SpringSecurity配置用户登录时,需要配置CorsFilter,如下:

然后在项目启动的时候报一下错误:

10:24:01.010 [restartedMain] WARN  o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - [refresh,591] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfig': Unsatisfied dependency expressed through field 'corsFilter'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.web.filter.CorsFilter' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
10:24:01.123 [restartedMain] INFO  o.a.c.c.StandardService - [log,173] - Stopping service [Tomcat]
10:24:01.234 [restartedMain] ERROR o.s.b.d.LoggingFailureAnalysisReporter - [report,40] - ***************************
APPLICATION FAILED TO START
***************************Description:Field corsFilter in com.wyj.yjwu.config.SecurityConfig required a bean of type 'org.springframework.web.filter.CorsFilter' that could not be found.The injection point has the following annotations:- @org.springframework.beans.factory.annotation.Autowired(required=true)Action:Consider defining a bean of type 'org.springframework.web.filter.CorsFilter' in your configuration.

该问题是因为Spring容器中没有找到CorsFilter这个Bean,需要手动配置,如下:

@Beanpublic CorsFilter corsFilter() {final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();final CorsConfiguration corsConfiguration = new CorsConfiguration();List<String> list = new ArrayList<>();list.add("*");corsConfiguration.setAllowCredentials(true);corsConfiguration.setAllowedOrigins(list);corsConfiguration.setExposedHeaders(list);corsConfiguration.setAllowedMethods(list);urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);return new CorsFilter(urlBasedCorsConfigurationSource);}

然后启动项目不再报错。

该问题应该算是一类型题,如果是没有找到对应的Bean的话,都会报这个错,所以只需要对其进行相应的配置就可以了。

SpringBoot配置CorsFilter相关推荐

  1. SpringBoot配置跨域 修改默认json序列化

    SpringBoot配置跨域 修改默认json序列化 修改跨域 addCorsMappings(CorsRegistry registry)方法添加跨域设置的,无法生效,原因是请求经过的先后顺序问题, ...

  2. SpringBoot 配置CORS

    前言:CORS相关介绍可以查看CORS ---- 跨域解决方案 ,这里记录下SpringBoot如何使用CORS解决跨域资源共享问题.以下使用的SpringBoot版本是 2.0.5.RELEASE. ...

  3. springboot配置Redis哨兵主从服务 以及 Redis 集群

    redis哨兵集群配置 Sentinel(哨兵)是Redis 的高可用性解决方案:由一个或多个Sentinel 实例 组成的Sentinel 系统可以监视任意多个主服务器,以及这些主服务器属下的所有从 ...

  4. SpringBoot配置属性之NOSQL

    SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之N ...

  5. springboot配置Druid数据源

    springboot配置druid数据源 Author:SimpleWu springboot整合篇 前言 对于数据访问层,无论是Sql还是NoSql,SpringBoot默认采用整合SpringDa ...

  6. aop springboot 传入参数_java相关:springboot配置aop切面日志打印过程解析

    java相关:springboot配置aop切面日志打印过程解析 发布于 2020-3-31| 复制链接 摘记: 这篇文章主要介绍了springboot配置aop切面日志打印过程解析,文中通过示例代码 ...

  7. SpringBoot配置属性之Server

    SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之N ...

  8. 使用SpringBoot配置了 server.servlet.path后无效的解决方案

    使用SpringBoot配置了 server.servlet.path后无效的解决方案 参考文章: (1)使用SpringBoot配置了 server.servlet.path后无效的解决方案 (2) ...

  9. SpringBoot配置属性之MQ

    SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之N ...

最新文章

  1. MYSQL 取随机记录的方法
  2. mysql 日志还原数据库_通过Mysql-bin日志恢复还原数据
  3. 20周年献礼:Visual Studio 2017正式版3月7日发布
  4. 微信小程序销毁某一注册函数_微信小程序云开发API 删除一条记录
  5. 在Amazon FreeRTOS V10中使用运行时统计信息
  6. 关于table的那些用法(一)
  7. python str 换行_一步一步学Python3(小学生也适用) 第八篇: 字符串(str)类型(上)...
  8. 回顾经典: AlexNet, CaffeNet — Winner of ILSVRC 2012
  9. GUI 图形用户界面编程(一)
  10. CSV保存身份证后再打开后4位0000的解决办法
  11. HZNU2012图解
  12. unity实现AI对战坦克
  13. xp计算机如何查看内存大小,怎样查看电脑内存大小_xp怎么看电脑内存大小
  14. google 天气预报API
  15. android 三星闪退,三星手机升级安卓10微信闪退怎么办?(附解决方法)
  16. RYU+Mininet的SDN架构-设计校园网络(三)
  17. github国内网访问方式
  18. 注意力经济是什么?NFT 在其中发挥怎样的作用?
  19. 中国社科院与美国杜兰大学金融管理硕士项目——迎接立夏,切莫忘记自我成长
  20. opencv的多图拼接

热门文章

  1. win10 mysql5安装图解_mysql5.7.19 安装配置方法图文教程(win10)
  2. 求你啦!配电室管理不要不会用工具
  3. android 热修复 框架,十分钟教会你使用安卓热修复框架AndFix
  4. 【C语言】函数指针(指向函数的指针)
  5. 用阳光代替WiFi信号连网,沙特科学家这成果亮了
  6. 小程序navigator 标签open-type属性 有值
  7. 一款备份驱动后可用于PE的工具
  8. 【词性的选择与所放位置练习题】主语的选择-动词
  9. 计算机等级考试程序题怎么保存,计算机等级考试题库.docx
  10. 2021 年江西省职业院校技能大赛网络搭建与应用技能竞赛方案(中职组)