Spring boot security thymeleaf关于favicon.ico的404等问题

出现/favicon.ico访问404、网页图标不更新、成功登录后出现404等问题,做了各种尝试。在如下环境解决方法
环境: spring boot + spring security+ thymeleaf
解决方法:

1.favicon.ico文件放在项目的/resources/static目录下
2.自定义的WebSecurityConfigurerAdapter类中

 private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "/resources/**",   
            "/static/**",
            "/css/**",
            "/ajax/**",
            "/fonts/**",
            "/img/**",
            "/js/**",
            "/login/**",
            "/webjars/**",
            "/error/**"
            };

    /**
     * 放行静态资源
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers(CLASSPATH_RESOURCE_LOCATIONS);
    }
  1. 不需要在上面类中添加“/favicon.ico"的登录放行配置
 @Override
    public void configure(HttpSecurity http) throws Exception {

        //解决非thymeleaf的form表单提交被拦截问题
        http.csrf().disable();


        http.authorizeRequests() //授权
  1. 不需要在application.yml中做任何配置
  2. 引用的html文件中必须添加,否则还是404错误
	<link rel="Bookmark" type="image/x-icon" th:href="@{/static/favicon.ico}">
    <link rel="icon" type="image/x-icon" th:href="@{/static/favicon.ico}">
    <link rel="shortcut icon" type="image/x-icon" th:href="@{/static/favicon.ico}">

.

```xml <!-- Spring Boot Starter Parent --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Security --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!-- Thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- Thymeleaf Spring Security --> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency> <!-- Spring Boot Starter Test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> ``` 注释说明: 1. Spring Boot Starter Parent:Spring Boot 的父级依赖,定义了 Spring Boot 的版本和依赖管理。 2. Spring Boot Starter Web:Spring Boot Web 组件的依赖,包含了 Spring MVC、Tomcat 等。 3. Spring SecuritySpring Security 组件的依赖,提供了安全认证和授权功能。 4. ThymeleafThymeleaf 模板引擎的依赖,用于生成 HTML 页面。 5. Thymeleaf Spring SecurityThymeleafSpring Security 集成的依赖,提供了 Thymeleaf 中使用 Spring Security 的标签和属性。 6. Spring Boot Starter Test:Spring Boot 测试组件的依赖,包含了 JUnit、Mockito 等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

muyi517

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值