Skip to main content
 首页 » 编程设计

spring-boot之Favicon 未正确呈现,看起来乱码

2025年12月25日27me-sa

我的网站图标似乎突然出现了问题。我不确定我做了什么改变导致它开始失败,但它曾经渲染得很好,现在它是乱码。

这是它的外观示例:



我正在使用 Spring Boot 并在谷歌上搜索了有关如何让您的网站图标显示的所有典型答案......但我没有运气。

我注意到的一件事(不确定这是否正常)是,当我访问网站图标 URL 时,它不会将其作为图标加载到浏览器中,而是作为一堆文本加载。

这是我访问我的 localhost:8080/favicon.ico url 时发生的情况:



我唯一能想到的我最近更改的可能对网站图标产生影响的更改是我的 WebSecurityConfig.java...我添加了一个 REALM 和基本身份验证。

这是 WebSecurityConfig.java 文件:

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter 
{ 
  @Autowired 
  private UserDetailsService userDetailsService; 
 
  private static String REALM="MY_TEST_REALM"; 
 
  @Bean 
  public PasswordEncoder passwordEncoder() { 
      return new BCryptPasswordEncoder(); 
  } 
 
  @Autowired 
  public void globalSecurity (AuthenticationManagerBuilder auth) throws Exception 
  { 
    auth.userDetailsService(userDetailsService) 
      .passwordEncoder(passwordEncoder()); 
  } 
 
  @Override 
  protected void configure(HttpSecurity http) throws Exception 
  { 
    // authenticate / authorize 
    // authentication = who the hell are you? i.e. username/password 
    // authorization = what can you access in the app? 
 
    http 
      .csrf().disable() 
      .authorizeRequests() 
        .antMatchers("/*").permitAll() 
        .antMatchers("/js/**").permitAll() 
        .antMatchers("/webinars/**").permitAll() 
        .antMatchers("/img/**").permitAll() 
        .antMatchers("/fonts/**").permitAll() 
        .antMatchers("/register").permitAll() 
        .antMatchers("/samcart").permitAll() 
        .antMatchers("/sales").permitAll() 
        .antMatchers("/sales/**").permitAll() 
        .antMatchers("/paypal/**").permitAll() 
        .antMatchers("/forgotPassword").permitAll() 
        .antMatchers("proffesso-favicon.ico").permitAll() 
        .antMatchers("/students/purchasedCourse.html").permitAll() 
        .antMatchers("/students/courses/**").permitAll() 
        .antMatchers("/teachers/courses/*/image").permitAll() 
        .antMatchers("/teachers/courses/*/offers/*/image").permitAll() 
        .antMatchers("/handlebars/**").permitAll() 
        .antMatchers("/css/**").permitAll() 
        .antMatchers("/admin/**").hasRole("ADMIN") 
        .antMatchers("/admin").hasRole("ADMIN") 
        .anyRequest().authenticated() 
        .and() 
      .httpBasic() 
        .realmName(REALM) 
        .authenticationEntryPoint(getBasicAuthEntryPoint()) 
        .and() 
      .formLogin() 
        .loginPage("/login") 
        .defaultSuccessUrl("/students/courses") 
        .successHandler(new NoRedirectSavedRequestAwareAuthenticationSuccessHandler()) 
        .permitAll() 
        .and() 
      .logout() 
        .logoutSuccessUrl("/").permitAll() 
        .and() 
      .sessionManagement() 
        .maximumSessions(1); 
  } 
 
  @Bean 
  public CustomBasicAuthenticationEntryPoint getBasicAuthEntryPoint(){ 
      return new CustomBasicAuthenticationEntryPoint(); 
  } 
 
  /* To allow Pre-flight [OPTIONS] request from browser */ 
  @Override 
  public void configure(WebSecurity web) throws Exception { 
      web.ignoring().antMatchers(HttpMethod.OPTIONS, "/**"); 
  } 
 
  @Bean 
  public HttpSessionEventPublisher httpSessionEventPublisher() { 
      return new HttpSessionEventPublisher(); 
  } 
 
  @Bean() 
  public SecurityEvaluationContextExtension securityEvaluationContextExtension () { 
    return new SecurityEvaluationContextExtension(); 
  } 
} 

请您参考如下方法:

不知道你是否想通了这一点。我最近遇到了同样的事情,我的图标看起来就像你截图中的那个。对我来说,这是由 Maven 资源过滤引起的。我为 *.ico 添加了一个排除项,如下所示:

<resources> 
    <resource> 
        <directory>src/main/resources</directory> 
        <filtering>true</filtering> 
        <excludes> 
                <exclude>**/*.ico</exclude> 
        </excludes> 
    </resource> 
</resources>