springboot+thymeleaf+springSecurity 页面获取后端对象值

十一 Thymeleaf语法详解-获取作用域对象中的数据 @Controller public class DemoController { @RequestMapping("/showInfo5") public String showInfo5(HttpServletRequest request, Model model) { request.setAttribute("req", "HttpServletRequest"); req... 阅读详情

thymeleaf模板引擎使用过程中或许会有的问题,获取值的方式也是其中之一。

那么我就简单记录一下我在使用过程中遇到并解决问题所使用的方法。希望可以帮到大家!

首先讲一下页面的写法,实现使用了thymeleaf的话,那么thymeleaf的xmls是需要的;

<!doctype html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">

然后页面就可以使用thymeleaf的th:标签属性了!

前端页面获取没有什么歧义,大家应该都知道,例如

<span th:text="${data.id}"></span>

其中要注意的是url的赋值需要用到@{},例如

<a th:href="@{${data.url}}" target="_blank" th:text="${data.name}"></a>

那么,JS怎么获取呢?重点来了,那就是“[[]]”,例如

<script th:inline="javascript">
    var id = [[${data.id}]];
</script>

异步请求的时候需要写url也是一样,需要用到"[[]]";例如

<script type="text/javascript">
	var ajaxUrl = "[[@{/test/path}]]";
</script>

使用spring-security是怎么获取session中的用户信息呢?

还有一些朋友会集成安全框架,比如spring的spring-security安全框架,使用这个框架的时候要获取后端session中存储的用户信息怎么获取呢?是这样的:

<span th:text="${session.SPRING_SECURITY_CONTEXT.authentication.userInfo.stUsnm}"></span>

这样就取到了对应session中存放的用户信息,当然,我后端是这样存放的:

我有一个userinfo的class(当然,大家可以扩展自己需要的信息)

public class UserInfo {

    private String cdUs;
    
    private String stLgnm;
    
    private String stLgps;
    
    private String stLgpsWjm;
    
    private String stUsnm;
    
    private String cdDp;
    
    private String stDpnm;
    
    private String cdRl;
    
    private String stRonm;
    
    private String nmFlag;
    
    public String getStLgpsWjm() {
        return stLgpsWjm;
    }
    public void setStLgpsWjm(String stLgpsWjm) {
        this.stLgpsWjm = stLgpsWjm;
    }
    public String getNmFlag() {
        return nmFlag;
    }
    public void setNmFlag(String nmFlag) {
        this.nmFlag = nmFlag;
    }
    public String getCdUs() {
        return cdUs;
    }
    public void setCdUs(String cdUs) {
        this.cdUs = cdUs;
    }
    public String getStLgnm() {
        return stLgnm;
    }
    public void setStLgnm(String stLgnm) {
        this.stLgnm = stLgnm;
    }
    public String getStLgps() {
        return stLgps;
    }
    public void setStLgps(String stLgps) {
        this.stLgps = stLgps;
    }
    public String getStUsnm() {
        return stUsnm;
    }
    public void setStUsnm(String stUsnm) {
        this.stUsnm = stUsnm;
    }
    public String getCdDp() {
        return cdDp;
    }
    public void setCdDp(String cdDp) {
        this.cdDp = cdDp;
    }
    public String getStDpnm() {
        return stDpnm;
    }
    public void setStDpnm(String stDpnm) {
        this.stDpnm = stDpnm;
    }
    public String getCdRl() {
        return cdRl;
    }
    public void setCdRl(String cdRl) {
        this.cdRl = cdRl;
    }
    public String getStRonm() {
        return stRonm;
    }
    public void setStRonm(String stRonm) {
        this.stRonm = stRonm;
    }    
}

然后,我继承了security的UsernamePasswordAuthenticationToken类作为数据的存储(这个也可以在里面扩展自己需要的与用户有关联的一些信息)

public class SecurityUser extends UsernamePasswordAuthenticationToken {
    
    private static final long serialVersionUID = 1L;
    
    private UserInfo userInfo;

    public UserInfo getUserInfo() {
        return userInfo;
    }
    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }
    public static long getSerialversionuid() {
        return serialVersionUID;
    }
    public SecurityUser(String username, String password, Collection<? extends GrantedAuthority> authorities) {
        super(username, password, authorities);
    }
    public SecurityUser(String username, String password, UserInfo userinfo, Collection<? extends GrantedAuthority> authorities) {
        super(username, password, authorities);
        this.userInfo = userinfo;
    }
}

再然后,我实现了AuthenticationProvider接口的验证信息,并存放用户的信息

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private HttpSession session;

    @Autowired
    private UserService userService;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        
        String username = authentication.getName();
        String password = authentication.getCredentials().toString();
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();

        // 检查用户名密码是否正确
        UserInfo user = userService.loadUserByUsernameAndPassword(username, MD5Util.encryption(password));
        if (user == null) {
            logger.error("{} 登录失败", username);
            throw new BadCredentialsException("用户名或密码错误!");
        } else if ("1".equals(user.getNmFlag())) { //验证是否可用
            throw new AccountExpiredException("用户已锁定!");
        }
        user.setStLgpsWjm(password);
        Authentication auth = new SecurityUser(username, password, user, grantedAuthorities);
        return auth;
    }
    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }

}

