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,主文在网站的底部。

 

posted @ 2020-05-14 00:03  自然而然の等待  阅读(1791)  评论(0)    收藏  举报