Spring Security-Thymeleaf使用

简介: Spring Security-Thymeleaf使用

一、Thymeleaf中Spring Security的使用


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


Thymeleaf对Spring Security的支持都放在thymeleaf-extras-springsecurityX中,目前最新版本为5。所以需要在项目中添加此jar包的依赖和thymeleaf的依赖。


<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
复制代码


在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">
复制代码



1.获取属性


可以在html页面中通过 sec:authentication=""获取


UsernamePasswordAuthenticationToken中所有getXXX的内容,包含父类中的getXXX的内容。


根据源码得出下面属性:


  1. l name:登录账号名称


  1. l principal:登录主体,在自定义登录逻辑中是UserDetails


  1. l credentials:凭证


  1. l authorities:权限和角色


  1. l details:实际上是WebAuthenticationDetails的实例。可以获取remoteAddress(客户端ip)和sessionId(当 前sessionId)




1.1实现步骤:


1.1.1新建demo.html


在项目resources中新建templates文件夹,在templates中新建demo.html页面

image.png

1.1.2编写demo.html


在demo.html中编写下面内容,测试获取到的值


<!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>  
复制代码


1.1.3编写控制器


thymeleaf页面需要控制转发,在控制器类中编写下面方法


@RequestMapping("/demo")
public String demo(){
    return "demo";
}
复制代码



2.权限判断


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



2.1不同权限的用户显示不同的按钮


2.1.1设置用户角色和权限


设定用户具有admin,/insert,/delete权限ROLE_abc角色。


return new User(username,password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,ROLE_abc,/insert,/delete"));
复制代码


2.1.2控制页面显示效果


在页面中根据用户权限和角色判断页面中显示的内容


通过权限判断:
<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>



相关文章
|
4天前
|
前端开发 Java 开发者
Spring Boot 3 集成 Thymeleaf
Thymeleaf是一款用于Web和独立环境的现代化服务器端Java模板引擎。它能够处理HTML、XML、JavaScript、CSS甚至纯文本。Thymeleaf的语法简单易懂,它允许开发者在模板中嵌入表达式,以便动态地渲染数据。
80 1
Spring Boot 3 集成 Thymeleaf
|
4天前
|
SQL 前端开发 JavaScript
Spring Boot + Thymeleaf 使用PageHelper实现分页
Spring Boot + Thymeleaf 使用PageHelper实现分页
|
4天前
|
前端开发 JavaScript Java
防止spring把静态资源识别thymeleaf模板
防止spring把静态资源识别thymeleaf模板
13 0
|
4天前
|
安全 前端开发 Java
Spring Boot+Mybatis+Thymeleaf实现宠物医院管理系统
Spring Boot+Mybatis+Thymeleaf实现宠物医院管理系统
|
4天前
|
前端开发 Java Maven
spring boot 整合前端thymeleaf
spring boot 整合前端thymeleaf
27 0
|
4天前
|
SQL Java 关系型数据库
【Spring Boot+Thymeleaf+MyBatis+mysql】实现电子商务平台实战(附源码)持续更新~~ 包括sql语句、java、html代码
【Spring Boot+Thymeleaf+MyBatis+mysql】实现电子商务平台实战(附源码)持续更新~~ 包括sql语句、java、html代码
56 0
|
4天前
|
XML 前端开发 Java
Spring Boot的Web开发之Thymeleaf模板引擎的解析及使用(Thymeleaf的基础语法以及常用属性)
Spring Boot的Web开发之Thymeleaf模板引擎的解析及使用(Thymeleaf的基础语法以及常用属性)
68 0
|
6月前
|
JavaScript Java Spring
Spring Boot集成thymeleaf异步刷新页面
Spring Boot集成thymeleaf异步刷新页面
145 0
|
Java Spring
在Spring Boot中配置Thymeleaf的模板路径
在Spring Boot中配置Thymeleaf的模板路径
507 0
|
9月前
|
Java Spring
Spring Boot入门(十三) 之 thymeleaf一些常用语法规则
Spring Boot入门(十三) 之 thymeleaf一些常用语法规则