这样一来,验证通过后,前端页面就可以通过session.SPRING_SECURITY_CONTEXT.authentication.userInfo.stUsnm获取到了。聪明的朋友应该都看出来了,userInfo对应的是我SecurityUser的class中的用户信息属性。

当然,js中也可以获取

<script th:inline="javascript">
	   var loginUser = [[${session.SPRING_SECURITY_CONTEXT.authentication.userInfo}]];
</script>

这样就获取到了对应的登录用户的信息

SpringBoot+SpringSecurity+thymeleaf 代码实现 SpringBoot+SpringSecurity+thymeleaf 代码实现最近这两天在学习spring security 学的心累的不要不要的,终于在今天让我成功的写了一个成功案例,开心。并且在学习的过程中找到了一个springsecurity4官方文档的中文版,写的非常棒(个人感觉) 传送门:没错我就是传送门放代码之前说下环境: 1.开发工具 idea 2.项目集成工具 gradle 2 阅读详情

相关推荐

动力节点springsecurity笔记-SpringSecurity 集成thymeleaf

上一讲里面我们集成了thymeleaf实现在页面链接的动态判断是否显示,那么在实际开发中,我们会遇到有验证码的功能,那么如何处理呢?复制上一个工程springsecurity-13-thymeleaf ,修改名字为springsecurity-14-captcha见《base64及jwt学习文档.doc》

Java___interview的博客 906

Spring Boot中使用thymeleaf以及各种取值,判断,选择,截取等方式

在Spring Boot 1.5.9.RELEASE版本中,默认的Thymeleaf版本为2.1.6.RELEASE版本,这里推荐使用3.0以上版本。在Spring Boot中,默认的html页面地址为src/main/resources/templates,默认的静态资源地址为src/main/resources/static。Spring Boot支持FreeMarker、Groovy、Thymeleaf和Mustache四种模板解析引擎,官方推荐使用Thymeleaf。checkbox选中。

weixin_2630663675的博客 2558

保姆级教程:在RK3588开发板上编译带MPP硬件加速的FFmpeg(含完整依赖库配置)

本文提供在RK3588开发板上编译支持MPP硬件加速的FFmpeg的详细教程,涵盖交叉编译环境搭建、MPP框架及依赖库配置,最终实现4K视频硬件编解码性能优化。通过实战步骤和性能对比,展示如何充分发挥RK3588的硬件加速能力。

weixin_30839881的博客 418

thymleaf 里获取表格某一行的 id 值

本篇主要介绍在 thymleaf 里获取表格某一行的值,如 id。

qq_43881663的博客 1216

mstar PQ软件修改方法

1.修改ADC的值 Static code APIXC_AdcGainOffsetSetting tADCSetting[ADC_SET_NUMS]= #define INIT_ADC_RGB_RED_GAIN               0x1000 #define INIT_ADC_RGB_GREEN_GAIN           0x1000 #define INIT_ADC_RGB

qq_18810259的专栏 2565

SurfaceControlViewHost使用介绍

可以看到,主要用途:是把一个进程的View显示到另外一个进程里面。

chadm.xiang 1287

thymeleaf标签获取后台数据

不多bb,代码如下: 后台 model.addAttribute("a",a);//a是对象 model.addAttribute("b", b); model.addAttribute("c", b); model.addAttribute("d", d); model.addAttribute("e", e); return new ModelAndView("usercenter/usercenter",model); 前端 <p th:html="'+${a.userName}+

