22.在thymeleaf中获取属性值
Spring Security 可以在一些视图技术中进行控制显示效果。例如:jsp 或者 Thymeleaf。
在非前后端分离且使用Spring Boot的项目中多使用 thymeleaf 作为视图展示技术。
1.引入依赖
thymeleaf 对 spring security 的支持都放在 thymeleaf-extras-springsecurityX(版本)中,目前最新版本为5。所以需要在项目中添加此jar包的依赖和thymeleaf的依赖。
<!--thymeleaf springsecurity5 依赖-->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<!--thymeleaf 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.创建查询相关属性的页面
在html页面中引入thymeleaf命名空间和security命名空间。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
<title>Title</title>
</head>
登录账号:<span sec:authentication="name"></span><br>
登录账号:<span sec:authentication="principal.username"></span><br>
凭证:<span sec:authentication="credentials"></span><br>
权限和角色:<span sec:authentication="authorities"></span><br>
客户端地址:<span sec:authentication="details.remoteAddress"></span><br>
sessionId:<span sec:authentication="details.sessionId"></span><br>
</html>
3.controller层中写入页面跳转
@RequestMapping("demo")
public String demo() {
return "demo";
}
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 Eternal Night
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果