news 2026/4/23 17:16:59

【038-安全开发篇】JavaEE应用SpringBoot框架MyBatis注入Thymeleaf模版注入

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【038-安全开发篇】JavaEE应用SpringBoot框架MyBatis注入Thymeleaf模版注入

思维导图


知识点:

1、JavaEE-SpringBoot-WebAPP&路由
2、JavaEE-SpringBoot-Mybatis&注入
3、JavaEE-SpringBoot-Thymeleaf&SSTI

章节点

3、Java:
功能:数据库操作,文件操作,序列化数据,身份验证,框架开发,第三方库使用等.
框架库:MyBatis,SpringMVC,SpringBoot,Shiro,Log4j,FastJson等
技术:Servlet,Listen,Filter,Interceptor,JWT,AOP,反射机制待补充
安全:SQL注入,RCE执行,反序列化,脆弱验证,未授权访问,待补充
安全:原生开发安全,第三方框架安全,第三方库安全等,待补充

演示案例: SpringBoot-Web应用-路由响应 SpringBoot-数据库应用-Mybatis SpringBoot-模版引擎-Thymeleaf

Spring Boot是由Pivotal团队提供的一套开源框架,可以简化spring应用的创建及部署。它提供了丰富的Spring模块化支持,可以帮助开发者更轻松快捷地构建出企业级应用。Spring Boot通过自动配置功能,降低了复杂性,同时支持基于JVM的多种开源框架,可以缩短开发时间,使开发更加简单和高效。

SpringBoot-Web应用-路由响应

参考:https://springdoc.cn/spring-boot/
1、路由映射
@RequestMapping @GetMapping
2、参数传递
@RequestParam
3、数据响应
@RestController @Controller
@RestController注解相当于@ResponseBody+@Controller合在一起的作用。

@RestControllerpublicclassHelloController{//无参数访问响应@RequestMapping("/xiaodi")publicStringhello(){return"hello xiaodi";}//无参数指向GET方法访问响应@RequestMapping(value="/get",method=RequestMethod.GET)publicStringhelloGet(){return"hello get xiadi";}//有参数指向GET方法访问响应@RequestMapping(value="/getp",method=RequestMethod.GET)publicStringhellogetp(Stringname){return"hello get "+name;}//有参数指向POST方法访问响应@RequestMapping(value="/getpost",method=RequestMethod.POST)publicStringhelloGetParameters(Stringname){return"hello POST "+name;}}

SpringBoot-数据库应用-Mybatis

操作步骤:

0、数据库先创建需操作的数据

1、项目添加Mybatis&数据库驱动

-pom.xml

<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.2</version></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency>

2、项目配置数据库连接信息
-application.yml

spring:datasource:url:jdbc:mysql://localhost:3306/demo01 username:root password:123456driver-class-name:com.mysql.cj.jdbc.Driver

3、创建User类用来操作数据库数据
-com.example.demo.entity.User

set get toString方法

4、创建Mapper动态接口代理类实现
-com.example.demo.mapper.UserMapper

@MapperpublicinterfaceUserMapper{@Select("select * from admin where id like '%${id}%'")//模糊查询,有安全危险publicList<User>findAll(Integerid);}

5、创建Controller实现Web访问调用
-com.example.demo.controller.UserController

@RestControllerpublicclassUserController{@AutowiredprivateUserMapperuserMapper;@RequestMapping(value="/getdata",method=RequestMethod.GET)//@ResponseBodypublicList<User>getdata(Integerid){List<User>all=userMapper.findAll(id);System.out.println(all);returnall;}}

可以在csdn搜索mybatis中SQL注入的文章,虽然有这种漏洞的可能性小,但是还是有可能

SpringBoot-模版引擎-Thymeleaf

  • 不安全的模版版本
    日常开发中:语言切换页面,主题更换等传参导致的SSTI注入安全问题(只有3.0.0到3.0.11版本有,后面就被修复了)
    漏洞参考:https://mp.weixin.qq.com/s/NueP4ohS2vSeRCdx4A7yOg

配置application.properties指向模版页面
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

@RequestMapping(value="/")publicStringindex(Modelmodel){model.addAttribute("data","hello xiaodi");return"index";}@GetMapping("/path")publicStringthymeleaf(Stringlang){returnlang;}<?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="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.springframework</groupId><artifactId>java-spring-thymeleaf</artifactId><version>1.0</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><!--latest--><version>2.2.0.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency></dependencies><properties><java.version>1.8</java.version></properties><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 14:26:07

LobeChat能否生成API文档?程序员省时利器

LobeChat能否生成API文档&#xff1f;程序员省时利器 在微服务架构盛行的今天&#xff0c;一个中等规模的后端系统往往包含数十甚至上百个API接口。每当接口变更&#xff0c;开发团队就得手动更新文档——这种重复劳动不仅耗时&#xff0c;还容易遗漏关键字段或示例。更尴尬的是…

作者头像 李华
网站建设 2026/4/23 14:28:01

Langflow RAG 技术架构与实现深度解析

Langflow RAG 技术架构与实现深度解析 在大语言模型&#xff08;LLM&#xff09;应用快速落地的今天&#xff0c;如何高效构建稳定、可解释、可维护的 RAG&#xff08;检索增强生成&#xff09;系统&#xff0c;已成为开发者面临的核心挑战之一。传统方式依赖大量胶水代码串联组…

作者头像 李华
网站建设 2026/4/22 21:15:48

AutoDL部署Langchain-Chatchat 0.3.0全指南

AutoDL部署Langchain-Chatchat 0.3.0全指南 在当前大模型应用爆发的背景下&#xff0c;越来越多开发者希望搭建属于自己的本地知识库问答系统。而 Langchain-Chatchat 凭借其完整的私有文档处理闭环、对主流推理框架的良好支持以及出色的中文语义理解能力&#xff0c;已经成为…

作者头像 李华
网站建设 2026/4/18 11:55:34

Lostlife2.0下载官网风险提示及防范措施

YOLO技术的安全边界&#xff1a;当“一键部署”变成系统后门 在智能制造车间的边缘服务器上&#xff0c;一个看似普通的AI视觉检测任务正悄然失控——摄像头画面正常输出&#xff0c;检测结果也准确无误&#xff0c;但后台日志却显示设备频繁连接境外IP地址。这不是科幻剧情&am…

作者头像 李华
网站建设 2026/4/23 15:46:18

达梦数据库实战指南:从环境搭建到核心操作

作为国产数据库“四朵金花”之一&#xff0c;达梦数据库&#xff08;DM8&#xff09;凭借高兼容性、高可用性及全栈信创适配能力&#xff0c;已成为政务、金融、能源等关键领域国产化替代的核心选型。对于后端开发者而言&#xff0c;掌握达梦数据库的实战操作&#xff0c;是应对…

作者头像 李华