首先引入依赖:
<!--thymeleaf与Spring Security整合的依赖 https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
问题:
在springboot2.2.4中导入thymeleaf、springsecurity最新版本的依赖后使用thymeleaf-extras-springsecurity5进行认证授权操作,HTML文件中无法使用sec代码提示,并且浏览器执行报错
解决办法:
使用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 对应使用以及一些使用示例
————————————————
版权声明:本文为CSDN博主「wick_hp」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36488647/article/details/104532754