ACBEGF的博客 1572

openEuler 20.03 LTS面向RK3399移植系列(3)—openEuler 20.03 LTS面向Firefly RK3399的第一个版本(Linux kernel 4.4)

任务动机:完成openEuler面向RK3399开发板的移植。 任务描述:按照固件烧写准备,固件获取,固件打包和烧写的步骤实现openEuler面向RK3399开发板的移植,整理过程和相关命令记录到文档。 1. 制作openEuler的rootfs.img 1.1 开发环境 树莓派--openEuler系统 openEuler 20.03 LTS 的内测版本镜像,下载。 1.2 具体操作 1.2.1 ssh远程连接树莓派 确定树莓派IP ssh连接 1.2.2 制作rootfs.img

火种源码的机器人技术专栏 2369

thymeleaf获取id值_Thymeleaf系列三 调用对象的成员变量值、Map值、List值、属性的方法 、ctx对象、param、session和application...

1. 概述本文会对thymeleaf以下功能进行举例说明:调用对象的成员变量的属性调用Map的对象的属性调用List的对象的属性调用属性的方法使用ctx对象param:获取request的请求参数session:获取session的属性值application:获取application的属性值2. 主代码2.1. 公共类本文的代码会使用到Thymeleaf系列二 Thymeleaf的标准表达式语...

weixin_30925435的博客 996

全志Longan和Tina系统T113-S3-(Tina Linux 系统介绍)

Tina Linux是全志科技基于Linux内核开发的针对智能硬件类产品的嵌入式软件系统。Tina Linux基于openwrt-14.07 版本的软件开发包,包含了 Linux 系统开发用到的内核源码、驱动、工具、系统中间件与应用程序包。Tina系统软件架构如图所示。从下至上分别为Kernel && Driver、Libraries、System Services、Applications 四层。

szembed的博客 6339

comsol如何实现五厚度的单元模拟?

🏆本文收录于 《全栈 Bug 调优(实战版)》 专栏。专栏聚焦真实项目中的各类疑难 Bug,从成因剖析 → 排查路径 → 解决方案 → 预防优化全链路拆解,形成一套可复用、可沉淀的实战知识体系。无论你是初入职场的开发者,还是负责复杂项目的资深工程师,都可以在这里构建一套属于自己的「问题诊断与性能调优」方法论,助你稳步进阶、放大技术价值 。

**My Coding Family** 471

【SpringSecurity】七、SpringSecurity集成thymeleaf

/这里别用RestController了,不再返回一个json对象或者普通字符串了/*** 跳转到登陆页面*/@RequestMapping("/toLogin") //GET、POST都行的意思上面的这个return "login"字符串,是返回thymeleaf的逻辑视图名,物理视图 = 前缀 + 逻辑视图 + 后缀,即/templates/ + login + .html(点住application.yaml文件中thymeleaf的配置查看源码:/*** 登录成功后进入主页*/

llg___的博客 1720

强化学习从基础到进阶-案例与实践[4]:深度Q网络-DQN、double DQN、经验回放、rainbow、分布式DQN

强化学习从基础到进阶-案例与实践[4]:深度Q网络-DQN、double DQN、经验回放、rainbow、分布式DQN

丨汀、的博客 2066

springboot+springSecurity+thymeleaf整合

在这里咱们做个springboot+springSecurity+thymeleaf的整合,才用的框架是springboot+mybatisPlus,所有的用户数据,角色数据,权限数据均来自于数据库。 1 项目结构和数据库结构 为了便于了解整个项目结构,先看下项目结构和数据库结构 1.1 项目结构 1.2 数据库结构 建表和数据语句如下: CREATE DATABASE /*!32312 IF NOT EXISTS*/`user_db` /*!40100 DEFAULT CHARACTER SET ut

一个人的江湖 1963

Far planner代码系列(2)

graph_decoder系列(2) 承接上文 我们接着来看decoder_node.cpp的代码部分 void GraphDecoder::Init() { /* initialize subscriber and publisher */ //graph_sub_ 订阅节点/robot_vgraph 其类型是visibility_graph_msg/Graph的消息 graph_sub_ = nh.subscribe("/robot_vgrap...

博客 1534

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

thymeleaf使用springsecurity标签

所需依赖 springboot版本为2.0.7 <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity4</artifactId> <version>3.0.2....

qq_37813806的博客 1293

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

