关于springboot2.2.4和thymeleaf-extras-springsecurity5整合使用的一些问题

首先引入依赖:

     <!--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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值