Embed Kibana UI in Spring Boot 2
I have a Spring Boot 2 app that wants to embed Kibana UI in the page. I use an iframe to embed the UI. The iframe url is set to /kibana
. In the backend, I use HTTP-Proxy-Servlet to proxy the requests to the actual Kibana server.
Kibana version is 6.5.4
and Spring Boot version is 2.0.8.RELEASE
.
Include the HTTP-Proxy-Servlet
library first.
<dependency>
<groupId>org.mitre.dsmiley.httpproxy</groupId>
<artifactId>smiley-http-proxy-servlet</artifactId>
<version>1.11</version>
</dependency>
Then add proxy config to application.yml
.
proxy:
kibana:
servletUrl: /kibana/*
targetUrl: http://localhost:5601/kibana
Finally, create the ProxyServlet
.
@Configuration
class ProxyConfig : EnvironmentAware {
private lateinit var environment: Environment
override fun setEnvironment(environment: Environment) {
this.environment = environment
}
@Bean
fun kibanaProxyServletRegistrationBean() = createProxyServlet("proxy.kibana")
@Bean
fun kibanaFilterRegistrationBean(filter: HiddenHttpMethodFilter): FilterRegistrationBean<HiddenHttpMethodFilter> {
val registration = FilterRegistrationBean(filter)
registration.isEnabled = false
return registration
}
private fun createProxyServlet(propertyPath: String): ServletRegistrationBean<ProxyServlet> {
val servletRegistrationBean = ServletRegistrationBean(ProxyServlet(), environment.get("$propertyPath.servletUrl"))
servletRegistrationBean.addInitParameter("targetUri", environment.get("$propertyPath.targetUrl"))
servletRegistrationBean.addInitParameter(ProxyServlet.P_HANDLEREDIRECTS, "true")
servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "false")
return servletRegistrationBean
}
}
Now accessing http://localhost:8080/kibana shows the Kibana UI.
You need to update Kibana config kibana.yml
to set the base path. Check that you can access http://localhost:5601/kibana
. This url matches the targetUrl
value in application.yml
.
server.basePath: "/kibana"
server.rewriteBasePath: true