Spring Boot 自定义 favicon.ico 未显示

问题描述 投票:0回答:11

我知道这个问题已经在这里被反复问过,并且有多种解决方案。我已经尝试了其中的几种,除了建议您为此编写自己的配置 bean 的那些。我不想做这一切只是为了显示一个小图标,这似乎有点过分了。但我无法让它发挥作用。这些是我迄今为止尝试过的解决方案。

  1. 只需在静态资源下添加 favicon.ico 就可以了....但不行。
  2. application.properties 中的 spring.mvc.favicon.enabled=false,根本没有显示 favicon(我想这就是全部要点)。
  3. 尝试了 2 个将 favicon 作为链接包含在 html 页面中的示例。就像这样:
    
    <link rel="icon" type="image/png" href="favicon.png" />
    <link rel="icon" type="image/x-icon" href="favicon.ico" />

这些都不起作用。

  1. 尝试将我自己的图标重命名为其他名称并如上所述引用它。不起作用。

在浏览器中检查页面时,尽管没有显示图标,但有时我根本没有打印出任何错误,或者我收到一条错误消息,指出

GET http://localhost:8080/myapp/favicon.png 404 ()
将类型引用为 JSON(我觉得很奇怪)。

我已经没有想法了,所以如果有人能告诉我为什么这不起作用,请告诉我。我是否忘记了那些神奇的弹簧注释之一? 这就是我的主课的样子。

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

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

 }

我使用 thymeleaf 作为模板引擎

spring-mvc spring-boot thymeleaf favicon
11个回答
16
投票

我通过将 favicon.ico 放入 main/resource/static 并将此行添加到我的安全配置中解决了这个问题

 httpSecurity
            .authorizeRequests()
                .antMatchers( "/favicon.ico").permitAll()

10
投票

我也有 SpringBoot 配置并且正在工作

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

还有resources/public/img下的favicon.png


6
投票

如果有人在使用较新版本的 Spring 时遇到同样的问题(在我的例子中是 spring boot 2.4.4),以下是对我来说效果很好的场景:

  1. 将 favicon.ico 放入
    /resources/static
    文件夹中。我也尝试将它放在
    /resourses/
    文件夹中,效果也很好,所以不用太担心文件夹。
  2. 在配置文件夹中创建一个
    FaviconConfiguration
    ,其中包含以下内容:
@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. 如果您使用 Spring Security,请不要忘记为您的 favicon 资源添加
    antMatcher
    ,方法是将以下代码添加到您的 SpringSecurityConfiguration(如上面已经提到的 JaneXQ):
.antMatchers("/static/favicon.ico").permitAll()
  1. 在 html 中明确使用指向您的自定义图标的链接。为此,只需按照以下方式将以下链接放入 html 页面的
    each
    <head> 部分(我在这里使用了 thymeleaf):
<head>
    ...
    <link rel="icon" type="image/ico" th:href="@{../static/favicon.ico}">
    ...
</head>

4
投票

将您的

favicon.png
放在
src/main/resources/public
下,并将其添加到您的
*.html
页面的
header
部分

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

4
投票

将您的

favicon.ico
文件放入:

src/main/resources/public

2
投票

我将我的图标保存为 src/main/resources/static/favicon.ico,这是一个简单的 .png 下载

我无法让它显示,直到我尝试另一个浏览器并且它工作正常 - 所以尝试清除浏览器缓存,或尝试在另一个浏览器上测试


1
投票

好的,这看起来现在有效了。当然,我在咆哮之后就设法让它工作:)。

无论如何,我所做的是。

  1. 从主类中删除@EnableWebMvc
  2. 根据网址将 ../ 添加到 href,例如 /index 很好,但 /edit/something.html 则不然

抱歉浪费了大家的时间,但希望这对像我这样的新手有用


1
投票

尝试将 th:href 替换为 href。这对我有用。

<link rel="icon" href="/favicon.ico" type="image/ico">

0
投票

由于某种原因,.ico 格式无法正常工作。我只是放置了一个 png 图像,spring 自动选择了该图标。

我将 png 图片放在 \src\main 中 资源\公共

Spring boot + thymeleaf


0
投票

我有同样的问题并修复它删除@EnableAdminServer注释


0
投票
  • 弹簧靴
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

  • 春季安全

@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();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.