且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在带有嵌入式tomcat的Spring Boot应用程序中运行hawt.io

更新时间:2023-09-06 12:34:04

我遇到了完全相同的问题-这就是我解决问题的方式.

I had exactly the same issue - and here is how I solved the problem.

我发现spring-boot不支持旧的web.xml配置,这是当maven-war-plugin在您自己的战争中进行hawtio-web项目的覆盖时得到的. 由此产生的战争包含您的Web代码以及hawtio-web存档的内容.

I found out that spring-boot doesnt support legacy web.xml configuration which is what you get when the maven-war-plugin does the overlay of the hawtio-web project on top of your own war. The resulting war contains both your web code as well as the content of the hawtio-web archive.

请参见 http://docs .spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html

因此,我在春季开始了配置servlet和过滤器的过程.

So I started the process of configuring the servlets and filters in spring.

首先将必要的依赖项添加到pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring-boot-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-actuator</artifactId>
        <version>${spring-boot-version}</version>
    </dependency>
    <dependency>
        <groupId>io.hawt</groupId>
        <artifactId>hawtio-springboot</artifactId>
        <version>${hawtio.version}</version>
    </dependency>
    <dependency>
        <groupId>io.hawt</groupId>
        <artifactId>hawtio-core</artifactId>
        <version>${hawtio.version}</version>
    </dependency>
</dependencies>

我正在使用以下版本:

<hawtio.version>2.0.0</hawtio.version>
<spring-boot.version>1.2.3.RELEASE</spring-boot.version>

添加配置类以配置servlet和过滤器:

@Configuration
public class HawtioConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/hawtio/plugins/**").addResourceLocations("/app/", "classpath:/static/hawtio/app/");
        registry.addResourceHandler("/hawtio/**").addResourceLocations("/", "/app/", "classpath:/static/hawtio/",
                "classpath:/static/hawtio/app/");
    }

    @Override
    public void addViewControllers(final ViewControllerRegistry registry) {
        registry.addViewController("/hawtio/plugin").setViewName("forward:/plugin");
        registry.addViewController("/hawtio/").setViewName("redirect:/hawtio/index.html");
    }

    @Bean
    public ServletRegistrationBean userServlet() {
        return new ServletRegistrationBean(new UserServlet(), "/user/*", "/hawtio/user/*");
    }

    @Bean
    public ServletRegistrationBean jolokiaproxy() {
        return new ServletRegistrationBean(new ProxyServlet(), "/hawtio/proxy/*");
    }

    @Bean
    public ServletRegistrationBean kubeservice() {
        return new ServletRegistrationBean(new ServiceServlet(), "/hawtio/service/*");
    }

    @Bean
    public ServletRegistrationBean kubepod() {
        return new ServletRegistrationBean(new PodServlet(), "/hawtio/pod/*");
    }

    @Bean
    public ServletRegistrationBean fileupload() {
        return new ServletRegistrationBean(new UploadServlet(), "/hawtio/file-upload/*");
    }

    @Bean
    public ServletRegistrationBean loginservlet() {
        return new ServletRegistrationBean(new LoginServlet(), "/hawtio/auth/login/*");
    }

    @Bean
    public ServletRegistrationBean logoutservlet() {
        return new ServletRegistrationBean(new LogoutServlet(), "/hawtio/auth/logout/*");
    }

    @Bean
    public ServletRegistrationBean keycloakservlet() {
        return new ServletRegistrationBean(new KeycloakServlet(), "/hawtio/keycloak/*");
    }

    @Bean
    public ServletRegistrationBean exportcontextservlet() {
        return new ServletRegistrationBean(new ExportContextServlet(), "/hawtio/exportContext/*");
    }

    @Bean
    public ServletRegistrationBean mavenSource() {
        return new ServletRegistrationBean(new JavaDocServlet(), "/hawtio/javadoc/*");
    }

    @Bean
    public ServletRegistrationBean contextFormatter() {
        return new ServletRegistrationBean(new ContextFormatterServlet(), "/hawtio/contextFormatter/*");
    }

    @Bean
    public ServletRegistrationBean gitServlet() {
        return new ServletRegistrationBean(new GitServlet(), "/hawtio/git/*");
    }

    @Bean
    public ServletListenerRegistrationBean hawtioContextListener() {
        return new ServletListenerRegistrationBean<>(new HawtioContextListener());
    }

    @Bean
    public ServletListenerRegistrationBean fileCleanerCleanup() {
        return new ServletListenerRegistrationBean<>(new FileCleanerCleanup());
    }

    @Bean
    public FilterRegistrationBean redirectFilter() {
        final FilterRegistrationBean filter = new FilterRegistrationBean();
        filter.setFilter(new RedirectFilter());
        filter.setUrlPatterns(Collections.singletonList("/hawtio/*"));
        return filter;
    }

    @Bean
    public FilterRegistrationBean sessionExpiryFilter() {
        final FilterRegistrationBean filter = new FilterRegistrationBean();
        filter.setFilter(new SessionExpiryFilter());
        filter.setUrlPatterns(Collections.singletonList("/hawtio/*"));
        return filter;
    }

    @Bean
    public FilterRegistrationBean cacheFilter() {
        final FilterRegistrationBean filter = new FilterRegistrationBean();
        filter.setFilter(new CacheHeadersFilter());
        filter.setUrlPatterns(Collections.singletonList("/hawtio/*"));
        return filter;
    }

    @Bean
    public FilterRegistrationBean CORSFilter() {
        final FilterRegistrationBean filter = new FilterRegistrationBean();
        filter.setFilter(new CORSFilter());
        filter.setUrlPatterns(Collections.singletonList("/hawtio/*"));
        return filter;
    }

    @Bean
    public FilterRegistrationBean XFrameOptionsFilter() {
        final FilterRegistrationBean filter = new FilterRegistrationBean();
        filter.setFilter(new XFrameOptionsFilter());
        filter.setUrlPatterns(Collections.singletonList("/hawtio/*"));
        return filter;
    }

    @Bean
    public FilterRegistrationBean AuthenticationFilter() {
        final FilterRegistrationBean filter = new FilterRegistrationBean();
        filter.setFilter(new AuthenticationFilter());
        filter.setUrlPatterns(Arrays.asList("/hawtio/auth/*", "/jolokia/*", "/hawtio/upload/*", "/hawtio/javadoc/*"));
        return filter;
    }

}

我已经用jetty和tomcat进行了测试-并且可以同时使用. 我也已将此文件作为hawtio的补丁提交,但尚未发布.您可以自己编译hawtio并导入HawtioConfiguration: https://github.com/hawtio/hawtio/blob/master/hawtio-springboot/src/main/java/io/hawt/springboot/HawtioConfiguration.java

I have tested this with both jetty and tomcat - and it works with both. I have also submitted this as a patch to hawtio, but it is not released as of yet. You could compile hawtio yourself and import the HawtioConfiguration: https://github.com/hawtio/hawtio/blob/master/hawtio-springboot/src/main/java/io/hawt/springboot/HawtioConfiguration.java

我还更新了hawtio-sample以使用HawtioConfiguration: https://github.com/hawtio/hawtio/tree/master/hawtio -sample-springboot

I have also updated the hawtio-sample to use the HawtioConfiguration: https://github.com/hawtio/hawtio/tree/master/hawtio-sample-springboot

现在,我可以通过访问 http://localhost:8080/hawtio/index.html 访问hawtio

Now i can access hawtio by visiting http://localhost:8080/hawtio/index.html

希望这会有所帮助.

祝你好运.