環境版本: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,主文在網站的底部。