Spring Security 与 Thymeleaf 整合—03
Thymeleaf可以和Spring Security进行整合:
可以在在用户登陆前和登陆后展示不同的视图,
并且根据用户的权限展示相应的权限内容,
或者获取显示用户的信息并显示在页面上。
thymeleaf和springsecurity的整合:参考的官方网址:【https://github.com/thymeleaf/thymeleaf-extras-springsecurity 】
【整合的步骤?】
1.导入thymeleaf和springsecurity整合的依赖。
<dependency>
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--整合依赖-->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<!--spring security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2.页面中需要导入thymeleaf和security的命名空间,并在页面中使用sec:xxx属性来使用。
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
基本使用:
判断是否以及授权
【sec:authorize="isAuthenticated()"】
判断是否包含权限:
【<div sec:authorize="hasRole('ROLE_ADMIN')">
This will only be displayed if authenticated user has role ROLE_ADMIN.
</div>
】
展示用户的用户名:
【
<div th:text="${#authentication.name}">
The value of the "name" property of the authentication object should appear here.
</div>
】