问题:
在springboot2.2.4中导入thymeleaf、springsecurity最新版本的依赖后使用thymeleaf-extras-springsecurity5进行认证授权操作,HTML文件中无法使用sec代码提示,并且浏览器执行报错
经过降低springboot版本和使用thymeleaf-extras-springsecurity4等方法后都无法解决问题
解决办法:
使用Springboot最新版本和thymeleaf-extras-springsecurity5的话在HTML文件中的约束导入正确的使用方式如下:
<html lang="en"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security"> <!-- 主要是这行代码 官方建议使用 -->
而本人使用的方式如下所示:
最新版本该方式是使用错误的:
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<!-- 如果使用新版本不要使用该方式 -->
简单的使用示例:
显示登录用户名:
<span sec:authentication="name"></span>
显示当前用户权限:
方式1: <span sec:authentication="principal.authorities"></span>
方式2: <span sec:authentication="authorities"></span>
官方文档:
https://www.thymeleaf.org/doc/articles/springsecurity.html
https://github.com/thymeleaf/thymeleaf-extras-springsecurity
该文档介绍了不同版本的 thymeleaf、 springsecurity 、thymeleaf-extras-springsecurity 对应使用以及一些使用示例