【问题标题】:How can i intercept requests for other applications with servlet filters?如何使用 servlet 过滤器拦截对其他应用程序的请求?
【发布时间】:2019-12-21 19:32:08
【问题描述】:

我想设计一个应用程序来记录在同一 apache 服务器上运行的另一个应用程序的请求和响应。我将用java编写这个程序,并将使用spring boot。我知道使用 spring,您可以轻松编写 servlet 过滤器和侦听器,它们可以记录或预处理针对当前程序的传入请求。我想要的是记录可能在不同端口上接收请求的其他程序的请求。有没有办法用弹簧靴做到这一点?

我发现的只是这个帖子Intercept another web application requests,但它没有答案。

谁能指出我正确的方向?

谢谢。

【问题讨论】:

    标签: java spring spring-boot servlet-filters


    【解决方案1】:

    您将需要使用spring-cloud-starter-netflix-zuul 之类的东西来创建一个反向代理,该代理能够拦截所有请求和响应,同时透明地将它们转发到正确的服务。 您的 Apache 必须配置为将调用定向到此类反向代理应用程序,并且您的目标应用程序(取决于它的编写方式)可能需要一些小的调整。

    您的pom.xml 文件(如果您使用 Maven)需要包含以下内容:

        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-contract-stub-runner</artifactId>
                <exclusions>
                    <exclusion>
                        <artifactId>spring-boot-starter-web</artifactId>
                        <groupId>org.springframework.boot</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    

    您的application.yml 文件需要包含以下内容:

    server:
      port: 80
    
    zuul:
      addHostHeader: true
      sensitiveHeaders:
      ignoredServices: '*'
      routes:
        mysvc:
          path: /mysvc/**
          serviceId: mysvc-svc
          stripPrefix: false
        uisvc:
          path: /uisvc/**
          serviceId: uisvc-frontend-svc
          stripPrefix: true
    
    
    hystrix:
      command:
        mysvc-svc:
          execution:
            isolation:
              thread:
                timeoutInMilliseconds: 120000
        uisvc-frontend-svc:
          execution:
            isolation:
              thread:
                timeoutInMilliseconds: 30000
    
    mysvc-svc:
      ribbon:
        NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
        listOfServers: localhost:8080 # comma-separated list
        ConnectTimeout: 5000
        ReadTimeout: 120000
        MaxTotalHttpConnections: 500
        MaxConnectionsPerHost: 100
    
    uisvc-frontend-svc:
      ribbon:
        NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
        listOfServers: localhost:4200 # comma-separated list
        ConnectTimeout: 1000
        ReadTimeout: 10000
        MaxTotalHttpConnections: 500
        MaxConnectionsPerHost: 100
    
    

    您的Application.java 主类需要或多或少如下所示:

    @SpringBootApplication
    @EnableZuulProxy
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    此时,您应该能够将逻辑连接到全新的网关应用程序中,并使用RequestContext 类拦截请求,大致遵循以下网址中的说明: https://github.com/Netflix/zuul/issues/264

    【讨论】:

    • 我无法对目标应用程序进行任何调整,我需要做的就是拦截请求和响应。无论如何谢谢这个建议。我会调查一下。谢谢!当我确定这是我正在寻找的东西时,我会接受它作为答案
    猜你喜欢
    • 2012-02-29
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 2020-05-21
    • 2021-12-25
    • 2013-01-31
    • 1970-01-01
    • 2017-07-23
    相关资源
    最近更新 更多