George Powell
George Powell

Reputation: 9509

How to configure tomcat to use different ports for different endpoints in a single application?

I have a tomcat .war application that has a number of endpoints. Let's say they are:

http://myapp.com/myapp/endpoint.a
http://myapp.com/myapp/endpoint.b

I want http://myapp.com/myapp/endpoint.a to be available over port 80, and http://myapp.com:8080/myapp/endpoint.b to only be available over port 8080.

I can't have apache in front of tomcat, and it is unacceptable for /myapp/endpoint.b to be accessible on the same port as /myapp/endpoint.a.

Splitting the endpoints into different application contexts is also not possible.

So far:

Upvotes: 4

Views: 1719

Answers (1)

George Powell
George Powell

Reputation: 9509

It is solvable at the application level with a combination of:

  • A custom PortAuthorisationFilter configured in web.xml with a filter-mapping of the endpoints we want to restrict:

<url-pattern>*.b</url-pattern>

  • Inside the filter we check ServletRequest.getLocalPort() is equal to :8080 and reject the request otherwise. This method claims to be the port used in the TCP connection so cannot be spoofed.

At the tomcat level, we have http connectors at ports 80 and 8080, that apply to the entire application.

Upvotes: 3

Related Questions