Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure a filter in Java

I am trying to follow instructions and configure a prerender filter for Spring Boot but since there is no web.xml in Boot I need to do this with Java configurations.

Instructions for Spring applications with web.xml that I am trying to follow:

1: Add this line to your web.xml:

<filter>
      <filter-name>prerender</filter-name>
      <filter-class>com.github.greengerong.PreRenderSEOFilter</filter-class>
      <init-param>
          <param-name>prerenderToken</param-name>
          <param-value>[get from prerender: https://prerender.io/]</param-value>
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>prerender</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>

2: add dependency on your project pom:

<dependency>
  <groupId>com.github.greengerong</groupId>
  <artifactId>prerender-java</artifactId>
  <version>1.6.4-SNAPSHOT</version>
</dependency>

What I did so far:

I added the dependency to the pom. I created a WebConfig class and instantiated the Bean:

@Configuration
public class WebConfig {
    @Bean
    public PreRenderSEOFilter PreRenderSEOFilter() {
        PreRenderSEOFilter prerenderFilter = new PreRenderSEOFilter();
        return prerenderFilter;
    }
}

But I don't know how to configure it. The prerenderFilter has a method init(FilterConfig filterConfig). But I'm not sure how to use it.

How do I configure this prerenderFilter?

like image 761
nilsi Avatar asked Sep 06 '26 13:09

nilsi


1 Answers

In your WebApplicationInitializer, You can use following code:

    com.github.greengerong.PreRenderSEOFilter seoFilter = new com.github.greengerong.PreRenderSEOFilter();
    FilterRegistration.Dynamic filter =  container.addFilter("prerender", seoFilter);
    filter.setInitParameter("prerenderToken", "<YOU TOKEN>");
    filter.addMappingForUrlPatterns(null , true, "/*");
like image 143
Aamir Faried Avatar answered Sep 08 '26 02:09

Aamir Faried



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!