当 management.port 与服务器端口不同时,如何调用 OncePerRequestFilter ? [英] How make OncePerRequestFilter called when management.port is different from server port?

查看:214
本文介绍了当 management.port 与服务器端口不同时,如何调用 OncePerRequestFilter ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展 OncePerRequestFilter 的过滤器.当我使用 management.port=8081server.port=8080(或任何不同的端口)时,不会在任何 8081 Urls 上调用我的过滤器.

I have a filter that extends 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.

过滤器在 8080 网址上调用.

The filter is only called on 8080 Urls.

如何在所有网址上调用它,包括 8081 上的网址?

How do I make it called on all Urls, including those on 8081?

过滤器:

@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 );
    }
}

我添加它:

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

我尝试将 @ManagementContextConfiguration 添加到我的配置类中,但这也不起作用.

I tried adding @ManagementContextConfiguration to my config class, but this didn't work either.

推荐答案

虽然我找不到文档,但似乎答案是执行以下所有:

Although I was unable to find documentation, it appears the answer is to do all of the following:

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

META-INF/spring.factories:

META-INF/spring.factories:

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

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

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

after spring-boot-2.0.0.RELEASE (web subpackage):

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

这篇关于当 management.port 与服务器端口不同时,如何调用 OncePerRequestFilter ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