- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一个使用 Spring-Security 的 Spring-Boot 应用程序。我有一个请求范围的 bean,我想将它 Autowiring 到安全过滤器链中的一个自定义过滤器中,但目前它不起作用。
我了解在 DispatcherServlet 之外使用请求范围的 bean 需要一些配置,并且已阅读此 http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/beans.html#beans-factory-scopes-other但是还没有成功:
For Servlet 3.0+, this can done programmatically via the WebApplicationInitializer interface.
(我使用的是最新的 Tomcat,servlet 3+ 也是)
我尝试过同时使用 RequestContextListener 和 RequestContextFilter(文档说它们和 DispatcherServlet 做的事情完全相同),但在这两种情况下我仍然会遇到错误,因为我的 Autowiring 对象为空:
我尝试注册过滤器
@Configuration
@ComponentScan
@EnableAutoConfiguration
class Application extends SpringBootServletInitializer {
@Override protected SpringApplicationBuilder configure( SpringApplicationBuilder application ) {
application.sources( Application )
}
@Override public void onStartup( ServletContext servletContext ) throws ServletException {
super.onStartup( servletContext )
servletContext.addFilter("requestContextFilter", new RequestContextFilter() ).addMappingForUrlPatterns(null, false, "/*")
}
我尝试注册监听器
@Configuration
@ComponentScan
@EnableAutoConfiguration
class Application extends SpringBootServletInitializer {
@Override protected SpringApplicationBuilder configure( SpringApplicationBuilder application ) {
application.sources( Application )
}
@Override public void onStartup( ServletContext servletContext ) throws ServletException {
super.onStartup( servletContext )
servletContext.addListener( new RequestContextListener() )
}
我是否遗漏了一些明显的东西?我已经查看了 Spring Boot 的自动配置源代码,但还没有遇到任何东西。
更新
我是个白痴,我在我的 SpringSecurity 配置中添加了我的过滤器,在 configure()
方法中:
http.addFilterBefore( new PreAuthFilter(), BasicAuthenticationFilter )
但尚未将新过滤器注册为 Bean。根据下面 M. Denium 的评论,我不需要所有额外的配置显式添加监听器/过滤器,只需注册 bean 就足够了。
最佳答案
如更新/评论中所述,这是我自己的愚蠢造成的。
Spring-Boot 能够将 Request/Session 范围的 bean Autowiring 到 DispatcherServlet
之外的过滤器中。根据 Spring 的文档,我们需要添加 RequestContextListener
或 RequestContextFilter
来启用这个功能:
To support the scoping of beans at the request, session, and global session levels (web-scoped beans), some minor initial configuration is required before you define your beans. (This initial setup is not required for the standard scopes, singleton and prototype.) ...
If you access scoped beans within Spring Web MVC, in effect, within a request that is processed by the Spring DispatcherServlet, or DispatcherPortlet, then no special setup is necessary: DispatcherServlet and DispatcherPortlet already expose all relevant state.
为了处理这个问题,我需要注册一个 RequestContextListener bean:
@Bean public RequestContextListener requestContextListener(){
return new RequestContextListener();
}
如果您不注册该 bean,您将收到一条错误消息,指出您正在尝试访问 DispatcherServlet 之外的 Request 范围。
我遇到的问题(只是没有注入(inject) Autowiring 的对象)是因为我只是将自定义过滤器注册为标准类实例,而不是 Spring 托管 bean:
http.addFilterBefore( new PreAuthFilter(), BasicAuthenticationFilter )
为了解决这个问题,我只是将 PreAuthFilter
的创建移到了一个单独的 @Bean
方法中,然后 @Autowired
功能就可以正常工作了.
关于spring - 在 SpringBoot 中配置 RequestContextListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30254079/
SpringBoot-Admin 服务监控 简单介绍 Spring Boot Actuator 是 Spring Boot 自带的一个功能模块, 提供了一组已经开箱即用的生产环境下常用
我想查找通过关键字匹配字段 nameEnglish 或 nameChinese 的模型列表。我花了一个多小时谷歌搜索但我做不到。请帮忙。 Springboot Mongo 入门示例 https://s
(请注意:在调查 this issue 时,我更好地发现了我在此处介绍的问题根源) 我对 Hibernate 和 SpringBoot 非常陌生。我的项目涉及一个搜索引擎,其中索引(javafx 客户
我最近有一个 Web 应用程序从 springboot 升级到 springboot 2。当我将其部署到 Tomcat 8 时,它似乎启动了,但没有完全启动。 在 localhost.2019-09-
我是 Spring boot 的新手...我在运行 Controller 时遇到问题, Description: Field todoService in com.springboot.todoCon
我有一个SpringBoot应用程序,它使用以下配置与PostgreSQL通信,通过AWS Beanstrik部署:。在我将AWS Aurora证书更新为rds-ca-ecc384-g1之前,一切都很
一、介绍 在实际的软件系统开发过程中,由于业务的需求,在代码层面实现数据的脱敏还是远远不够的,往往还需要在数据库层面针对某些关键性的敏感信息,例如:身份证号、银行卡号、手机号、工资等信息进行加密存储
Selenium Selenium是一个用于Web应用程序自动化测试的开源工具套件。它主要用于以下目的: 浏览器自动化:Selenium能够模拟真实用户在不同浏览器(如Chrome、Fire
一、简介 在实际的项目开发过程中,经常需要用到邮件通知功能。例如,通过邮箱注册,邮箱找回密码,邮箱推送报表等等,实际的应用场景非常的多。 早期的时候,为了能实现邮件的自动发送功能,通常会使用 Ja
SpringBoot:基于redis自定义注解实现后端接口防重复提交校验 一、添加依赖 org.springframework.boot spring
SpringBoot:使用Jackson完成全局序列化配置 一、测试准备 com.fasterxml.jackson.core jackson-cor
springboot:整合rocketmq 一、简易消息操作 生产者整合mq 导入依赖 org.springframework.boot
springboot:常用注解 一、spring常用注解 包扫描+组件标注注解 @Component:泛指各种组件 @Controller、@Service、@Repository都可以称为@Comp
我们经常需要在两个系统之间进行一些数据的交互,这时候我们就需要开发数据交互接口。 一般来说,遇到比较多的接口有HTTP接口、WebService接口、FTP文件传输。今天我要来学习一下在SpringB
背景 近期项目上线,甲方要求通过安全检测才能进行验收,故针对扫描结果对系统进行了一系列的安全加固,本文对一些常见的安全问题及防护策略进行介绍,提供对应的解决方案 跨站脚本攻击 XSS常发生于论坛评论等
1.排除 Spring-boot-starter 默认的日志配置 将原本的 spring-boot-starter 改为 org.springframework.boot
springboot:解决跨域问题 一、跨域简介 URL的组成: // 协议 + 域名(子域名 + 主域名) + 端口号 + 资源地址 http://www.baidu.com:8080/ 只要协
一、自定义Starter 的思路: 创建一个Maven工程,创建三个模块 一个模块为demo-app,一个模块为demo-module,一个模块为demo-module-springboot-star
1.pom.xml 4.0.0 org.springframework.boot spring-boot-starter-parent
1.pom.xml 4.0.0 org.springframework.boot spring-boot-starter-parent
我是一名优秀的程序员,十分优秀!