Home > Spring > Spring Boot > Create a custom favicon in Spring Boot application

Create a custom favicon in Spring Boot application

In this article, we will learn how to configure custom favicon in the spring boot application. Favicon is the iconic representation of any website. It is placed in the address bar of any web browser. Spring boot provides option to create custom favicon for your website.

First create a favicon for your website, place it in the resources directory of your spring boot project inside a folder static. Path of the same is specified below.

<Spring-Boot-Project-Directory>src/main/resources/static/favicon.ico

Now in your WebMvc Configurer class place the below code to configure the custom resource.

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(
                "/favicon.ico")
                .addResourceLocations(
                        "classpath:/static/");
    }
}

Now in your WebSecurityConfigurer class, permit the favicon URL, as mention below

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

Now hit the favicon URL – http(s)://<Your-Domain>/favicon.ico