本文介绍了spring-boot + tomcat RewriteValve的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 RewriteValve 以某种方式自定义嵌入式 tomcat?正如我在 api 目前只有 addContextValvesaddEngineValves 但正如文档中指出的 tomcat,RewriteValve 应该放在 Host 或 webapp 的 context.xml 中.我不明白 addContextValves 是否适用于此.

Is it possible to somehow customize embedded tomcat with RewriteValve? As I can see in api there are currently only methods for addContextValves and addEngineValves but as pointed tomcat in documentation, RewriteValve should be placed in Host or in a webapp's context.xml. I don't understand if addContextValves could work for this.

谢谢

推荐答案

是的,这是可能的.您需要遵循以下两个步骤:

Yes, it is possible. These are two steps you need to follow:

  1. 实现您自己的 TomcatEmbeddedServletContainerFactory bean 并设置 RewriteValve

  1. Implement your own TomcatEmbeddedServletContainerFactory bean and set up the RewriteValve

  import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
  ...
  import org.apache.catalina.valves.rewrite.RewriteValve;
  ...

  @Bean TomcatEmbeddedServletContainerFactory servletContainerFactory() {
    TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
    factory.setPort(8080);
    factory.addContextValves(new RewriteValve());
    return factory;
  }

  • 在你的应用程序的WEB-INF目录下添加一个rewrite.conf文件并指定重写规则.这是一个示例 rewrite.conf 内容,我在 angular 应用程序中使用它来利用 angular 的 PathLocationStrategy(基本上我将所有内容重定向到 index.html,因为我们只使用 spring boot 来提供静态 Web 内容):

  • Add a rewrite.conf file to the WEB-INF directory of your application and specify the rewrite rules. Here is an example rewrite.conf content, which I'm using in the angular application to take advantage of the angular's PathLocationStrategy (basicly I redirect everything to the index.html as we just use spring boot to serve the static web content):

      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]
    

  • 这篇关于spring-boot + tomcat RewriteValve的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    05-27 03:36