Monday, January 26, 2015

How to add a RemoteAddrValve or RemoteAddrFilter in Spring Boot

To add a RemoteAddrValve in Spring Boot,

you can do the following:

@Component
public class TomcatRemoteAddrValveCustomizer implements EmbeddedServletContainerCustomizer {

@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
TomcatEmbeddedServletContainerFactory tomcatContainer =
(TomcatEmbeddedServletContainerFactory) container;
RemoteAddrValve remoteAddrValve = new RemoteAddrValve();
// remoteAddrValve.setAllow("127.0.0.1");
remoteAddrValve.setAllow("0:0:0:0:0:0:0:1");
tomcatContainer.addContextValves(remoteAddrValve);
}

}

To add a RemoteAddrFilter in Spring Boot,

you can do the following:

@Bean
public FilterRegistrationBean remoteAddressFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
RemoteAddrFilter filter = new RemoteAddrFilter();
// filter.setAllow("127.0.0.1");
filter.setAllow("0:0:0:0:0:0:0:1");
filterRegistrationBean.setFilter(filter);
filterRegistrationBean.addUrlPatterns("/gs/serving-web-content/testParameters");
return filterRegistrationBean;
}

References:
http://tomcat.apache.org/tomcat-8.0-doc/config/valve.html#Remote_Address_Filter
http://stackoverflow.com/questions/10498237/how-can-i-restrict-access-to-certain-urls-by-source-ip-in-tomcat
http://tomcat.apache.org/tomcat-8.0-doc/config/filter.html#Remote_Address_Filter
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-customizing-embedded-containers
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-embedded-container-servlets-and-filters

No comments:

Post a Comment