【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合在一起的作用。

java 复制代码
@RestController
public class HelloController {
    //无参数访问响应
    @RequestMapping("/xiaodi")
    public String hello() {
        return "hello xiaodi";
    }
    //无参数指向GET方法访问响应
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    public String helloGet(){
        return "hello get xiadi";
    }
    //有参数指向GET方法访问响应
    @RequestMapping(value = "/getp",method = RequestMethod.GET)
    public String hellogetp(String name){
        return "hello get "+name;
    }
    //有参数指向POST方法访问响应
    @RequestMapping(value = "/getpost",method = RequestMethod.POST)
    public String helloGetParameters(String name){
        return "hello POST "+name;
    }
}

SpringBoot-数据库应用-Mybatis

操作步骤:

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

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

-pom.xml

java 复制代码
<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

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

3、创建User类用来操作数据库数据

-com.example.demo.entity.User

java 复制代码
set get toString方法

4、创建Mapper动态接口代理类实现

-com.example.demo.mapper.UserMapper

java 复制代码
@Mapper
public interface UserMapper {
    @Select("select * from admin where id   like '%${id}%'")//模糊查询,有安全危险
    public List<User> findAll(Integer id);
}

5、创建Controller实现Web访问调用

-com.example.demo.controller.UserController

java 复制代码
@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @RequestMapping(value = "/getdata",method = RequestMethod.GET)
    //@ResponseBody
    public List<User> getdata(Integer id) {
        List<User> all = userMapper.findAll(id);
        System.out.println(all);
        return all;
    }
}

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

SpringBoot-模版引擎-Thymeleaf

配置application.properties指向模版页面

spring.thymeleaf.prefix=classpath:/templates/

spring.thymeleaf.suffix=.html

java 复制代码
@RequestMapping(value = "/")
public String index(Model model) {
    model.addAttribute("data","hello xiaodi");
    return "index";
}
@GetMapping("/path")
public String thymeleaf(String lang) {
    return lang;
}

<?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>
相关推荐
独泪了无痕6 小时前
Spring Data Redis 快速上手
spring boot·redis·后端
Thomas.Sir9 小时前
第9课:SpringBoot4.x Web开发基础:WebMvc自动配置、静态资源、请求参数接收
java·spring boot
EnCi Zheng11 小时前
后端怎么把AI接口稳定安全低成本接进业务:超时重试、密钥隔离与成本熔断清单
人工智能·后端·安全·架构·api
Smoothcloud润云11 小时前
GPU远程纳管安全吗?最小权限、网络边界与日志审计实战
服务器·网络·人工智能·安全·gpu算力·gpu
猫猫.15312 小时前
HashTable、HashMap、ConcurrentHashMap 核心区别总结
java·开发语言·java-ee
长不大的杨瑞杰12 小时前
OpenCode v2 MCP 与插件体系实战:把数据库、工单和内部 API 安全接入 Agent
人工智能·安全·语言模型·软件工程·1024程序员节
VimorphTech13 小时前
一段字幕太长,怎样拆句并安排显示时间?
java·spring boot
万联WANFLOW13 小时前
Kimi K3.1 Pro 接口接入指南:模型选型、费用计算与零数据保留
java·spring boot·python·安全·spring·大模型
安教安全员通关宝13 小时前
2026年浙江安全员B证考试题库+答案+解析
安全
aiks_yuan14 小时前
Spring AI 2.1 ChatMemory 已过时用什么替代:迁移到 Session Memory 实战(JDBC 存储与会话压缩)
spring boot