【SpringBoot】SpringBoot + SpringSecurity + Thymeleaf 整合

在 SpringBoot 工程中,借用 SpringSecurity 权限框架来对登录用户所拥有的不同的权限来显示不同的页面。并且,如果有用户已登录,则右上角显示用户名和其角色。如下图: admin 用户权限最大,显示所有页面内容: zzc 用户只有两个角色权限,所以,只显示部分内容: 没有用户登录时,页面显示如下: 【首页】、【登录】下方的内容都没有显示。 好了,需求已经了解清楚了,那咱们就直接上代码了哈。【文末有源码】 【开发环境】: IDEA-2020.2 SpringBoot-2.5.5

sco5282的博客 2165

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

记一次使用Springboot+Thymeleaf模板插件+SpringSecurity进行页面跳转

记一次使用Springboot+Thymeleaf模板插件+SpringSecurity进行页面跳转 首先:搭建Springboot项目(此处省略) 引入pom依赖 <!-- thymeleaf模板插件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-

qq_42957424的博客 476

基于springboot+thymeleaf+springsecurity搭建一套web小案例

用于springboot 整合thymeleaf + springsecurity 框架做认证,实现小项目的入门指导作用。

冬阳 1332

两种方法实现SpringBoot项目在Thymeleaf中获取Spring Security当前用户的信息

第一种方法:使用扩展包获取用户信息 使用方法 1、首先 引入Thymeleaf和SpringSecurity的扩展依赖: 我的SpringBoot版本是2.2.6.RELEASE 其默认引入的的SpringSecurity版本是5.2.2.RELEASE 因此 我选择5版本的扩展依赖包 <dependency> <groupId>org.thymeleaf.extras&...

"✧treasure mountain✧" 9730

【SpringBoot】SpringBoot整合SpringSecurity+thymeleaf实现认证授权(配置对象版)

**Spring Security 是 Spring 家族中的一个安全管理框架,Spring Security 的两大核心功能就是`认证(authentication)和授权(authorization)`。** - 认证 :你是什么人。 - 授权 :你能做什么。 - 用户 :主要包括用户名称、用户密码和当前用户所拥有的角色信息,可用于实现`认证`操作。 - 角色 :主要包括角色名称、角色描述和当前角色所拥有的权限信息,可用于实现`授权`操作。

墩墩分墩 3059

springboot+springsecurity+thymeleaf+mybatis整合

springboot+springsecurity+thymeleaf+mybatis整合 首先我们要考虑的是如何将springsecurity自带的用户类UserDetails 与我们项目的用户类绑定起来 方式一:重写UserDetailsService的loadUserByUsername,通过在数据库查寻到的数据为springsecurity自带的用户类UserDetails赋值,参考SpringSecurity使用 中的“通过数据库授权” 方式二:就是让系统的用户类继承springsecurity自

桀骜浮沉的博客 680

springboot+layui+thymeleaf+springsecurity 简单整合

1.项目结构: 2.pom依赖: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="...

he_yanghui的博客 1202

快速上手SpringSecurity:SpringBoot + Thymeleaf 入门案例推荐

快速上手SpringSecurity:SpringBoot + Thymeleaf 入门案例推荐 去发现同类优质开源项目:https://gitcode.com/ 项目介绍 在现代Web应用开发中,权限管理是不可或缺的一部分。为了帮助开发者快速掌握并整合权限管理框架,我们推出了一个基于 SpringBoot、SpringSecurity 和 Thymeleaf 的入门案例源代码。这个项目不仅适合初...

gitblog_09817的博客 923

springboot使用thymeleaf 和 springSecurity中出现的部分问题以及解决

springboot使用thymeleaf 和 springSecurity中出现的部分问题以及解决

y13698742的博客 855

SpringBoot++SpringSecurity+Thymeleaf简单学习

1.pom.xml文件 2.源码分析 3.问题分析 3.1找不到html文件 问题描述:浏览器访问 localhost:8000 没有跳转到index默认页面 问题分析:使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,或者html,配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容,所以无法跳转到HTML页面 解决办法:使用了@RestController注解,应该使用@Con

weixin_46056331的博客 851
上一篇: tomcat+maven实现热部署配置
下一篇: linux(centos)上的oracle11.2.0.4安装
异常的昵称
博客等级 码龄10年 18粉丝 17原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

异常的昵称

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值