代码之家  ›  专栏  ›  技术社区  ›  Johan Fredin

弹簧靴定制favicon。ico未显示

  •  16
  • Johan Fredin  · 技术社区  · 8 年前

    我知道这个问题已经被反复问过了,有几种解决方案。除了建议为此编写自己的配置bean之外,我已经尝试了其中的一些。我不想做这些只是为了显示一个小图标,它有点过头了。但我不能让它工作。这些是我迄今为止尝试过的解决方案。

    1. 春天mvc。法维孔。enabled=应用程序中的false。属性,没有显示任何favicon(我想这就是重点)。
    2. <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 ()

    这就是我的主课。

    @SpringBootApplication 
    @ComponentScan
    @Configuration
    @EnableWebMvc
    public class JobengineMonitorApplication implements CommandLineRunner {
    
        public static void main(String[] args) {
            SpringApplication.run(JobengineMonitorApplication.class, args);
        }
    
     }
    

    我使用thymeleaf作为模板引擎

    11 回复  |  直到 8 年前
        1
  •  14
  •   JaneXQ    8 年前

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

     httpSecurity
                .authorizeRequests()
                    .antMatchers( "/favicon.ico").permitAll()
    
        2
  •  8
  •   cralfaro    8 年前

    我在SpringBoot配置中也有这个功能,并且正在运行

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

    还有favicon。png下

        3
  •  3
  •   Ikbel    5 年前

    把你的 favicon.png src/main/resources/public 并将其添加到您的 *.html 页面完全位于 header 部分

     <link rel="shortcut icon" type="image/png" th:href="@{favicon.png}"/>
    
        4
  •  3
  •   Nikita Shcherbakov    4 年前

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

    1. /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. antMatcher 通过将以下代码添加到 SpringSecurityConfiguration (作为 JaneXQ
    .antMatchers("/static/favicon.ico").permitAll()
    
    1. 使用自定义favicon的链接 明确地 <head> 第节 每个
    <head>
        ...
        <link rel="icon" type="image/ico" th:href="@{../static/favicon.ico}">
        ...
    </head>
    
        5
  •  2
  •   Bohemian    6 年前

    favicon.ico 文件输入:

    src/main/resources/public
    
        6
  •  2
  •   oldbird    4 年前

    我保存了我的favicon,这是一个简单的。png下载为src/main/resources/static/favicon。ico公司

        7
  •  1
  •   Johan Fredin    8 年前

    好的,这似乎正在工作。当然,我在咆哮之后就设法让它工作了:)。

    无论如何,我所做的是。

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

        8
  •  0
  •   Arv    5 年前

    出于某种原因。ico格式不工作。我只是放置了一个png图像,spring自动选择了favicon。

    弹簧靴+胸腺

        9
  •  0
  •   roy ren    4 年前

    尝试用href替换th:href。这对我很有效。

    <link rel="icon" href="/favicon.ico" type="image/ico">
    
        10
  •  0
  •   horelvis    4 年前

        11
  •  0
  •   EduGord    3 年前

    我对此采取了一种非正统的做法。

    @RestController
    @RequestMapping(path = "/")
    public class PublicController {
        private static final String FAVICON_PATH = "src/main/resources/static/favicon.ico";
        private static final byte[] IN_MEMORY_FAVICON;
    
        /**
         * This will prevent Bean/Application initialization if it catches an {@link IOException}.
         */
        static {
            try {
                IN_MEMORY_FAVICON = Files.readAllBytes(Paths.get(FAVICON_PATH));
            } catch (IOException e) {
                throw new RuntimeException(String.join("", FAVICON_PATH, " not found"));
            }
        }
        // Your code before
        @GetMapping(path="/favicon.ico", produces="image/x-icon")
        public byte[] favicon() {
            return IN_MEMORY_FAVICON;
        }
        // Your code after
    }
    

    如果您的申请 @EnableWebSecurity 确保你 permitAll() HttpMethod.GET 正在搜索favicon资源。

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
    // Your code before
    httpSecurity.antMatchers(HttpMethod.GET, "/favicon.ico").permitAll()
    // Your code after
    return httpSecurity.build();
    

    OncePerRequestFilter 通过重写来绕过应用于搜索favicon的请求的过滤器可能很有用 shouldNotFilter(HttpServletRequest request)

    @Component
    public class MyCustomFilter extends OncePerRequestFilter {
        private final AntPathMatcher antPathMatcher = new AntPathMatcher();
        // Your code before
        @Override
        protected boolean shouldNotFilter(HttpServletRequest request) {
            return antPathMatcher.match( "/favicon.ico", request.getServletPath()));
        }
       // Your code after
    }