I am trying to make use of Tomcat's rewrite valve in my Spring Boot app however cannot determine where to put the rewrite.conf in order to be loaded successfully.
I am using Spring Boot 2.0.3.RELEASE with Tomcat 8.5.31 and packaging the application as a fat jar.
I have configured the rewrite valve like so:
@Bean
public TomcatServletWebServerFactory containerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.addContextValves(new RewriteValve());
return factory;
}
However, it appears to rely on a directory structure of /WEB-INF to load the rewrite.conf from, which being a fat jar, I don't currently have.
Has anybody found a solution to configure this without changing the application packaging structure to a WAR with a WEB-INF directory?
config file containing the rewrite directives, it must be placed in the Host configuration folder.
Tomcat 7 & 8.0 work with Spring Boot, but the default is to use Tomcat 8.5. If you cannot use Tomcat 8.5 (for example, because you are using Java 1.6) you will need to change your classpath to reference a different version.
Another way to change the port of embedded tomcat in the Spring Boot application is by specifying the server. port property in the resource file. For example, if you want your Spring boot application to listen on port 8080, then you can specify server. port=8080 on the application.
By default, Spring Boot provides an embedded Apache Tomcat build. By default, Spring Boot configures everything for you in a way that's most natural from development to production in today's platforms, as well as in the leading platforms-as-a-service.
With spring boot 2 and tomcat embedded.
First create rewrite.config
file under resources directory like this resources/rewrite.config.
This rule example for run routing react in my server side (react needs to redirect all routes to index.html)
RewriteCond %{REQUEST_URI} !^.*\.(bmp|css|gif|htc|html?|ico|jpe?g|js|pdf|png|swf|txt|xml|svg|eot|woff|woff2|ttf|map)$
RewriteRule ^(.*)$ /index.html [L]
then create your own custom class for configure tomcat server like this:
@Component
public class CustomContainer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
@Override
public void customize(TomcatServletWebServerFactory factory) {
final RewriteValve valve = new RewriteValve() {
@Override
protected synchronized void startInternal() throws LifecycleException {
super.startInternal();
try {
InputStream resource = new ClassPathResource("rewrite.config").getInputStream();
InputStreamReader resourceReader = new InputStreamReader(resource);
BufferedReader buffer = new BufferedReader(resourceReader);
parse(buffer);
} catch (IOException e) {
e.printStackTrace();
}
}
};
valve.setEnabled(true);
factory.addContextValves(valve);
}
}
This custom class overrides starInternal method to implement how to retrieve the config file for parsing it and add that valve in context valves.
This work fine for me :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With