Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot: Overriding favicon

People also ask

How to disable favicon in Spring Boot?

If we don't want any favicon for our application, we can disable it by setting the property spring. mvc. favicon. enabled to false.

What is favicon ico file?

The favicon. ico file is a small graphic icon that is used by some browsers (including Microsoft Internet Explorer and Mozilla Firefox) to enhance the display of address bar information and "favorites" bookmark lists. When requesting a resource, these browsers also try to locate the site's custom favicon.

What size should a favicon be?

Favicon images are small in size, only 16 pixels in height by 16 pixels in width, so there is not much space for complex designs. Still, a good favicon that is clean, simple and easily identifiable can provide a good visual indicator for visitors navigating to your site through their tabs or bookmarks.


None of this was necessary for me.

Why override the default when you can bundle a resource with the generated JAR that will take higher precedence than the default one.

To achieve a custom favicon.ico file, I created a src/main/resources directory for my application and then copied the favicon.ico file into there. The files in this resources directory are moved to the root of the compiled JAR and therefore your custom favicon.ico is found before the Spring provided one.

Doing the above achieved the same effect as your updated solution above.

Note that since v1.2.0 you can also put the file in src/main/resources/static.


You can just put your own favicon.ico in the root of the classpath or in any of the static resource locations (e.g. classpath:/static). You can also disable favicon resolution completely with a single flag spring.mvc.favicon.enabled=false.

Or to take complete control you can add a HandlerMapping (just copy the one from Boot and give it a higher priority), e.g.

@Configuration
public static class FaviconConfiguration {

@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
    SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
    mapping.setOrder(Integer.MIN_VALUE);
    mapping.setUrlMap(Collections.singletonMap("mylocation/favicon.ico",
            faviconRequestHandler()));
    return mapping;
}

@Bean
protected ResourceHttpRequestHandler faviconRequestHandler() {
    ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
    requestHandler.setLocations(Arrays
            .<Resource> asList(new ClassPathResource("/")));
    return requestHandler;
}
}

I really like Springboot because overall it is full of smart solutions but I refuse to register an application bean to provide a favicon because that is just plain stupid.

I just added my own favicon link in my html page head like so.

<link rel="icon" type="image/png" href="images/fav.png">

Then I renamed my favicon and placed it at

<ProjFolder>/src/main/resources/static/images/fav.png

Now I have an icon in Chrome and Firefox browser tabs and the Safari address bar without having to use Spring and Java, and I should not have to chase changes to Springboot in newer versions for such trivial functionality.


Since Spring Boot 1.2.2 and 1.1.11 you can easily disable default favicon using spring.mvc.favicon.enabled = false property.

For more informations visit:

  • https://spring.io/blog/2015/02/27/spring-boot-1-2-2-released
  • https://spring.io/blog/2015/02/27/spring-boot-1-1-11-released

UPDATE: there is no longer spring.mvc.favicon.enabled property since Spring Boot 2.2.x

  • https://github.com/spring-projects/spring-boot/issues/17925#issuecomment-523512581

Newer versions of Boot (1.2 for sure but maybe also 1.1.8) allow you to just put a favicon.ico in your static resources.


I tried a lot of options/variants, but only this worked. (Spring Boot 2.2.7)

Favicon.ico files & robots.txt put in /resources/static

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
  protected void configure(HttpSecurity http) throws Exception { 
    http
      .authorizeRequests()
        .antMatchers("/favicon.ico").permitAll()
        .antMatchers("/robots.txt").permitAll()
        .antMatchers("/").permitAll()
        .anyRequest().authenticated();
    }
}

and

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
@Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler(
      "/favicon.ico",
      "/robots.txt")
      .addResourceLocations(
        "classpath:/static/");
  }
}

If it helps, please like it ;)