qidao123.com技术社区-IT企服评测·应用市场

标题: 【Java安全】底子配置篇-01 [打印本页]

作者: 宁睿    时间: 2024-7-20 19:34
标题: 【Java安全】底子配置篇-01
Java安全-01


  
前置底子

基于SpringBoot框架睁开开发,起首要相识一下整个项目的分层布局
从上到下:


实战代码布局:

实在最外层紧张分为两块:

进入到内层:



这里的API层 就相当于Controller层

Controller层只会从前端吸收数据,然后进行数据转发到Service层,不做具体的操纵

一些参数的判断验证,在Controller层进行验证,好比想上传文件的话,可以先在Controller层验证文件名后缀

小demo开发记录

文件上传

为了方便整个系统的开发,我们要规范化文件上传的接口,放在Service层中,保证可以在任何界面都可以去调用并进行上传
起首是Controller层
  1. package com.example.thymeleaf.controller;
  2. import com.example.thymeleaf.service.UploadService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.*;
  5. import org.springframework.web.multipart.MultipartFile;
  6. import java.io.IOException;
  7. import org.springframework.web.servlet.mvc.support.RedirectAttributes;
  8. @RestController
  9. @RequestMapping("/upload")
  10. public class UploadController {
  11.     //解释一下自动装配的功能:使得控制器类能够使用服务类的方法而无需手动创建服务类的实例
  12.     @Autowired
  13.     private UploadService service;
  14.     @GetMapping
  15.     public String showUploadForm() {
  16.         return "<!DOCTYPE html>"
  17.                 + "<html><head><title>File Upload</title></head>"
  18.                 + "<body><h1>Upload a File</h1>"
  19.                 + "<form method="POST" enctype="multipart/form-data" action="/upload">"
  20.                 + "<input type="file" name="file" />"
  21.                 + "<button type="submit">Upload</button>"
  22.                 + "</form></body></html>";
  23.     }
  24.     @PostMapping
  25.     public String handleFileUpload(@RequestParam("file") MultipartFile file,
  26.                                    RedirectAttributes redirectAttributes) {
  27.         try {
  28.             String fileName = service.uploadFile(file);
  29.             return "You successfully uploaded '" + fileName + "'";
  30.         } catch (IOException e) {
  31.             e.printStackTrace();
  32.             return "Failed to upload '" + file.getOriginalFilename() + "'";
  33.         }
  34.     }
  35. }
复制代码
路径是/upload 直接访问是get请求 会展示表单

点击upload上传文件,是post请求 会上传文件
Service层:
  1. package com.example.thymeleaf.service;
  2. import org.springframework.web.multipart.MultipartFile;
  3. import java.io.IOException;
  4. public interface UploadService {
  5.     String uploadFile(MultipartFile file) throws IOException;
  6. }
复制代码
具体方法实现:
  1. package com.example.thymeleaf.service.impl;
  2. import com.example.thymeleaf.service.UploadService;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.stereotype.Service;
  5. import org.springframework.web.multipart.MultipartFile;
  6. import java.io.File;
  7. import java.io.IOException;
  8. @Service
  9. public class UploadServiceImpl implements UploadService {
  10.     @Value("${file.upload-dir}")
  11.     private String uploadDir;
  12.     @Override
  13.     public String uploadFile(MultipartFile file) throws IOException{
  14.         if(file.isEmpty()){
  15.             throw new IOException("Failed to upload empty file.");
  16.         }
  17.         File dest = new File(uploadDir + "/" + file.getOriginalFilename());
  18.         file.transferTo(dest); //将上传的文件保存到指定的目标文件。
  19.         return file.getOriginalFilename();
  20.     }
  21. }
复制代码
其中记得在配置文件中指定一下目录


课程学习记录

初始配置阶段

学习守则:学一个东西前,先本身列一个学习门路,有目的有方向一步一步来

0x00 微服务阶段汗青

all in one:所有功能
微服务架构:把功能进行分区存储,对某个功能
相识Spring家族:

Spring Boot:构建一切
Spring Cloud:协调一切
Spring Cloud Data Flow:连接一切
0x01 第一个SpringBoot程序

玩一下,修改banner
https://www.bootschool.net/ascii-art/search

在source目录下 创建banner.txt文件即可

6 乐成了
0x02 原理初探

主动配置:
pom.xml:

启动器:

主程序:
  1. //@SpringBootApplication : 解释一下这个注解 这个是标注这个是Springboot的一个应用  如果没有 直接整个崩掉
  2. // 作用就是:启动类下的所有资源被导入
  3. @SpringBootApplication
  4. public class BeginnerApplication {
  5.     public static void main(String[] args) {
  6.         //将Springboot启动
  7.         SpringApplication.run(BeginnerApplication.class, args);
  8.     }
  9. }
复制代码
解释一下其中的注解,点进去读源码


  1. @SpringBootConfiguration : Springboot的配置
  2.                 @Configuration:  spring配置类
  3.                 @Component : 说明这是一个spring的组件
  4.                
  5. @EnableAutoConfiguration : 自动配置
  6.                 @AutoConfigurationPackage : 自动配置包
  7.                         @Import({Registrar.class}) : 导入选择器   注册
  8.                 @Import({AutoConfigurationImportSelector.class})
复制代码
0x03 SpringBoot配置文件

