【问题标题】:Auto-redirect root path to Spring Boot Context Path将根路径自动重定向到 Spring Boot 上下文路径
【发布时间】:2019-10-04 22:21:51
【问题描述】:

我正在使用 application.properties 文件中指定的 Spring Boot 上下文路径,效果很好

server.port=5000
server.context-path=/services

Spring Boot 2.0 及以上版本

server.port=5000
server.servlet.context-path=/services

但是我们如何实现根目录的默认重定向,即“/”到“/services”

http://localhost:5000/services - 效果很好!

但我希望 http://localhost:5000/ 自动重定向到 -> http://localhost:5000/services 以便最终用户能够访问域的根目录并自动重定向到上下文路径

当前访问根会抛出 404(这对默认配置有意义)

如何实现根目录的自动重定向,即“/”到上下文路径?

【问题讨论】:

  • 但是你为什么要那样做。如果您想直接打开上下文,只需删除上下文即可。
  • 我们想为所有其他端点使用上下文,但只需要根自动重定向

标签: spring spring-boot


【解决方案1】:

我想我可以提供一个解决方案。请参考以下代码。

@Configuration
public class RootServletConfig {

    @Bean
    public TomcatServletWebServerFactory servletWebServerFactory() {
        return new TomcatServletWebServerFactory() {

            @Override
            protected void prepareContext(Host host, ServletContextInitializer[] initializers) {
                super.prepareContext(host, initializers);
                StandardContext child = new StandardContext();
                child.addLifecycleListener(new Tomcat.FixContextListener());
                child.setPath("");
                ServletContainerInitializer initializer = getServletContextInitializer(getContextPath());
                child.addServletContainerInitializer(initializer, Collections.emptySet());
                child.setCrossContext(true);
                host.addChild(child);
            }
        };
    }

    private ServletContainerInitializer getServletContextInitializer(String contextPath) {
        return (c, context) -> {
            Servlet servlet = new HttpServlet() {
                @Override
                protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                    resp.sendRedirect(contextPath);
                }
            };
            context.addServlet("root", servlet).addMapping("/*");
        };
    }
}

【讨论】:

【解决方案2】:

看来你不能简单地做到这一点。设置server.servlet.context-path=/services 会将服务器的根路径设置为/services。当您使用/ 重定向路径时 - 实际上,您正在使用路径/services 进行重定向。 这是我试图解决这个问题的方法:

  • 如果您的应用程序前面有一个中间 Web 服务器(apache、nginx) - 您可以在其设置中进行重定向。
  • 或者您可以将server.servlet.context-path 设置替换为您自己的路径名,如下所示:
    application.config 中的app.endpoints.services_path=/services
    控制器 中的@RequestMapping("${app.endpoints.services_path}") 映射。
    然后从路径/ 重定向到/services

例如,通过以下方式之一:

  • 带有重定向控制器
@Controller
public class RootRedirectController {
    @GetMapping(value = "/")
    public void redirectToServices(HttpServletResponse httpServletResponse){
        httpServletResponse.setHeader("Location", "/services");
        httpServletResponse.setStatus(302);
    }
}
  • 或者通过向 Spring Boot 添加自定义
@Configuration
public class WebAppConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/services");
        // This method can be blocked by the browser!
        // registry.addRedirectViewController("/", "redirect:/services");
    }
}

希望这会有所帮助。对不起,我的谷歌翻译。

【讨论】:

    猜你喜欢
    • 2021-02-03
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    • 2014-10-29
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    相关资源
    最近更新 更多