FAM_Maurice
FAM_Maurice

Reputation: 423

How can i intercept requests for other applications with servlet filters?

I want to design an application that logs both the request and the response of another application running on the same apache server. I will write this program in java and will be using spring boot. I know that with spring you can easily write servlet filters and listeners that can log or preprocess incoming requests intended for the current program. What i want though is log the requests for other programs that may receive requests on different ports. Is there a way to do this with spring boot?

All i found was this thread Intercept another web application requests but it has no answers.

Can anyone point me in the right direction?

Thank you.

Upvotes: 0

Views: 294

Answers (1)

Filippo Possenti
Filippo Possenti

Reputation: 1410

You will need to use something like spring-cloud-starter-netflix-zuul to create a reverse-proxy capable of intercepting all requests and responses while transparently forwarding them to the correct service. Your Apache will have to be configured to direct the calls to such reverse proxy application and your target application (depending on how it's been written) may need some minor tweaks.

Your pom.xml file (if you are using Maven) will need to include the following:

    <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>

Your application.yml file will need to contains something like the following:

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

Your Application.java main class will need to look more or less as follows:

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

At this point you should be able to wire logic into your brand new gateway application and intercept the requests using the RequestContext class, by broadly following the instructions at the following url: https://github.com/Netflix/zuul/issues/264

Upvotes: 1

Related Questions