Spring Boot custom favicon.ico not showing
Asked Answered
B

12

23

I know this question has been asked over and over here and there are several solutions. I've tried several of those except the ones that suggests writing you own configuration bean for this. I don't want to do all that just to display a tiny icon it seams overkill. But I can not get it to work. These are the solutions I've tried so far.

  1. just add favicon.ico under static resources and it should work....it doesn't.
  2. spring.mvc.favicon.enabled=false in application.properties, no favicon showed at all (which I guess is the whole point of that).
  3. Tried 2 examples of including the favicon as a link in the html pages. Like so: <link rel="icon" type="image/png" href="favicon.png" /> <link rel="icon" type="image/x-icon" href="favicon.ico" />

Neither of those work.

  1. Tried renaming my own favicon to something else and reference it as above. Does not work.

When inspecting the page in the browser I sometimes get no error at all printed out despite no icon showing, or I get an error saying GET http://localhost:8080/myapp/favicon.png 404 () Where it is refering the type as JSON (which I find strange).

I'm running out of ideas here so if anyone can tell me why this is not working please let me know. Did I perhaps forget one of those magic spring annotations? This is what my main class looks like.

@SpringBootApplication 
@ComponentScan
@Configuration
@EnableWebMvc
public class JobengineMonitorApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(JobengineMonitorApplication.class, args);
    }

 }

I am using thymeleaf as the template engine

Bougie answered 19/9, 2017 at 13:41 Comment(0)
B
1

Ok so this appears to be working now. Of course I managed to get it working just after ranting about it :).

Anyway, what i did was.

  1. Remove @EnableWebMvc from the main class
  2. For added ../ to the href depending on the urls e.g /index was fine but /edit/something.html was not

Sorry for wasting peoples time but hopefully this could be useful for another rookie like me

Bougie answered 19/9, 2017 at 14:2 Comment(2)
did you try the solution i proposed? Is something is working in my project and seems quite easy and no configurationReverso
Why did you remove EnableWebMVC ? Did you get your attempt (1) to work? setting "favico.ico" in static resources. I am having issues with that.Stenotype
L
17

I solved this problem by putting favicon.ico in main/resource/static and adding this lines to my security config

 httpSecurity
            .authorizeRequests()
                .antMatchers( "/favicon.ico").permitAll()
Lillylillywhite answered 5/4, 2018 at 18:12 Comment(1)
Can you share your security config class to give a broader context? I am facing the same issue and haven't resolved it.Glassworker
R
12

I have this with SpringBoot configuration too and is working

<link rel="shortcut icon" type="image/png" th:href="@{/img/favicon.png}"/>

And the favicon.png under resources/public/img

Reverso answered 19/9, 2017 at 13:47 Comment(1)
For me it was th:href="@{/favicon.ico}" to resolve /src/main/resources/static/favicon.ico.Bailee
F
9

If anyone faces the same problem using newer version of Spring (in my case spring boot 2.4.4), here's the scenario that worked fine for me:

  1. Put the favicon.ico in the /resources/static folder. I also tried to put it just in the /resourses/ folder and it worked fine as well, so do not worry about the folder that much.
  2. Create a FaviconConfiguration in your configurations folder with the following content:
@Configuration
public class FaviconConfiguration {

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

    @Bean
    protected ResourceHttpRequestHandler faviconRequestHandler() {
        ResourceHttpRequestHandler requestHandler
                = new ResourceHttpRequestHandler();
        requestHandler.setLocations(Collections.singletonList(new ClassPathResource("/")));
        return requestHandler;
    }
}
  1. In case you use Spring Security don't forget to add antMatcher for your favicon resource by adding the following code to your SpringSecurityConfiguration (as JaneXQ already mentioned above):
.antMatchers("/static/favicon.ico").permitAll()
  1. Use link to your custom favicon explicitly in your html. To do that just put the following link in the <head> section of each of your html pages in the following way (I used thymeleaf in here):
<head>
    ...
    <link rel="icon" type="image/ico" th:href="@{../static/favicon.ico}">
    ...
</head>
Friend answered 6/5, 2021 at 20:57 Comment(3)
I hate that this is the only thing I got to work. It is way overblown for the favicon, but this works!Charolettecharon
Yeah, sir. I kinda had done a little research spending hours before I got it worked fine for meFriend
I needed exactly this! Thanks a lot.Pigsty
B
8

Put your favicon.ico file in:

src/main/resources/public
Byzantium answered 24/6, 2019 at 7:39 Comment(4)
can you share the config? I also have it in public but still default is loadedDiedra
@alex I used no special config for thisByzantium
Excellent! 2nd comment above yours here, while it may work, is like shooting someone in the chest 5 times and having 'em bleed out. Yours here however is a shot in the head.Presage
If this is not working for you, close the tab with your app open, clear your browser cache, open a new tab, and try again. Worked for me after that.Machinate
D
4

Put your favicon.png under src/main/resources/public and add this to your *.html page exactly in the header section

 <link rel="shortcut icon" type="image/png" th:href="@{favicon.png}"/>
Dehnel answered 17/10, 2020 at 15:54 Comment(0)
R
2

I saved my favicon which was a simple .png download as src/main/resources/static/favicon.ico

I couldn't get it to display until I tried another browser and it worked fine - so try clearing the browser cache, or try testing on another browser

Regolith answered 19/8, 2021 at 14:3 Comment(1)
Great reminder - clear the browser cache after trying some of these solutions! I tried multiple solutions (who knows how many would have worked) but only after clearing the browser cache did my favicon appear. For me, I did not have to configure a custom favicon bean, so try the simpler fixes, clear the cache, and see if that works!Unlearn
B
1

Ok so this appears to be working now. Of course I managed to get it working just after ranting about it :).

Anyway, what i did was.

  1. Remove @EnableWebMvc from the main class
  2. For added ../ to the href depending on the urls e.g /index was fine but /edit/something.html was not

Sorry for wasting peoples time but hopefully this could be useful for another rookie like me

Bougie answered 19/9, 2017 at 14:2 Comment(2)
did you try the solution i proposed? Is something is working in my project and seems quite easy and no configurationReverso
Why did you remove EnableWebMVC ? Did you get your attempt (1) to work? setting "favico.ico" in static resources. I am having issues with that.Stenotype
N
1

Try to replace th:href with href. It worked for me.

<link rel="icon" href="/favicon.ico" type="image/ico">
Niel answered 8/5, 2021 at 13:50 Comment(2)
Does this address the question? The OP does not mention using th:href.Diapophysis
That simply did the trick (and automatically resolved from /src/main/resources/static/favicon.icoBailee
A
0

For some reason .ico format was not working. I just placed a png image instead and spring automatically picked the favicon.

I placed the png image in \src\main\resources\public

Spring boot + thymeleaf

Artillery answered 17/1, 2021 at 13:18 Comment(0)
S
0

I had the same issue and fix it removing @EnableAdminServer annotation

Superimposed answered 14/9, 2021 at 17:55 Comment(0)
B
0
  • Spring boot
plugins {
    id 'java'
    id 'org.springframework.boot' version '3.0.6'
    id 'io.spring.dependency-management' version '1.1.0'
}

group = 'com.security.web'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
 <link rel="shortcut icon" type="image/x-icon" th:href="@{favicon.ico}"/>
  • src/main/resources/static/favicon.ico

  • spring security

@EnableWebFluxSecurity
@Configuration
public class WebSecurityConfig {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity httpSecurity) {
        return httpSecurity
                .formLogin().and()
                .httpBasic().disable()
                .authorizeExchange()
                .pathMatchers("/", "/login", "/signup").permitAll()
                .pathMatchers(HttpMethod.POST, "/api/users").permitAll()
                .pathMatchers("/favicon.ico").permitAll()
                .pathMatchers(HttpMethod.GET, "/api/users/**").hasRole("ADMIN")
                .anyExchange().authenticated()
                .and()
                .build();
    }
}
Brunner answered 23/5, 2023 at 8:15 Comment(0)
V
0

Spring Boot 3.1.4 Solution

<link rel="icon" type="image/x-icon" th:href="@{/assets/myfavicon.ico}"/>

Favicon Directory

/src/main/resources/static/assets

Spring Security Configuration

@Configuration
@EnableWebSecurity
public class SecurityConfiguration {

  ....
  ....

  @Bean
  public SecurityFilterChain myApplicationSecurityFilterChain(HttpSecurity httpSecurity) throws Exception {

    httpSecurity
      .authorizeHttpRequests((requests) -> requests
        .requestMatchers("/", "/assets/**").permitAll()
        .anyRequest().authenticated()
      )
      ...
      ...
      return httpSecurity.build();
  }
}
Vacua answered 18/10, 2023 at 20:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.