今天在学习Spring Security的时候遇到了些问题,网上搜索了很久都没有找到完美的解决方案。
最后摸索了一下解决了。
主要出现了以下问题:
- thymeleaf-extras-springsecurity与Spring Security版本不对应导致页面中的sec:xx表达式无效
- html文件头部的名称空间错误导致idea无法正常提示代码
sec:xx表达式无效
表达式没有效果主要原因是thymeleaf-extras-springsecurity与Spring Security、thymeleaf版本不对应导致.
正确对应关系:
thymeleaf-extras-springsecurity版本 | Spring Security 版本 |
---|---|
3 | 3.x |
4 | 4.x |
5 | 5.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”>
修改后的效果: