Spring Boot Security(二)

Spring Security 配合 Thymeleaf 使用

注:Thymeleaf H5标注头:

xmlns:th="https://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security"

且 pom.xml 中

<!-- thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- thymeleaf html5 注解提示 --> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency> <!--- end -->

一、登录页面

1、记住我

  • 在 Spring Security 中开启 rememberMe;
  • 登录验证表单中需携带 remember-me。

2、退出登录

  • 在 Spring Security 中进行登出配置

    // 登出配置 http.logout() .logoutUrl("/logout") .invalidateHttpSession(true) .logoutSuccessUrl("/login") .permitAll() .and() .csrf() .disable();
  • 向 logoutUrl 发送 post 请求即可

二、主页面

3、针对不同用户(权限)显示不同界面,利用 thymeleaf 模板引擎来实现

<div sec:authorize="!isAuthenticated()"><!--未认证用户--> 请先登录<a href="/login">登录</a> </div> <div sec:authorize="isAuthenticated()"><!--已认证用户--> <h3><span sec:authentication="name"></span><span sec:authentication="principal.authorities"></span>你好!</h3> </div> <div sec:authorize="hasRole('ADMIN')"> <h3>您有ADMIN权限</h3> </div> <div sec:authorize="hasRole('USER')"> <h3>您有ADMIN权限</h3> </div>

__EOF__

本文作者ZOLMK
本文链接https://www.cnblogs.com/zolmk/p/14074227.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zolmk  阅读(107)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示