简体   繁体   English

当 management.port 与服务器端口不同时,如何调用 OncePerRequestFilter ?

[英]How make OncePerRequestFilter called when management.port is different from server port?

I have a filter that extends OncePerRequestFilter .我有一个扩展OncePerRequestFilter的过滤器。 When I the management.port=8081 and the server.port=8080 (or any differing ports), my filter is not called on any 8081 Urls.当我使用management.port=8081server.port=8080 (或任何不同的端口)时,不会在任何 8081 Urls 上调用我的过滤器。

The filter is only called on 8080 Urls.过滤器在 8080 Urls 上调用。

How do I make it called on all Urls, including those on 8081?我如何让它在所有 URL 上调用,包括 8081 上的 URL?

Filter:筛选:

@Order( Ordered.LOWEST_PRECEDENCE )
public class TestFilter extends OncePerRequestFilter
{
    public TestFilter()
    {
        System.out.println( "Started" );
    }

    @Override
    protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException
    {
        System.out.println( "Checked should not filter" );
        return false;
    }

    @Override
    public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException
    {
        System.out.println( "Filtering" );

        // continue the processing
        filterChain.doFilter( request, response );
    }
}

I add it by:我通过以下方式添加它:

@Configuration
public class MyConfig
{
    @Bean
    public TestFilter testFilter()
    {
        return new TestFilter()
    }
}

EDIT: I tried adding @ManagementContextConfiguration to my config class, but this didn't work either.编辑:我尝试将@ManagementContextConfiguration添加到我的配置类,但这也不起作用。

Although I was unable to find documentation, it appears the answer is to do all of the following:虽然我找不到文档,但似乎答案是执行以下所有操作

  1. Add a class that's annotated with @ManagementContextConfiguration添加一个用@ManagementContextConfiguration注释的类
  2. Put that configuration file outside the component scan (so spring boot's normal auto-config won't find it)将该配置文件放在组件扫描之外(因此 spring boot 的正常自动配置不会找到它)
  3. Declare it in META-INF/spring.factories:在 META-INF/spring.factories 中声明:

META-INF/spring.factories: META-INF/spring.factories:

before spring-boot-2.0.0.RELEASE:在 spring-boot-2.0.0.RELEASE 之前:
org.springframework.boot.actuate.autoconfigure.ManagementContextConfiguration=com.packageoutsidescan.MyManagementFilterConfigurationClass

after spring-boot-2.0.0.RELEASE ( web subpackage):在 spring-boot-2.0.0.RELEASE ( web子包) 之后:

org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration=com.packageoutsidescan.MyManagementFilterConfigurationClass

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 当使用“management.server.port”属性而不是弃用的“management.port”时,启动 MockMvc 测试为执行器端点提供 404 - Spring Boot MockMvc test giving 404 for actuator endpoint when using the "management.server.port" property instead of the deprecated "management.port" 如何验证SQL Server Management Studio上的端口号? - How do I verify the port numbers on SQL Server Management Studio? 如何与其他端口绑定 - How to bind with a different port 本地主机时如何重定向到本地存储的 index.html 文件:<port> 从浏览器调用</port> - How to redirect to locally stored index.html file when localhost:<port> is called from a browser 服务器如何针对相同和不同类型的进程从相同的端口号答复更多数量的客户端? - How server replies to more number of clients from the same port number for same and different type of process? 使用带有临时端口的 JMX 服务器时,如何获取服务器端口号? - When using a JMX server with ephemeral port, how to get the server port number? 从应用程序服务器监听端口 - listenin a port from application server TCP:当服务器在随机端口上侦听时,客户端如何知道要发送到哪个端口? - TCP: When server listening on random port how does client know which port to send to? 如何在不同的端口上运行@RestController? - How to run @RestController on a different port? iText和iTextSharp端口有何不同? - How different are iText and the iTextSharp port?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM