SpringSecurity(十五)—–Thymeleaf中Spring Security的使用

SpringSecurity(十五)—–Thymeleaf中Spring Security的使用

一.使用理由

Spring Security可以在一些视图技术中进行控制显示效果。例如:JSP或Thymeleaf。在非前后端分离且使用Spring Boot的项目中多使用Thymeleaf作为视图展示技术。

二.实现步骤

1)添加依赖

Thymeleaf对Spring Security的支持都放在thymeleaf-extras-springsecurityX中,目前最新版本为5。所以需要在项目中添加此jar包的依赖和thymeleaf的依赖。后面的X要根据所有的springboot版本来决定

    <!--thymeleaf与security整合包-->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
            <version>3.0.4.RELEASE</version>
        </dependency>
        <!--thymeleaf启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2)在html页面中引入thymeleaf命名空间和security命名空间

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">

3)具体使用

3-1)获取属性

可以在html页面中通过 sec:authentication=””获取UsernamePasswordAuthenticationToken中所有getXXX的内容,包含父类中的getXXX的内容。
根据源码得出下面属性:
name:登录账号名称
principal:登录主体,在自定义登录逻辑中是UserDetails
credentials:凭证
authorities:权限和角色
details:实际上是WebAuthenticationDetails的实例。可以获取remoteAddress(客户端ip)和sessionId(当前sessionId)

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    登录账号:<span sec:authentication="name">123</span><br/>
    登录账号:<span sec:authentication="principal.username">456</span><br/>
    凭证:<span sec:authentication="credentials">456</span><br/>
    权限和角色:<span sec:authentication="authorities">456</span><br/>
    客户端地址:<span sec:authentication="details.remoteAddress">456</span><br/>
    sessionId:<span sec:authentication="details.sessionId">456</span><br/>
</body>
</html>

在这里插入图片描述

三.权限判断

在html页面中可以使用sec:authorize=”表达式”进行权限控制,判断是否显示某些内容。表达式的内容和access(表达式)的用法相同。如果用户具有指定的权限,则显示对应的内容;如果表达式不成立,则不显示对应的元素。
1.不同权限的用户显示不同的按钮

在页面中根据用户权限和角色判断页面中显示的内容
通过权限判断:
<button sec:authorize="hasAuthority('/insert')">新增</button>
<button sec:authorize="hasAuthority('/delete')">删除</button>
<button sec:authorize="hasAuthority('/update')">修改</button>
<button sec:authorize="hasAuthority('/select')">查看</button>
<br/>
通过角色判断:
<button sec:authorize="hasRole('abc')">新增</button>
<button sec:authorize="hasRole('abc')">删除</button>
<button sec:authorize="hasRole('abc')">修改</button>
<button sec:authorize="hasRole('abc')">查看</button>

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/2349.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • centos 7.5 内核版本_内核版本多少算好手机

    centos 7.5 内核版本_内核版本多少算好手机实验环境CentOS-7-x86_64-Minimal-1708.isoCentOSLinuxrelease7.4.1708(Core)Kernel3.10.0-693.el7.x86_64方案一:小版本升级连接并同步CentOS自带yum源,更新内核版本。此方法适用于更新内核补丁。具体实验步骤:sudoyumlistkernelsudoyumupdate-yke…

    2022年8月23日
    8
  • LaTeX 换行、换页、空白空间[通俗易懂]

    LaTeX 换行、换页、空白空间[通俗易懂]一般来说,我们不推荐你改变默认的LaTeX文档结构。当然,我们有时候也有这个需求。所以,在本文中,我们将解释如何在文档中插入空行,以及插入任意的空白。

    2022年5月14日
    149
  • 数字电路实验(三)——加法器、运算器

    数字电路实验(三)——加法器、运算器1、实验步骤:A全加器:1个vhd文件,用来定义顶层实体1个vwf文件,用来进行波形仿真,将验证的波形输入1、新建,编写源代码。(1).选择保存项和芯片类型:【File】-【newprojectwizard】-【next】(设置文件路径+设置projectname为【C:\Users\lenovo\Desktop\笔记\大二上\数字电路\实验课\实验三\全加器】)-【next】(设…

    2022年7月12日
    18
  • Python六大基本数据类型介绍[通俗易懂]

    Python六大基本数据类型介绍[通俗易懂]Python基本数据类型一、整型1、整型:int2、二进制整型二、浮点型三、布尔型四、复数类型五、字符串六、列表七、元组八、集合一、整型1、整型:int在数字中,正整数、0、负整数都称为整型。例:intvar=1000#type获取数据类型res1=type(intvar)print(res1)#id获取内存地址res2=id(intvar)print(res2)运行结果:2、二进制整型也可用二进制表示整型,print自动转换为十进制。例:intvar=

    2022年5月13日
    40
  • flex vue 垂直居中居上_推荐几种在移动端实现垂直居中的方法[通俗易懂]

    flex vue 垂直居中居上_推荐几种在移动端实现垂直居中的方法[通俗易懂]推荐几种在移动端实现垂直居中的方法。方法1:table-cellhtml结构垂直居中CSS.box1{display:table-cell;vertical-align:middle;text-align:center;}方法2:display:flex.box2{display:flex;justify-content:center;align-items:Center;}123…

    2022年5月13日
    36

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号