日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

thymeleaf 判断页面不显示_Thymeleaf 中 Spring Security 的使用 0151

發布時間:2024/1/23 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 thymeleaf 判断页面不显示_Thymeleaf 中 Spring Security 的使用 0151 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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-spri ngsecurity5">

1 獲取屬性

可 以 在 html 頁 面 中 通 過 sec:authentication="" 獲 取

UsernamePasswordAuthenticationToken 中所有 getXXX 的內容,包含父

類中的 getXXX 的內容。

根據源碼得出下面屬性:

? name:登錄賬號名稱

? principal:登錄主體,在自定義登錄邏輯中是 UserDetails

? credentials:憑證

? authorities:權限和角色

? details:實際上是 WebAuthenticationDetails 的實例。可以獲取

remoteAddress(客戶端 ip)和 sessionId(當前 sessionId)

1.1實現步驟:

1.1.1 新建 demo.html

在項目 resources 中新建 templates 文件夾,在 templates 中新建

demo.html 頁面

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,/de lete"));

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> 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的thymeleaf 判断页面不显示_Thymeleaf 中 Spring Security 的使用 0151的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。