thymeleaf中security的sec:authorize标签不起作用的原因


环境版本:springboot版本2.1.6.RELEASE、spring-security版本5.1.5.RELEASE、thymeleaf-spring5版本3.0.11.RELEASE

1、错误thymeleaf的html代码如下:

1 <sec:authorize access="hasRole('ROLE_USER')">
2 <!--显示内容-->
3 </sec:authorize> 

注:改写发为thymeleaf-spring4的写法,thymeleaf-spring5不起作用

2、正确thymeleaf的html代码如下:

1 <div sec:authorize="hasRole('ROLE_ADMIN')">
2     <!--显示内容-->                    
3 </div>

3、还需在html模板引入thymeleaf-spring5命名空间

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

注:与网上上的有点区别,但是能起作用(我的环境下亲测有效),网上大多数是xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"与xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"这种命名空间。以实际网站为主https://github.com/thymeleaf/thymeleaf-extras-springsecurity,主文在网站的底部。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM