2020-11-17

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值