在使用thymeleaf渲染前端的html時,thymeleaf為SpringSecurity提供的標簽屬性,首先需要引入thymeleaf-extras-springsecurity4依賴支持。
一、在pom 文件中的引入springsecurity的標簽依賴thymeleaf-extras-springsecurity5。
<dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> <version>3.0.4.RELEASE</version> </dependency>
二、在html文件里面申明使用。
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5" >
三、常用SpringSecurity的標簽屬性介紹
判斷用戶是否已經登陸認證,引號內的參數必須是isAuthenticated()。 sec:authorize="isAuthenticated()" 獲得當前用戶的用戶名,引號內的參數必須是name。 sec:authentication=“name” 判斷當前用戶是否擁有指定的權限。引號內的參數為權限的名稱。 sec:authorize=“hasRole(‘role’)” 獲得當前用戶的全部角色,引號內的參數必須是principal.authorities。 sec:authentication="principal.authorities"
四、運用介紹
使用sec:authorize="isAuthenticated()" 判斷是否登錄 登錄之后顯示的頁面內容
<div sec:authorize="isAuthenticated()"> <h2><span sec:authentication="name"></span>,您好 您的身份是 <span sec:authentication="principal.authorities"></span> </h2> </div>
使用sec:authorize="hasRole('VIP1')"控制
<div sec:authorize="hasRole('VIP1')"> <h3>我的VIP角色</h3> <ul> <li><a th:href="@{/level1/1}">免費閱讀</a></li> <li><a th:href="@{/level1/2}">購買8折</a></li> </ul> </div>
以上是本人對Thymeleaf提供的SpringSecurity的標簽支持的簡單使用,希望對需要的朋友能夠提供到幫助!