码迷,mamicode.com
首页 > 编程语言 > 详细

Springboot使用Thymeleaf提供的SpringSecurity标签控制支持

时间:2020-09-17 21:55:18      阅读:43      评论:0      收藏:0      [点我收藏+]

标签:artifact   文件   ipa   style   depend   cat   阅读   属性   渲染   

在使用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的标签支持的简单使用,希望对需要的朋友能够提供到帮助!

Springboot使用Thymeleaf提供的SpringSecurity标签控制支持

标签:artifact   文件   ipa   style   depend   cat   阅读   属性   渲染   

原文地址:https://www.cnblogs.com/a876459952/p/13651374.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!