简体   繁体   中英

Spring Boot Embedded Tomcat 'allowLinking' property

I have been looking for a way to expose the property 'allowLinking' to allow an TomcatEmbeddedServletContainerFactory to follow symlinks to resources that are under its documentRoot.

According to the Tomcat 8 Migration docs this functionality switched to the 'Resources' tag for Tomcat 8+ : Tomcat 8 Migration Guide

<!-- Tomcat 7: -->
<Context allowLinking="true" />

<!-- Tomcat 8: -->
<Context>
  <Resources allowLinking="true" />
</Context>

How would one expose this property while configuring the TomcatEmbeddedServletContainerFactory programmatically for a Spring Boot application?

I had exactly the same issue, and was able to implement the following to resolve this:

@Bean
public EmbeddedServletContainerFactory servletContainer() 
{
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
    // allow symbolic links under the filesystem context
    // don't use this on Windows!
    if (IOCase.SYSTEM.isCaseSensitive())
    {
        Log.info("Enabling support for symbolic links on the webserver.");
        for (TomcatContextCustomizer customizer : tomcat.getTomcatContextCustomizers())
        {
            StandardContext context = new StandardContext();
            context.setAllowLinking(true);
            customizer.customize(context);
        }
    }
    return tomcat;
}

thks Erik Brandsberg, Modify the code, in Tomcat 8 to solve this problem

@Bean
public EmbeddedServletContainerFactory servletContainer()
{
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
    // allow symbolic links under the filesystem context
    // don't use this on Windows!
    if (IOCase.SYSTEM.isCaseSensitive())
    {
        TomcatContextCustomizer customizer = new TomcatContextCustomizer() {
            @Override
            public void customize(Context context) {
                StandardRoot r = new StandardRoot();
                r.setAllowLinking(true);
                context.setResources(r);
            }
        };
        tomcat.addContextCustomizers(customizer);

    }
    return tomcat;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM