springboot+springsecurity+thymeleaf失效问题和An error happened during template

本文介绍如何在SpringBoot项目中整合SpringSecurity和Thymeleaf,实现安全控制和视图渲染。文章解决版本不匹配导致的安全功能失效问题,以及模板解析错误,确保正确使用sec:authorize和sec:authentication。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot+springsecurity+thymeleaf的使用方式十分简单,用一个类继承WebSecurityConfigurerAdapter并重写方法即可和@EnableWebSecurity开启服务,交给spring管理

遇到的两个问题错误

sec:authentication,sec:authorize 失效问题

An error happened during template parsing (template: “class path resource [templates/index.html]”)模板解析错误

第一:版本问题

springboot和springsecurity版本不匹配,会产生thymeleaf和security联合使用不生效问题

thymeleaf和springsecurity以及thymeleaf-extras-springsecurity的版本配合问题,如果是springboot2.1.0以上则可用thymeleaf-extras-springsecurity5,因为thymeleaf和springsecurity都是5.如果想用thymeleaf-extras-springsecurity4,则需降到需要将springboot的版本调低至2.0.9及以下。

第二:模板解析错误
则是在HTML页面设置有误,导致模板解析不了
开头需注意引用问题,在thymeleaf中使用springsecurity

html lang=“en” xmlns:th=“http://www.thymeleaf.org”
xmlns:sec=“http://www.thymeleaf.org/thymeleaf-extras-springsecurity5”

sec:authorize :判断信息是否存在

sec:authentication:取出相应的值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值