一、在.html頁面引入spring security的命名空間html
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
二、引入依賴spring
<dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency>
三、使用sec:authorize屬性post
<body> <p>點擊<a th:href="@{/hello}">這裏</a>打個招呼吧</p> <form th:action="@{/logout}" method="post"> <input type="submit" value="退出登錄"> </form> <p sec:authorize="hasRole('ROLE_admin')"><a th:href="@{/hello/helloAdmin}">管理員</a></p> <p sec:authorize="hasRole('ROLE_normal')"><a th:href="@{/hello/helloUser}">普通用戶</a></p> </body>
四、從頁面上顯示當前登錄的用戶名code
<h1>歡迎光臨 當前登錄帳號:<label th:text="${name}"></label> 經過標籤設置::<label sec:authentication="name"></label> </h1>