一样寻常操纵 起首删除主动天生的application.properties
然后新建一个配置文件application.yaml 修改下后缀 名字不需要改变
   先容一下两者的区别:
  
  yaml底子语法:
  1. #普通的key-value
  2. name: qinjing
  3. #对象
  4. student:
  5.         name: qinjiang
  6.         age: 3
  7.        
  8. #行内写法
  9. student: {name: qinjiang,age: 3}
  10. #数组  两个空格
  11. pets:
  12.   - cat
  13.   - dog
  14.   - pig
  15.    
  16. pets: [cat, dog, pig]
复制代码
之以是使用yaml 是因为可以给实体类赋值
0x04 给属性赋值的几种方法

起首可以使用注解

来test里面测试一下

第二种方法:在yaml配置文件中赋值 核心

留意之以是能注入乐成,还需要设置一些配置

上面爆红 不影响工作 这是去yaml中找person 去注入数据
可玩性:直接写一些占位符


表达式

如果前面这个参数存在 则读取参数 否则直接是hello


疏松绑定:

留意一下就好 以后看yaml文件 如果看到横杠分隔 知道怎么个事就好

第三种方法 配置properties


嘿嘿 乐成

properties防止乱码小本领:

0x05 JSR303校验

在字段加一层过滤器验证,保证数据的合法性
使用方法

留意肯定要在pom文件中配置
  1. <!--验证数据的依赖-->
  2. <dependency>
  3.     <groupId>org.springframework.boot</groupId>
  4.     <artifactId>spring-boot-starter-validation</artifactId>
  5. </dependency>
复制代码
可以修改默认报错语句


jsr303 校验语句

0x06 多环境配置

在配置application.properties时 有默认环境 测试环境 上线环境 但是逐个修改很麻烦 可以直接进行多环境配置

下面凸显yaml的多文档模式的便捷

分割线就是划分文件
0x07 再探主动配置原理

学这个的目的 是让我们更会写内容 在思索中写配置 而不是看文档背


遇到什么 看什么 配置就是在pom中加一个starter启动即可
开发阶段

0x00 SpringBoot Web开发

目录布局:
在resources中 static放静态资源 templates放模版

导入静态资源

前提引入maven
  1. <dependency>
  2.     <groupId>org.webjars</groupId>
  3.     <artifactId>jquery</artifactId>
  4.     <version>3.4.1</version>
  5. </dependency>
复制代码
访问乐成拿下




乐成访问public文件夹下的内容 留意: 优先级 按这个顺序 第一个就是在当前目录下 第二个指还可以创建一个resources文件夹

一样寻常风俗:

   总结:
    首页和图标定制

首页 index.html

直接访问 非常完美

0x01 模版引擎


先解释一下这个东西的作用

简单来说就是在前端界面预留位置 然后结合后端的数据 进行渲染 天生终极呈现给用户的界面

jsp
freemarker
Thymeleaf

导入依赖
  1. <!-- Thymelead  基于3.x开发-->
  2. <dependency>
  3.     <groupId>org.thymeleaf</groupId>
  4.     <artifactId>thymeleaf-spring5</artifactId>
  5. </dependency>
  6. <dependency>
  7.     <groupId>org.thymeleaf.extras</groupId>
  8.     <artifactId>thymeleaf-extras-java8time</artifactId>
  9. </dependency>
复制代码
默认放在templates目录下 后缀名是html

   结论:
  只要需要使用thymeleaf 只需要导入对应的依赖就可以
  我们将html放在templates目录下即可
  使用方法如下:在Controller层引已往
  1. @RestController
  2. public class HelloController {
  3.     @GetMapping("/hello")
  4.     public String hello(){
  5.         return "hello, world!";
  6.     }
  7. }
复制代码
此外肯定要掌握研讨 读源码的本领 要不然就会被直接公司招新人 你就被淘汰
  声明定名空间 在html文件中 添加后面这一句xm…
  1. <html lang="en" xmlns:th="http://www.thymeleaf.org">
复制代码
所有的html元素都可以被thymeleaf接受: 即th:元素名
小demo:
Controller层:

html:

效果:

0x02 Thymeleaf语法

   取变量:${…}
  取URL:@{…}
  文本转义:text
不转义:utext
demo:


效果:

检查一下网页代码 可以发现尖括号被转义了 这也就提供了防范xss的思绪


起首在Controller层中写个数组
  1. model.addAttribute("users", Arrays.asList("happySu", "is", "your dad"));
复制代码
然后再html中修改一下 提供两种赋值方法
  1. <!DOCTYPE html><html lang="en" xmlns:th="http://www.thymeleaf.org">
  2. <head>    <meta charset="UTF-8">    <title>Title</title></head><body><h1>111</h1><div th:text="${msg}"></div><div th:utext="${msg}"></div><!--方法一: ${}都是在取变量的值  这是循环遍历users变量 赋值给user  先遍历出来  然后后面text取值 取刚刚遍历出来的变量user的值进行输出--><h3 th:each="user:${users}" th:text="${user}"></h3><!--方法二: 行内输出  两个中括号包裹起来就行--><h3 th:each="user:${users}">[[ ${user} ]]</h3></body></html>
复制代码
效果

0x03 SpringMVC配置原理

@Configuration注解:表示是一个配置类
总结:在Springboot中,有非常多的xxxConfiguration帮助我们进行扩展配置 看到后要留意嗷

学完底子的一点内容,后面睁开一个项目的团体开发,期待吧~

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 qidao123.com技术社区-IT企服评测·应用市场 (https://dis.qidao123.com/) Powered by Discuz! X3.4