Limit tomcat in spring boot to specific IP addresses

I need to limit the built-in tomcat application with spring loading to specific IP addresses. I want to allow only incoming connections from two IP addresses and not all. I know how to do this in tomcat, which does not work inline, but does not know how to configure it when spring boots. Various properties server.tomcat.*do not seem to support this support. There is a property server.addressthat allows me to bind to a local IP address, but that is not what I need.

+4
source share
1 answer

Found this answer looking for the same solution. This is a more accurate way to do this in Spring Boot.

@Bean
public FilterRegistrationBean remoteAddressFilter() {

    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
    RemoteAddrFilter filter = new RemoteAddrFilter();

    filter.setAllow("192.168.0.2");
    filter.setDenyStatus(404);

    filterRegistrationBean.setFilter(filter);
    filterRegistrationBean.addUrlPatterns("/*");

    return filterRegistrationBean;

}

The default answer is 403. To change this value to 404, it is added filter.setDenyStatus(404);

You can also assign Deny addresses instead filter.setDeny("192\\.168\\.0\\.2");

RemoteAddressFilter Docs for Tomcat

+5
source

Source: https://habr.com/ru/post/1569710/


All Articles