微信分销网站开发,云服务器是什么意思,网站开发服务费分录,毕业设计做网站可以用模版吗目录
SpringBoot-Web应用-路由响应
SpringBoot-数据库应用-Mybatis
SpringBoot-模版引擎-Thymeleaf
思维导图 Java知识点 功能#xff1a;数据库操作#xff0c;文件操作#xff0c;序列化数据#xff0c;身份验证#xff0c;框架开发#xff0c;第三方库使用等. 框架…目录
SpringBoot-Web应用-路由响应
SpringBoot-数据库应用-Mybatis
SpringBoot-模版引擎-Thymeleaf
思维导图 Java知识点 功能数据库操作文件操作序列化数据身份验证框架开发第三方库使用等. 框架库MyBatisSpringMVCSpringBootShiroLog4jFastJson等 技术ServletListenFilterInterceptorJWTAOP反射机制待补充 安全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项目 修改服务器URLhttps://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请求的访问路由带参数名nameRequestMapping(value /xiaodiget_g, method RequestMethod.GET)//GetMapping(value /xiaodiget)public String get_g(RequestParam String name) {return get test name;}// 指定POST请求的访问路由带参数名nameRequestMapping(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 标签中添加以下内容 dependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion2.2.2/version/dependencydependencygroupIdcom.mysql/groupIdartifactIdmysql-connector-j/artifactIdscoperuntime/scope/dependency 项目配置数据库连接信息
默认配置信息文件为 src/main/resources/ 目录下的 application.properties这里手动改为了 application.yml然后清空文件内容写入下面代码
spring:datasource:url: jdbc:mysql://localhost:3306/demo01username: rootpassword: 123456driver-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 快捷键 altinsert, 或者右键生成 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;}Overridepublic 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 ListUser findAll(Integer id);Select(select * from admin where id1)public ListUser 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 {Autowiredprivate UserMapper UserMapper;GetMapping(/getadmin)public ListUser getadmindata(RequestParam Integer id){ListUser all UserMapper.findAll(id);return all;}GetMapping(/getid)public ListUser getadminid(){ListUser 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; //langen index-en}}resources/templates/index.html
!DOCTYPE html
html xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8titleTitle/title
/head
body
span th:text${data}小迪安全/span
/body
/html
resources/templates/index-en.html
!DOCTYPE html
html langen
headmeta charsetUTF-8titleTitle/title
/head
body/body
/html
application.properties
# 应用服务 WEB 访问端口
server.port8080
# THYMELEAF (ThymeleafAutoConfiguration)
# 开启模板缓存默认值 true
spring.thymeleaf.cachetrue
# 检查模板是否存在然后再呈现
spring.thymeleaf.check-templatetrue
# 检查模板位置是否正确默认值 :true
spring.thymeleaf.check-template-locationtrue
#Content-Type 的值默认值 text/html
spring.thymeleaf.content-typetext/html
# 开启 MVC Thymeleaf 视图解析默认值 true
spring.thymeleaf.enabledtrue
# 模板编码
spring.thymeleaf.encodingUTF-8
# 要被排除在解析之外的视图名称列表⽤逗号分隔
spring.thymeleaf.excluded-view-names
# 要运⽤于模板之上的模板模式。另⻅ StandardTemplate-ModeHandlers( 默认值 HTML5)
spring.thymeleaf.modeHTML5
# 在构建 URL 时添加到视图名称前的前缀默认值 classpath:/templates/
spring.thymeleaf.prefixclasspath:/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)思维导图