SpringBoot 2.x.x整合Spring Security5和Thymeleaf遇到的坑

今天在学习Spring Security的时候遇到了些问题,网上搜索了很久都没有找到完美的解决方案。
最后摸索了一下解决了。
主要出现了以下问题:

  1. thymeleaf-extras-springsecurity与Spring Security版本不对应导致页面中的sec:xx表达式无效
  2. html文件头部的名称空间错误导致idea无法正常提示代码

sec:xx表达式无效

表达式没有效果主要原因是thymeleaf-extras-springsecurity与Spring Security、thymeleaf版本不对应导致.

正确对应关系:

thymeleaf-extras-springsecurity版本Spring Security 版本
33.x
44.x
55.x
  • 版本3.0.4.RELEASE-适用于Thymeleaf 3.0(需要Thymeleaf 3.0.10+)
  • 版本2.1.3.RELEASE-适用于Thymeleaf 2.1(需要Thymeleaf 2.1.2+)

maven信息

  • groupId: org.thymeleaf.extras
  • artifactId:
    - Spring Security 3: thymeleaf-extras-springsecurity3
    - Spring Security 4: thymeleaf-extras-springsecurity4
    - Spring Security 5: thymeleaf-extras-springsecurity5

由于我使用的是springboot 2.2.2,pom文件依赖如图:
在这里插入图片描述

Idea无法正常提示代码

这个问题的解决方案我在网上找了很久最终在thymeleaf-extras-springsecurity官方github找到了解决方案。

标签没有提示标签无提示

后面发现是因为html的命名空间的问题。

来到thymeleaf-extras-springsecurity的官方github:thymeleaf-extras-springsecurity

README的最后一项:

Namespace

The namespace for all versions of this dialect is http://www.thymeleaf.org/extras/spring-security.

<html xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

Getting the namespace incorrect won’t impact processing of your template. It
might however impact your IDE when it comes to things like suggestions/auto-completion
in your templates.

根据提示修改html的命名空间为:html xmlns:sec=“http://www.thymeleaf.org/extras/spring-security”>

修改后的效果:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值