我知道这个问题已经在这里被反复问过,并且有多种解决方案。我已经尝试了其中的几种,除了建议您为此编写自己的配置 bean 的那些。我不想做这一切只是为了显示一个小图标,这似乎有点过分了。但我无法让它发挥作用。这些是我迄今为止尝试过的解决方案。
<link rel="icon" type="image/png" href="favicon.png" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
这些都不起作用。
在浏览器中检查页面时,尽管没有显示图标,但有时我根本没有打印出任何错误,或者我收到一条错误消息,指出
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 作为模板引擎
我通过将 favicon.ico 放入 main/resource/static 并将此行添加到我的安全配置中解决了这个问题
httpSecurity
.authorizeRequests()
.antMatchers( "/favicon.ico").permitAll()
我也有 SpringBoot 配置并且正在工作
<link rel="shortcut icon" type="image/png" th:href="@{/img/favicon.png}"/>
还有resources/public/img下的favicon.png
如果有人在使用较新版本的 Spring 时遇到同样的问题(在我的例子中是 spring boot 2.4.4),以下是对我来说效果很好的场景:
/resources/static
文件夹中。我也尝试将它放在 /resourses/
文件夹中,效果也很好,所以不用太担心文件夹。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;
}
}
antMatcher
,方法是将以下代码添加到您的 SpringSecurityConfiguration(如上面已经提到的 JaneXQ):.antMatchers("/static/favicon.ico").permitAll()
each的
<head>
部分(我在这里使用了 thymeleaf):<head>
...
<link rel="icon" type="image/ico" th:href="@{../static/favicon.ico}">
...
</head>
将您的
favicon.png
放在 src/main/resources/public
下,并将其添加到您的 *.html
页面的 header
部分
<link rel="shortcut icon" type="image/png" th:href="@{favicon.png}"/>
将您的
favicon.ico
文件放入:
src/main/resources/public
我将我的图标保存为 src/main/resources/static/favicon.ico,这是一个简单的 .png 下载
我无法让它显示,直到我尝试另一个浏览器并且它工作正常 - 所以尝试清除浏览器缓存,或尝试在另一个浏览器上测试
好的,这看起来现在有效了。当然,我在咆哮之后就设法让它工作:)。
无论如何,我所做的是。
抱歉浪费了大家的时间,但希望这对像我这样的新手有用
尝试将 th:href 替换为 href。这对我有用。
<link rel="icon" href="/favicon.ico" type="image/ico">
由于某种原因,.ico 格式无法正常工作。我只是放置了一个 png 图像,spring 自动选择了该图标。
我将 png 图片放在 \src\main 中 资源\公共
Spring boot + thymeleaf
我有同样的问题并修复它删除@EnableAdminServer注释
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'
编辑*.html
<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();
}
}