Git;Java;SQL添加完成

This commit is contained in:
Bunny 2025-02-19 17:25:42 +08:00
parent 7661180ffe
commit d09f51c0c1
45 changed files with 509 additions and 213 deletions

14
src/note/Git/README.md Normal file
View File

@ -0,0 +1,14 @@
---
title: Git笔记
index: true
icon: iconoir:git
headerDepth: 4
category:
- 笔记
- 记录
- 学习
- 问题
- Git
---
<Catalog />

View File

@ -1,4 +1,11 @@
---
icon: ri:java-fill
---
# JavaStream
## 创建Stream
### Stream.builder()创建
```java
Stream<Integer> build = Stream.<Integer>builder()

14
src/note/Java/README.md Normal file
View File

@ -0,0 +1,14 @@
---
title: Java笔记
index: false
icon: devicon:java-wordmark
headerDepth: 4
category:
- 笔记
- 记录
- 学习
- 问题
- Java
---
<Catalog />

View File

@ -1,3 +1,5 @@
# Java返回Result
```java
import com.alibaba.fastjson2.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;

View File

@ -1,3 +1,5 @@
# PageResult 类
```java
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;

View File

@ -1,3 +1,18 @@
---
title: ResultUtils通用类
index: false
icon: vscode-icons:file-type-class
category:
- 笔记
- 记录
- 学习
- 问题
- Java
- utils
dir:
order: 5
---
```java
import com.alibaba.fastjson2.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;

View File

@ -1,3 +1,5 @@
# Result 类
```java
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;

View File

@ -1,3 +1,5 @@
# ResultEnum 统一结果状态类
```java
import lombok.Getter;

View File

@ -1,3 +1,5 @@
# Ant风格访问
Spring MVC 支持 **Ant 风格**Ant-style path matching主要体现在 URL 路径匹配模式上。这种模式是 Spring Web 框架用来进行 URL 模式匹配的一种方式,借用了 Apache Ant一个流行的 Java 构建工具中的路径匹配规则。Ant 风格的路径匹配使得 URL 路径映射更加灵活和方便。
+ `*` 匹配单一路径层级中的任意字符。

View File

@ -1,3 +1,20 @@
---
title: Spring笔记
index: true
icon: devicon:spring
category:
- 笔记
- 记录
- 学习
- 问题
- Java
- Spring
dir:
order: 1
---
+ 公共AI网站[https://chatgptplus.cn/](https://chatgptplus.cn/)
+ vue3官网[https://cn.vuejs.org/](https://cn.vuejs.org/)
+ SpringBoot官网[https://docs.spring.io/spring-boot/index.html](https://docs.spring.io/spring-boot/index.html)

View File

@ -1,68 +1,78 @@
## <font style="color:rgb(0, 0, 0);">Spring 表单验证</font>在 Spring MVC 中,表单验证是通过一系列的注解来完成的。
### `<font style="color:rgb(0, 0, 0);">@NotNull</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:确保字段值不为空。</font>
+ **用法**<font style="color:rgb(0, 0, 0);">:用于字段、方法参数或返回值上,表示该字段不能为空。如果字段为空,将验证失败并返回相应的错误信息。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
# Spring 表单验证
## Spring 表单验证在 Spring MVC 中,表单验证是通过一系列的注解来完成的。
### `@NotNull`
- **作用**:确保字段值不为空。
- **用法**:用于字段、方法参数或返回值上,表示该字段不能为空。如果字段为空,将验证失败并返回相应的错误信息。
- 示例
```java
@NotNull(message = "用户名不能为空")
private String username;
```
### `<font style="color:rgb(0, 0, 0);">@NotEmpty</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:确保字段不为空,并且不为一个空字符串。</font>
+ **用法**<font style="color:rgb(0, 0, 0);">:用于字符串、集合等类型,验证字段不仅不能为空,而且不能为空字符串。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
### `@NotEmpty`
- **作用**:确保字段不为空,并且不为一个空字符串。
- **用法**:用于字符串、集合等类型,验证字段不仅不能为空,而且不能为空字符串。
- 示例
```java
@NotEmpty(message = "密码不能为空")
private String password;
```
### `<font style="color:rgb(0, 0, 0);">@NotBlank</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:确保字段不为空,并且不为一个空白字符串(即非空白字符)。</font>
+ **用法**<font style="color:rgb(0, 0, 0);">:类似于 </font>`<font style="color:rgb(0, 0, 0);">@NotEmpty</font>`<font style="color:rgb(0, 0, 0);">,但除了不为空,还要求去除空白字符后不能为零长度。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
### `@NotBlank`
- **作用**:确保字段不为空,并且不为一个空白字符串(即非空白字符)。
- **用法**:类似于 `@NotEmpty`,但除了不为空,还要求去除空白字符后不能为零长度。
- 示例
```java
@NotBlank(message = "电子邮件不能为空")
private String email;
```
### `<font style="color:rgb(0, 0, 0);">@Size(min, max)</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:验证字段的大小,适用于字符串、集合、数组等类型。</font>
+ **用法**<font style="color:rgb(0, 0, 0);">:可以设置最小值和最大值来限制字段的长度或集合的大小。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
### `@Size(min, max)`
- **作用**:验证字段的大小,适用于字符串、集合、数组等类型。
- **用法**:可以设置最小值和最大值来限制字段的长度或集合的大小。
- 示例
```java
@Size(min = 6, max = 20, message = "密码长度必须在6到20之间")
private String password;
```
### `<font style="color:rgb(0, 0, 0);">@Email</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:验证字段是否符合有效的电子邮件格式。</font>
+ **用法**<font style="color:rgb(0, 0, 0);">:用于验证字符串字段是否为有效的电子邮件地址格式。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
### `@Email`
- **作用**:验证字段是否符合有效的电子邮件格式。
- **用法**:用于验证字符串字段是否为有效的电子邮件地址格式。
- 示例
```java
@Email(message = "请输入有效的电子邮件地址")
private String email;
```
### `<font style="color:rgb(0, 0, 0);">@Pattern(regexp)</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:根据正则表达式验证字段值。</font>
+ **用法**<font style="color:rgb(0, 0, 0);">:可以根据自定义的正则表达式来验证字段的内容。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
### `@Pattern(regexp)`
- **作用**:根据正则表达式验证字段值。
- **用法**:可以根据自定义的正则表达式来验证字段的内容。
- 示例
```java
@Pattern(regexp = "^\\d{10}$", message = "请输入有效的手机号码")
private String phoneNumber;
```
### `<font style="color:rgb(0, 0, 0);">@Min(value)</font>`** 和 **`<font style="color:rgb(0, 0, 0);">@Max(value)</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:确保数字类型字段的值在指定范围内。</font>
+ **用法**<font style="color:rgb(0, 0, 0);"></font>`<font style="color:rgb(0, 0, 0);">@Min</font>`<font style="color:rgb(0, 0, 0);"> 用于验证值是否大于等于指定的最小值,</font>`<font style="color:rgb(0, 0, 0);">@Max</font>`<font style="color:rgb(0, 0, 0);"> 用于验证值是否小于等于指定的最大值。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
### `@Min(value)` **和** `@Max(value)`
- **作用**:确保数字类型字段的值在指定范围内。
- **用法**`@Min` 用于验证值是否大于等于指定的最小值,`@Max` 用于验证值是否小于等于指定的最大值。
- 示例
```java
@Min(value = 18, message = "年龄不能小于18岁")
@ -70,158 +80,173 @@ private String phoneNumber;
private int age;
```
### `<font style="color:rgb(0, 0, 0);">@DecimalMin(value)</font>`** 和 **`<font style="color:rgb(0, 0, 0);">@DecimalMax(value)</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:用于验证浮动值是否在指定范围内,类似于 </font>`<font style="color:rgb(0, 0, 0);">@Min</font>`<font style="color:rgb(0, 0, 0);"></font>`<font style="color:rgb(0, 0, 0);">@Max</font>`<font style="color:rgb(0, 0, 0);">,但适用于 </font>`<font style="color:rgb(0, 0, 0);">BigDecimal</font>`<font style="color:rgb(0, 0, 0);"></font>`<font style="color:rgb(0, 0, 0);">Double</font>`<font style="color:rgb(0, 0, 0);"> 类型的数值。</font>
+ **用法**<font style="color:rgb(0, 0, 0);"></font>`<font style="color:rgb(0, 0, 0);">@DecimalMin</font>`<font style="color:rgb(0, 0, 0);"> 验证值是否大于等于指定的最小值,</font>`<font style="color:rgb(0, 0, 0);">@DecimalMax</font>`<font style="color:rgb(0, 0, 0);"> 验证值是否小于等于指定的最大值。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
### `@DecimalMin(value)` **和** `@DecimalMax(value)`
- **作用**:用于验证浮动值是否在指定范围内,类似于 `@Min``@Max`,但适用于 `BigDecimal``Double` 类型的数值。
- **用法**`@DecimalMin` 验证值是否大于等于指定的最小值,`@DecimalMax` 验证值是否小于等于指定的最大值。
- 示例
```java
@DecimalMin(value = "0.0", inclusive = true, message = "价格不能小于0")
private BigDecimal price;
```
### `<font style="color:rgb(0, 0, 0);">@Future</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:验证日期字段的值是否为将来日期。</font>
+ **用法**<font style="color:rgb(0, 0, 0);">:用于 </font>`<font style="color:rgb(0, 0, 0);">java.util.Date</font>`<font style="color:rgb(0, 0, 0);"></font>`<font style="color:rgb(0, 0, 0);">java.time.LocalDate</font>`<font style="color:rgb(0, 0, 0);"></font>`<font style="color:rgb(0, 0, 0);">java.time.LocalDateTime</font>`<font style="color:rgb(0, 0, 0);"> 等日期类型的字段。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
### `@Future`
- **作用**:验证日期字段的值是否为将来日期。
- **用法**:用于 `java.util.Date`、`java.time.LocalDate` 或 `java.time.LocalDateTime` 等日期类型的字段。
- 示例
```java
@Future(message = "日期必须是未来的时间")
private LocalDate eventDate;
```
### `<font style="color:rgb(0, 0, 0);">@Past</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:验证日期字段的值是否为过去的日期。</font>
+ **用法**<font style="color:rgb(0, 0, 0);">:类似于 </font>`<font style="color:rgb(0, 0, 0);">@Future</font>`<font style="color:rgb(0, 0, 0);">,但是验证日期必须是过去的时间。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
### `@Past`
- **作用**:验证日期字段的值是否为过去的日期。
- **用法**:类似于 `@Future`,但是验证日期必须是过去的时间。
- 示例
```java
@Past(message = "出生日期必须是过去的时间")
private LocalDate birthDate;
```
### `<font style="color:rgb(0, 0, 0);">@AssertTrue</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:验证字段值是否为 </font>`<font style="color:rgb(0, 0, 0);">true</font>`<font style="color:rgb(0, 0, 0);"></font>
+ **用法**<font style="color:rgb(0, 0, 0);">:适用于布尔类型字段,如果值不是 </font>`<font style="color:rgb(0, 0, 0);">true</font>`<font style="color:rgb(0, 0, 0);">,则验证失败。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
### `@AssertTrue`
- **作用**:验证字段值是否为 `true`
- **用法**:适用于布尔类型字段,如果值不是 `true`,则验证失败。
- 示例
```java
@AssertTrue(message = "必须接受条款和条件")
private boolean acceptedTerms;
```
### `<font style="color:rgb(0, 0, 0);">@AssertFalse</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:验证字段值是否为 </font>`<font style="color:rgb(0, 0, 0);">false</font>`<font style="color:rgb(0, 0, 0);"></font>
+ **用法**<font style="color:rgb(0, 0, 0);">:适用于布尔类型字段,如果值不是 </font>`<font style="color:rgb(0, 0, 0);">false</font>`<font style="color:rgb(0, 0, 0);">,则验证失败。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
### `@AssertFalse`
- **作用**:验证字段值是否为 `false`
- **用法**:适用于布尔类型字段,如果值不是 `false`,则验证失败。
- 示例
```java
@AssertFalse(message = "不能接受条款")
private boolean declinedTerms;
```
### `<font style="color:rgb(0, 0, 0);">@Valid</font>`** 和 **`<font style="color:rgb(0, 0, 0);">@Validated</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:触发嵌套对象的验证。</font>
+ **用法**<font style="color:rgb(0, 0, 0);">:当你有嵌套对象(如表单中的对象属性是另一个对象),使用 </font>`<font style="color:rgb(0, 0, 0);">@Valid</font>`<font style="color:rgb(0, 0, 0);"></font>`<font style="color:rgb(0, 0, 0);">@Validated</font>`<font style="color:rgb(0, 0, 0);"> 注解来递归验证该对象。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
### `@Valid` **和** `@Validated`
- **作用**:触发嵌套对象的验证。
- **用法**:当你有嵌套对象(如表单中的对象属性是另一个对象),使用 `@Valid``@Validated` 注解来递归验证该对象。
- 示例
```java
@Valid
private Address address;
```
### `<font style="color:rgb(0, 0, 0);">@Digits(integer, fraction)</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:验证数字字段的有效性,确保字段值是一个有效的数字,并且整数部分和小数部分的位数符合指定要求。</font>
+ **用法**<font style="color:rgb(0, 0, 0);"></font>`<font style="color:rgb(0, 0, 0);">integer</font>`<font style="color:rgb(0, 0, 0);"> 参数用于指定数字的整数部分的最大位数,</font>`<font style="color:rgb(0, 0, 0);">fraction</font>`<font style="color:rgb(0, 0, 0);"> 参数用于指定小数部分的最大位数。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
### `@Digits(integer, fraction)`
- **作用**:验证数字字段的有效性,确保字段值是一个有效的数字,并且整数部分和小数部分的位数符合指定要求。
- **用法**`integer` 参数用于指定数字的整数部分的最大位数,`fraction` 参数用于指定小数部分的最大位数。
- 示例
```java
@Digits(integer = 5, fraction = 2, message = "金额应为最大5位整数和2位小数")
private BigDecimal amount;
```
- <font style="color:rgb(0, 0, 0);">这个例子验证金额字段的最大值为 </font>`<font style="color:rgb(0, 0, 0);">99999.99</font>`<font style="color:rgb(0, 0, 0);">即最多5位整数和2位小数</font>
- 这个例子验证金额字段的最大值为 `99999.99`即最多5位整数和2位小数
### `<font style="color:rgb(0, 0, 0);">@CreditCardNumber</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:验证信用卡号的有效性,确保其符合信用卡的常见格式,通常包括 Luhn 算法的验证。</font>
+ **用法**<font style="color:rgb(0, 0, 0);">:该注解用于验证信用卡号的格式是否有效。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
### `@CreditCardNumber`
- **作用**:验证信用卡号的有效性,确保其符合信用卡的常见格式,通常包括 Luhn 算法的验证。
- **用法**:该注解用于验证信用卡号的格式是否有效。
- 示例
```java
@CreditCardNumber(message = "请输入有效的信用卡号")
private String creditCardNumber;
```
- <font style="color:rgb(0, 0, 0);">该注解会根据常见的信用卡规则(如 VISA、MasterCard 等)验证输入的信用卡号是否合法。</font>
- 该注解会根据常见的信用卡规则(如 VISA、MasterCard 等)验证输入的信用卡号是否合法。
### `<font style="color:rgb(0, 0, 0);">@Range(min, max)</font>`<font style="color:rgb(0, 0, 0);">(不是 Spring 内置的,但通常来自 Hibernate Validator</font>
+ **作用**<font style="color:rgb(0, 0, 0);">:验证字段值是否在指定的范围内。常用于 </font>`<font style="color:rgb(0, 0, 0);">Integer</font>`<font style="color:rgb(0, 0, 0);"></font>`<font style="color:rgb(0, 0, 0);">Long</font>`<font style="color:rgb(0, 0, 0);"></font>`<font style="color:rgb(0, 0, 0);">Double</font>`<font style="color:rgb(0, 0, 0);"> 等数值类型的字段。</font>
+ **用法**<font style="color:rgb(0, 0, 0);">:指定字段的有效范围,当值不在范围内时会验证失败。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
### `@Range(min, max)`(不是 Spring 内置的,但通常来自 Hibernate Validator
- **作用**:验证字段值是否在指定的范围内。常用于 `Integer`、`Long`、`Double` 等数值类型的字段。
- **用法**:指定字段的有效范围,当值不在范围内时会验证失败。
- 示例
```java
@Range(min = 1, max = 100, message = "数字必须在1到100之间")
private int quantity;
```
- <font style="color:rgb(0, 0, 0);">该注解会验证 </font>`<font style="color:rgb(0, 0, 0);">quantity</font>`<font style="color:rgb(0, 0, 0);"> 字段的值是否在 </font>`<font style="color:rgb(0, 0, 0);">1</font>`<font style="color:rgb(0, 0, 0);"></font>`<font style="color:rgb(0, 0, 0);">100</font>`<font style="color:rgb(0, 0, 0);"> 之间。</font>
- 该注解会验证 `quantity` 字段的值是否在 `1``100` 之间。
### `<font style="color:rgb(0, 0, 0);">@URL</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:验证字段是否为有效的 URL 格式。</font>
+ **用法**<font style="color:rgb(0, 0, 0);">:用于字符串类型的字段,验证其是否符合有效的 URL 格式。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
### `@URL`
- **作用**:验证字段是否为有效的 URL 格式。
- **用法**:用于字符串类型的字段,验证其是否符合有效的 URL 格式。
- 示例
```java
@URL(message = "请输入有效的网址")
private String website;
```
### `<font style="color:rgb(0, 0, 0);">@Valid</font>`** 与 **`<font style="color:rgb(0, 0, 0);">@Validated</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:用于嵌套对象的验证,确保嵌套对象的字段也进行验证。</font>
+ **用法**<font style="color:rgb(0, 0, 0);">:这两个注解会触发嵌套对象的验证,通常用于嵌套的复杂表单数据结构。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
### `@Valid` **与** `@Validated`
- **作用**:用于嵌套对象的验证,确保嵌套对象的字段也进行验证。
- **用法**:这两个注解会触发嵌套对象的验证,通常用于嵌套的复杂表单数据结构。
- 示例
```java
@Valid
private Address address;
```
- <font style="color:rgb(0, 0, 0);">如果 </font>`<font style="color:rgb(0, 0, 0);">Address</font>`<font style="color:rgb(0, 0, 0);"> 类中有字段使用了验证注解,</font>`<font style="color:rgb(0, 0, 0);">@Valid</font>`<font style="color:rgb(0, 0, 0);"> 会递归地验证 </font>`<font style="color:rgb(0, 0, 0);">Address</font>`<font style="color:rgb(0, 0, 0);"> 对象的所有字段。</font>
- 如果 `Address` 类中有字段使用了验证注解,`@Valid` 会递归地验证 `Address` 对象的所有字段。
### `<font style="color:rgb(0, 0, 0);">@FutureOrPresent</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:验证日期或时间字段的值是否是当前日期(包括今天)或未来的日期。</font>
+ **用法**<font style="color:rgb(0, 0, 0);">:该注解用于日期和时间字段,确保其为今天或将来的日期。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font><font style="color:rgb(0, 0, 0);"></font>
### `@FutureOrPresent`
- **作用**:验证日期或时间字段的值是否是当前日期(包括今天)或未来的日期。
- **用法**:该注解用于日期和时间字段,确保其为今天或将来的日期。
- 示例:
```java
@FutureOrPresent(message = "事件日期必须是今天或将来")
private LocalDate eventDate;
```
### `<font style="color:rgb(0, 0, 0);">@PastOrPresent</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:验证日期或时间字段的值是否是当前日期(包括今天)或过去的日期。</font>
+ **用法**<font style="color:rgb(0, 0, 0);">:与 </font>`<font style="color:rgb(0, 0, 0);">@FutureOrPresent</font>`<font style="color:rgb(0, 0, 0);"> 相反,确保字段是过去或今天的日期。</font>
+ <font style="color:rgb(0, 0, 0);"></font>
### `@PastOrPresent`
- **作用**:验证日期或时间字段的值是否是当前日期(包括今天)或过去的日期。
- **用法**:与 `@FutureOrPresent` 相反,确保字段是过去或今天的日期。
- 示
```java
@PastOrPresent(message = "出生日期必须是过去的时间或今天")
private LocalDate birthDate;
```
### `<font style="color:rgb(0, 0, 0);">@Null</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:验证字段是否为 </font>`<font style="color:rgb(0, 0, 0);">null</font>`<font style="color:rgb(0, 0, 0);">。如果字段不为空,则验证失败。</font>
+ **用法**<font style="color:rgb(0, 0, 0);">:该注解可以用于字段或方法参数上,确保字段值必须为 </font>`<font style="color:rgb(0, 0, 0);">null</font>`<font style="color:rgb(0, 0, 0);"></font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
### `@Null`
- **作用**:验证字段是否为 `null`。如果字段不为空,则验证失败。
- **用法**:该注解可以用于字段或方法参数上,确保字段值必须为 `null`
- 示例
```java
@Null(message = "该字段必须为null")
private String nickname;
```
### `<font style="color:rgb(0, 0, 0);">@ScriptAssert(lang, script)</font>`
+ **作用**<font style="color:rgb(0, 0, 0);">:通过自定义脚本验证字段值。</font>
+ **用法**<font style="color:rgb(0, 0, 0);">:允许使用自定义脚本(如 JavaScript来执行复杂的验证逻辑。需要指定脚本语言和脚本内容。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
### `@ScriptAssert(lang, script)`
- **作用**:通过自定义脚本验证字段值。
- **用法**:允许使用自定义脚本(如 JavaScript来执行复杂的验证逻辑。需要指定脚本语言和脚本内容。
- 示例
```java
@ScriptAssert(lang = "javascript", script = "_this.password == _this.confirmPassword", message = "密码和确认密码必须一致")
@ -229,15 +254,15 @@ private String password;
private String confirmPassword;
```
- <font style="color:rgb(0, 0, 0);">这个注解可以用于检查两个字段值是否一致。</font>
- 这个注解可以用于检查两个字段值是否一致。
### `<font style="color:rgb(0, 0, 0);">@UniqueElements</font>`<font style="color:rgb(0, 0, 0);">Hibernate Validator 扩展)</font>
+ **作用**<font style="color:rgb(0, 0, 0);">:确保集合中的元素是唯一的,常用于 List 或 Set 类型字段。</font>
+ **用法**<font style="color:rgb(0, 0, 0);">:适用于集合类型字段,确保集合中的元素不重复。</font>
+ <font style="color:rgb(0, 0, 0);">示例</font>
### `@UniqueElements`Hibernate Validator 扩展)
- **作用**:确保集合中的元素是唯一的,常用于 List 或 Set 类型字段。
- **用法**:适用于集合类型字段,确保集合中的元素不重复。
- 示例
```java
@UniqueElements(message = "列表中的元素必须唯一")
private List<String> tags;
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

View File

@ -1,16 +1,22 @@
## <font style="color:rgb(0, 0, 0);">向application域共享数据</font>
# 向application域共享数据
## 向application域共享数据
在 Spring MVC 中,`Application` 域(也称为 **ServletContext**)是一个全局范围,用于在整个应用程序中共享数据。不同于 **Session** 域和 **Request** 域,`Application` 域中的数据对整个 Web 应用的所有用户都是可见的,因此适合存储全局共享的配置信息、常量、初始化数据等。
在 Spring MVC 中,我们可以通过 `ServletContext` 来向 **Application** 域共享数据,通常使用 `setAttribute``getAttribute` 方法来进行操作。
### 1. **通过 **`ServletContext`** 共享数据**
### 1. **通过** `ServletContext` **共享数据**
`ServletContext` 是与整个 Web 应用程序相关联的对象,它允许你在多个请求和多个会话之间共享数据。在 Spring MVC 中,可以通过以下两种方式来访问 `ServletContext`
+ 直接使用 `HttpServletRequest.getSession().getServletContext()` 获取 `ServletContext`
+ 使用 `@Autowired` 注解注入 `ServletContext` 对象。
- 直接使用 `HttpServletRequest.getSession().getServletContext()` 获取 `ServletContext`
- 使用 `@Autowired` 注解注入 `ServletContext` 对象。
#### 示例 1通过 `ServletContext` 共享数据
##### 1.1 通过 `HttpServletRequest` 获取 `ServletContext`
```java
@Controller
public class ApplicationController {
@ -42,10 +48,11 @@ public class ApplicationController {
**URL 请求:**
+ `GET /setApplicationData` 会向 `Application` 域中存储 `"appName": "SpringMVCApp"``"version": "1.0.0"`
+ `GET /getApplicationData` 会从 `Application` 域中读取数据,并返回给视图。
- `GET /setApplicationData` 会向 `Application` 域中存储 `"appName": "SpringMVCApp"``"version": "1.0.0"`
- `GET /getApplicationData` 会从 `Application` 域中读取数据,并返回给视图。
##### 1.2 通过 `@Autowired` 注入 `ServletContext`
```java
@Controller
public class ApplicationController {
@ -77,21 +84,25 @@ public class ApplicationController {
在这个例子中,`@Autowired` 注解会自动将 `ServletContext` 注入到控制器中。
### 2. **应用场景**
将数据放到 `Application` 域中通常用于存储以下类型的数据:
+ **应用级别的数据**:例如应用名称、版本号、初始化配置等,这些数据是全局共享的。
+ **常量和初始化信息**:如果有一些需要在多个请求中共享的常量或初始化信息,可以将它们放到 `Application` 域中。
+ **数据库连接池或常用资源**:对于一些全局共享的资源(如数据库连接池),可以在 `Application` 域中进行配置并在不同的请求中共享。
- **应用级别的数据**:例如应用名称、版本号、初始化配置等,这些数据是全局共享的。
- **常量和初始化信息**:如果有一些需要在多个请求中共享的常量或初始化信息,可以将它们放到 `Application` 域中。
- **数据库连接池或常用资源**:对于一些全局共享的资源(如数据库连接池),可以在 `Application` 域中进行配置并在不同的请求中共享。
### 3. **注意事项**
+ **全局共享**:与 `Session``Request` 域不同,`Application` 域中的数据对所有用户和请求都可见。因此,要特别小心在 `Application` 域中存储敏感数据,避免泄漏用户个人信息等。
+ **生命周期**`Application` 域中的数据在整个应用程序生命周期内有效,直到应用服务器重新启动。因此,放入 `Application` 域的数据一般是全局的、不会频繁变化的。
+ **线程安全**`Application` 域的数据是共享的,因此在并发访问时要考虑线程安全问题。如果有多个线程访问同一数据,可能需要进行同步。
- **全局共享**:与 `Session``Request` 域不同,`Application` 域中的数据对所有用户和请求都可见。因此,要特别小心在 `Application` 域中存储敏感数据,避免泄漏用户个人信息等。
- **生命周期**`Application` 域中的数据在整个应用程序生命周期内有效,直到应用服务器重新启动。因此,放入 `Application` 域的数据一般是全局的、不会频繁变化的。
- **线程安全**`Application` 域的数据是共享的,因此在并发访问时要考虑线程安全问题。如果有多个线程访问同一数据,可能需要进行同步。
### 4. **清理 Application 域中的数据**
如果不再需要某个共享数据,可以使用 `removeAttribute` 方法从 `Application` 域中移除该数据。
#### 示例:删除 `Application` 域中的数据
```java
@Controller
public class ApplicationController {
@ -110,12 +121,14 @@ public class ApplicationController {
**URL 请求:**
+ `GET /removeApplicationData` 会从 `Application` 域中移除 `appName``version` 属性。
- `GET /removeApplicationData` 会从 `Application` 域中移除 `appName``version` 属性。
### 5. **使用** `@ApplicationScope`**Spring 方式)**
### 5. **使用 **`@ApplicationScope`**Spring 方式)**
如果使用 Spring 框架进行开发,也可以使用 Spring 提供的 `@ApplicationScope` 注解来定义在整个应用范围内共享的 Bean。这种方法通常用于 Spring 组件,而不是直接操作 `ServletContext`
#### 示例:使用 `@ApplicationScope`
```java
@Component
@Scope("application")
@ -130,10 +143,7 @@ public class AppConfig {
在这种情况下Spring 管理的 Bean 会在应用级别共享,类似于 `ServletContext` 中存储的数据。
> + `Application` 域(即 `ServletContext`)用于在整个应用程序范围内共享数据,适合存储全局共享的信息、配置和常量。
> + 通过 `HttpServletRequest.getServletContext()``@Autowired` 注解可以访问 `ServletContext` 并向 `Application` 域中共享数据。
> + 数据存储在 `Application` 域中可以在整个应用程序生命周期内有效,适用于共享全局性的、无需频繁更新的数据。
> + 应谨慎存储敏感数据,并注意线程安全和数据的生命周期。
>
##
- `Application` 域(即 `ServletContext`)用于在整个应用程序范围内共享数据,适合存储全局共享的信息、配置和常量。
- 通过 `HttpServletRequest.getServletContext()``@Autowired` 注解可以访问 `ServletContext` 并向 `Application` 域中共享数据。
- 数据存储在 `Application` 域中可以在整个应用程序生命周期内有效,适用于共享全局性的、无需频繁更新的数据。
- 应谨慎存储敏感数据,并注意线程安全和数据的生命周期。

View File

@ -1,12 +1,17 @@
## <font style="color:rgb(0, 0, 0);">向session共享数据</font>
# Session共享数据
## 向session共享数据
在 Spring MVC 中,**Session** 是用于存储用户会话期间的数据的一种机制。每个用户访问的应用程序都将拥有一个唯一的会话。通过 `HttpSession`,可以在用户的会话中存储一些数据,直到用户关闭浏览器或会话过期。
<font style="color:rgb(0, 0, 0);">Spring MVC 提供了多种方式来与 </font>`<font style="color:rgb(0, 0, 0);">HttpSession</font>`<font style="color:rgb(0, 0, 0);"> 进行交互,下面详细介绍如何通过 </font>`<font style="color:rgb(0, 0, 0);">HttpSession</font>`<font style="color:rgb(0, 0, 0);"> 向 Session 共享数据。</font>
Spring MVC 提供了多种方式来与 `HttpSession` 进行交互,下面详细介绍如何通过 `HttpSession` 向 Session 共享数据。
### <font style="color:rgb(0, 0, 0);">1. </font>**通过 **`<font style="color:rgb(0, 0, 0);">HttpSession</font>`** 操作 Session 数据**
<font style="color:rgb(0, 0, 0);">在 Spring MVC 控制器中,您可以通过 </font>`<font style="color:rgb(0, 0, 0);">HttpSession</font>`<font style="color:rgb(0, 0, 0);"> 对象来存储和读取会话数据。</font>
### 1. **通过** `HttpSession` **操作 Session 数据**
在 Spring MVC 控制器中,您可以通过 `HttpSession` 对象来存储和读取会话数据。
#### 示例:将数据添加到 Session
#### <font style="color:rgb(0, 0, 0);">示例:将数据添加到 Session</font>
```java
@Controller
public class SessionController {
@ -33,13 +38,15 @@ public class SessionController {
**URL 请求:**
+ `<font style="color:rgb(0, 0, 0);">GET /setSessionData</font>`<font style="color:rgb(0, 0, 0);"> 会将数据 </font>`<font style="color:rgb(0, 0, 0);">"username": "JohnDoe"</font>`<font style="color:rgb(0, 0, 0);"></font>`<font style="color:rgb(0, 0, 0);">"age": 30</font>`<font style="color:rgb(0, 0, 0);"> 存储到 Session 中。</font>
+ `<font style="color:rgb(0, 0, 0);">GET /getSessionData</font>`<font style="color:rgb(0, 0, 0);"> 会从 Session 中获取并显示存储的值。</font>
- `GET /setSessionData` 会将数据 `"username": "JohnDoe"``"age": 30` 存储到 Session 中。
- `GET /getSessionData` 会从 Session 中获取并显示存储的值。
### <font style="color:rgb(0, 0, 0);">2. </font>**使用 **`<font style="color:rgb(0, 0, 0);">@SessionAttributes</font>`** 注解**
`<font style="color:rgb(0, 0, 0);">@SessionAttributes</font>`<font style="color:rgb(0, 0, 0);"> 注解用于将控制器中的某些模型属性放入 Session 中。这种方式比直接操作 </font>`<font style="color:rgb(0, 0, 0);">HttpSession</font>`<font style="color:rgb(0, 0, 0);"> 更为方便和简洁,特别是当需要共享多个模型属性时。</font>
### 2. **使用** `@SessionAttributes` **注解**
`@SessionAttributes` 注解用于将控制器中的某些模型属性放入 Session 中。这种方式比直接操作 `HttpSession` 更为方便和简洁,特别是当需要共享多个模型属性时。
#### 示例:使用 `@SessionAttributes`
#### <font style="color:rgb(0, 0, 0);">示例:使用 </font>`<font style="color:rgb(0, 0, 0);">@SessionAttributes</font>`
```java
@Controller
@SessionAttributes("user")
@ -64,15 +71,17 @@ public class UserController {
**URL 请求:**
+ `<font style="color:rgb(0, 0, 0);">GET /setUser</font>`<font style="color:rgb(0, 0, 0);"> 会将 </font>`<font style="color:rgb(0, 0, 0);">user</font>`<font style="color:rgb(0, 0, 0);"> 对象放入 Session 中。</font>
+ `<font style="color:rgb(0, 0, 0);">GET /getUser</font>`<font style="color:rgb(0, 0, 0);"> 会从 Session 中获取 </font>`<font style="color:rgb(0, 0, 0);">user</font>`<font style="color:rgb(0, 0, 0);"> 对象。</font>
- `GET /setUser` 会将 `user` 对象放入 Session 中。
- `GET /getUser` 会从 Session 中获取 `user` 对象。
`<font style="color:rgb(0, 0, 0);">@SessionAttributes</font>`<font style="color:rgb(0, 0, 0);"> 注解不仅可以放入 Session 中,还可以与 </font>`<font style="color:rgb(0, 0, 0);">@ModelAttribute</font>`<font style="color:rgb(0, 0, 0);"> 注解结合使用,确保模型数据保持在 Session 中。</font>
`@SessionAttributes` 注解不仅可以放入 Session 中,还可以与 `@ModelAttribute` 注解结合使用,确保模型数据保持在 Session 中。
### <font style="color:rgb(0, 0, 0);">3. </font>**使用 **`<font style="color:rgb(0, 0, 0);">@ModelAttribute</font>`** 注解**
`<font style="color:rgb(0, 0, 0);">@ModelAttribute</font>`<font style="color:rgb(0, 0, 0);"> 注解允许将数据放入模型中,并且在方法调用前通过 </font>`<font style="color:rgb(0, 0, 0);">Model</font>`<font style="color:rgb(0, 0, 0);"> 传递给视图。如果和 </font>`<font style="color:rgb(0, 0, 0);">@SessionAttributes</font>`<font style="color:rgb(0, 0, 0);"> 一起使用,它可以将属性直接添加到 </font>`<font style="color:rgb(0, 0, 0);">HttpSession</font>`<font style="color:rgb(0, 0, 0);"></font>
### 3. **使用** `@ModelAttribute` **注解**
`@ModelAttribute` 注解允许将数据放入模型中,并且在方法调用前通过 `Model` 传递给视图。如果和 `@SessionAttributes` 一起使用,它可以将属性直接添加到 `HttpSession`
#### 示例:使用 `@ModelAttribute``@SessionAttributes`
#### <font style="color:rgb(0, 0, 0);">示例:使用 </font>`<font style="color:rgb(0, 0, 0);">@ModelAttribute</font>`<font style="color:rgb(0, 0, 0);"></font>`<font style="color:rgb(0, 0, 0);">@SessionAttributes</font>`
```java
@Controller
@SessionAttributes("cart")
@ -102,13 +111,15 @@ public class CartController {
**URL 请求:**
+ `<font style="color:rgb(0, 0, 0);">GET /addToCart?item=Apple</font>`<font style="color:rgb(0, 0, 0);"> 会将 </font>`<font style="color:rgb(0, 0, 0);">Apple</font>`<font style="color:rgb(0, 0, 0);"> 添加到 </font>`<font style="color:rgb(0, 0, 0);">cart</font>`<font style="color:rgb(0, 0, 0);"> 中。</font>
+ `<font style="color:rgb(0, 0, 0);">GET /viewCart</font>`<font style="color:rgb(0, 0, 0);"> 会显示购物车中的内容。</font>
- `GET /addToCart?item=Apple` 会将 `Apple` 添加到 `cart` 中。
- `GET /viewCart` 会显示购物车中的内容。
### <font style="color:rgb(0, 0, 0);">4. </font>**通过 **`<font style="color:rgb(0, 0, 0);">@RequestParam</font>`** 或 **`<font style="color:rgb(0, 0, 0);">@PathVariable</font>`** 获取 Session 数据**
<font style="color:rgb(0, 0, 0);">如果在请求中需要通过路径变量或请求参数传递数据并存储到 Session 中,可以结合 </font>`<font style="color:rgb(0, 0, 0);">@RequestParam</font>`<font style="color:rgb(0, 0, 0);"></font>`<font style="color:rgb(0, 0, 0);">@PathVariable</font>`<font style="color:rgb(0, 0, 0);"> 来实现。</font>
### 4. **通过** `@RequestParam` **或** `@PathVariable` **获取 Session 数据**
如果在请求中需要通过路径变量或请求参数传递数据并存储到 Session 中,可以结合 `@RequestParam``@PathVariable` 来实现。
#### 示例:使用 `@RequestParam` 存储 Session 数据
#### <font style="color:rgb(0, 0, 0);">示例:使用 </font>`<font style="color:rgb(0, 0, 0);">@RequestParam</font>`<font style="color:rgb(0, 0, 0);"> 存储 Session 数据</font>
```java
@Controller
public class SessionController {
@ -130,13 +141,15 @@ public class SessionController {
**URL 请求:**
+ `<font style="color:rgb(0, 0, 0);">GET /setSession/JohnDoe</font>`<font style="color:rgb(0, 0, 0);"> 会将 </font>`<font style="color:rgb(0, 0, 0);">"username": "JohnDoe"</font>`<font style="color:rgb(0, 0, 0);"> 存储到 Session 中。</font>
+ `<font style="color:rgb(0, 0, 0);">GET /getSession</font>`<font style="color:rgb(0, 0, 0);"> 会从 Session 中获取并显示 </font>`<font style="color:rgb(0, 0, 0);">username</font>`<font style="color:rgb(0, 0, 0);"></font>
- `GET /setSession/JohnDoe` 会将 `"username": "JohnDoe"` 存储到 Session 中。
- `GET /getSession` 会从 Session 中获取并显示 `username`
### <font style="color:rgb(0, 0, 0);">5. </font>**删除 Session 数据**
<font style="color:rgb(0, 0, 0);">如果希望在某个操作后清除 Session 中的某些数据,可以使用 </font>`<font style="color:rgb(0, 0, 0);">HttpSession</font>`<font style="color:rgb(0, 0, 0);"> 提供的 </font>`<font style="color:rgb(0, 0, 0);">removeAttribute</font>`<font style="color:rgb(0, 0, 0);"> 方法。</font>
### 5. **删除 Session 数据**
如果希望在某个操作后清除 Session 中的某些数据,可以使用 `HttpSession` 提供的 `removeAttribute` 方法。
#### 示例:删除 Session 数据
#### <font style="color:rgb(0, 0, 0);">示例:删除 Session 数据</font>
```java
@Controller
public class SessionController {
@ -151,23 +164,26 @@ public class SessionController {
**URL 请求:**
+ `<font style="color:rgb(0, 0, 0);">GET /removeSessionData</font>`<font style="color:rgb(0, 0, 0);"> 会从 Session 中删除 </font>`<font style="color:rgb(0, 0, 0);">"username"</font>`<font style="color:rgb(0, 0, 0);"> 属性。</font>
- `GET /removeSessionData` 会从 Session 中删除 `"username"` 属性。
### <font style="color:rgb(0, 0, 0);">6. </font>**Session 过期与清理**
<font style="color:rgb(0, 0, 0);">默认情况下Spring MVC 的 </font>`<font style="color:rgb(0, 0, 0);">HttpSession</font>`<font style="color:rgb(0, 0, 0);"> 会话会在用户关闭浏览器后过期或者会话超时默认30分钟。可以在 </font>`<font style="color:rgb(0, 0, 0);">web.xml</font>`<font style="color:rgb(0, 0, 0);"> 或应用的配置类中设置会话超时:</font>
### 6. **Session 过期与清理**
默认情况下Spring MVC 的 `HttpSession` 会话会在用户关闭浏览器后过期或者会话超时默认30分钟。可以在 `web.xml` 或应用的配置类中设置会话超时:
#### 示例:设置 Session 超时
#### <font style="color:rgb(0, 0, 0);">示例:设置 Session 超时</font>
```xml
<session-config>
<session-timeout>30</session-timeout> <!-- 设置会话超时为30分钟 -->
</session-config>
```
### <font style="color:rgb(0, 0, 0);">7. </font>**通过 Spring 配置 Session**
<font style="color:rgb(0, 0, 0);">通过 Spring 配置文件或 Java 配置类,还可以控制 Session 的相关行为如会话过期时间、session 的持久化等)。</font>
### 7. **通过 Spring 配置 Session**
通过 Spring 配置文件或 Java 配置类,还可以控制 Session 的相关行为如会话过期时间、session 的持久化等)。
#### 示例Java 配置类
#### <font style="color:rgb(0, 0, 0);">示例Java 配置类</font>
```java
@Configuration
@EnableWebMvc
@ -180,15 +196,12 @@ public class WebConfig implements WebMvcConfigurer {
}
```
`<font style="color:rgb(0, 0, 0);">SessionInterceptor</font>`<font style="color:rgb(0, 0, 0);"> 可以用于监控和管理 Session 数据。</font>
`SessionInterceptor` 可以用于监控和管理 Session 数据。
> <font style="color:rgb(0, 0, 0);">在 Spring MVC 中,向 Session 共享数据主要有以下几种方式:</font>
>
> + `<font style="color:rgb(0, 0, 0);">HttpSession</font>`<font style="color:rgb(0, 0, 0);">:通过 </font>`<font style="color:rgb(0, 0, 0);">HttpSession</font>`<font style="color:rgb(0, 0, 0);"> 对象存储和读取 Session 数据。</font>
> + `<font style="color:rgb(0, 0, 0);">@SessionAttributes</font>`<font style="color:rgb(0, 0, 0);">:通过 </font>`<font style="color:rgb(0, 0, 0);">@SessionAttributes</font>`<font style="color:rgb(0, 0, 0);"> 注解将模型属性添加到 Session 中。</font>
> + `<font style="color:rgb(0, 0, 0);">@ModelAttribute</font>`<font style="color:rgb(0, 0, 0);">:结合 </font>`<font style="color:rgb(0, 0, 0);">@SessionAttributes</font>`<font style="color:rgb(0, 0, 0);"> 使用,将模型数据持久化到 Session 中。</font>
> + `<font style="color:rgb(0, 0, 0);">@RequestParam</font>`** 和 **`<font style="color:rgb(0, 0, 0);">@PathVariable</font>`<font style="color:rgb(0, 0, 0);">:将请求参数或路径变量存储到 Session 中。</font>
> + **Session 过期与清理**<font style="color:rgb(0, 0, 0);">:可以通过配置控制会话超时,或手动清除 Session 数据。</font>
>
在 Spring MVC 中,向 Session 共享数据主要有以下几种方式:
##
- `HttpSession`:通过 `HttpSession` 对象存储和读取 Session 数据。
- `@SessionAttributes`:通过 `@SessionAttributes` 注解将模型属性添加到 Session 中。
- `@ModelAttribute`:结合 `@SessionAttributes` 使用,将模型数据持久化到 Session 中。
- `@RequestParam` **和** `@PathVariable`:将请求参数或路径变量存储到 Session 中。
- **Session 过期与清理**:可以通过配置控制会话超时,或手动清除 Session 数据。

View File

@ -1,4 +1,7 @@
# 编写Spring应用
## `@SpringBootApplication` 注解
1. `@EnableAutoConfiguration`
- 这个注解让 Spring Boot 根据项目中的依赖,自动配置 Spring 应用程序。Spring Boot 提供了大量的自动配置支持,帮助我们省去手动配置许多常见的功能。
- 例如,如果你的项目中加入了 `spring-boot-starter-web` 依赖Spring Boot 会自动配置一个嵌入式的 Tomcat 服务器和一些常见的 Web 功能。

View File

@ -1,4 +1,7 @@
# 访问控制
## 访问控制
请求地址时返回对应的网页文件
+ `@RestController`用于返回对象格式的内容,在后面会使用`ModelAndView`可以返回网页文件
@ -39,7 +42,7 @@
### 错误页面设置
在这个路径下可以配置错误码要访问的页面,也就是可以自定义页面内容
![](./images/SpringMVC笔记/image-20250122171536808.png)![](https://cdn.nlark.com/yuque/0/2025/png/42943943/1739542831045-260da870-b941-47d6-a441-11a42e585f75.png)
![](./images/1739542831045-260da870-b941-47d6-a441-11a42e585f75.png)
### 使用`@Controller`
返回需要访问的HTML内容页面最后返回的字符串就是页面这个页面位于`templates`目录下

View File

@ -1,3 +1,5 @@
# 重定向和转发使用
在 Spring MVC 中,**重定向Redirect**和**转发Forward**是两种常见的请求处理方式它们分别用于不同的场景。Spring 提供了灵活的 API 来实现这两种请求方式。
### 1. **转发Forward**

View File

@ -0,0 +1,17 @@
---
title: SpringBoot笔记
index: true
icon: devicon:spring
category:
- 笔记
- 记录
- 学习
- 问题
- Java
- Spring
dir:
order: 2
---
## SpringBoot笔记

View File

@ -1,4 +1,5 @@
## Thymeleaf快速入门
# Thymeleaf快速入门
Thymeleaf 是一种现代化的 Java 模板引擎,广泛用于生成 HTML、XML、JavaScript 等内容。它有许多内置的指令和功能,用于渲染动态内容、条件渲染、循环、处理表达式等。以下是 Thymeleaf 中常见的指令和属性的详细介绍:
### 1. `th:text`

View File

@ -1,11 +1,31 @@
## <font style="color:rgb(0, 0, 0);">SpringSecurity</font>
---
title: SpringSecurity笔记
index: true
icon: devicon:spring
category:
- 笔记
- 记录
- 学习
- 问题
- Java
- Spring
headerDepth: 4
dir:
order: 3
---
## SpringSecurity笔记
### 密码转换器Password Encoder
Spring Security 提供了多种密码转换器Password Encoder这些转换器用于对用户密码进行加密和验证。常见的密码转换器包括
1. **BCryptPasswordEncoder**
- 使用 **BCrypt** 算法对密码进行加密。
- 是最常用的密码加密方案具有强大的加密性并且支持自动加盐salt防止暴力破解攻击。
- 示例:
- 使用 **BCrypt** 算法对密码进行加密。
- 是最常用的密码加密方案具有强大的加密性并且支持自动加盐salt防止暴力破解攻击。
- 示例:
```java
@Bean
@ -14,10 +34,11 @@ public PasswordEncoder passwordEncoder() {
}
```
2. **NoOpPasswordEncoder**
- 不对密码进行加密,直接返回明文密码。
- 主要用于开发和测试环境,**不推荐在生产环境中使用**。
- 示例:
1. **NoOpPasswordEncoder**
- 不对密码进行加密,直接返回明文密码。
- 主要用于开发和测试环境,**不推荐在生产环境中使用**。
- 示例:
```java
@Bean
@ -26,10 +47,11 @@ public PasswordEncoder passwordEncoder() {
}
```
3. **Pbkdf2PasswordEncoder**
- 使用 **PBKDF2** 算法进行密码加密。
- 提供较强的安全性,并且支持对密码进行哈希。
- 示例:
1. **Pbkdf2PasswordEncoder**
- 使用 **PBKDF2** 算法进行密码加密。
- 提供较强的安全性,并且支持对密码进行哈希。
- 示例:
```java
@Bean
@ -38,10 +60,11 @@ public PasswordEncoder passwordEncoder() {
}
```
4. **Argon2PasswordEncoder**
- 使用 **Argon2** 算法对密码进行加密。
- Argon2 是目前被认为最强的密码哈希算法,支持内存密集型计算,从而防止硬件加速破解。
- 示例:
1. **Argon2PasswordEncoder**
- 使用 **Argon2** 算法对密码进行加密。
- Argon2 是目前被认为最强的密码哈希算法,支持内存密集型计算,从而防止硬件加速破解。
- 示例:
```java
@Bean
@ -50,10 +73,11 @@ public PasswordEncoder passwordEncoder() {
}
```
5. **SCryptPasswordEncoder**
- 使用 **SCrypt** 算法进行密码加密。
- SCrypt 是另一种内存密集型的密码加密算法,与 Argon2 类似,旨在防止硬件加速破解。
- 示例:
1. **SCryptPasswordEncoder**
- 使用 **SCrypt** 算法进行密码加密。
- SCrypt 是另一种内存密集型的密码加密算法,与 Argon2 类似,旨在防止硬件加速破解。
- 示例:
```java
@Bean
@ -62,18 +86,19 @@ public PasswordEncoder passwordEncoder() {
}
```
6. **MessageDigestPasswordEncoder** (已废弃)
- 基于 **MessageDigest** 算法进行加密(如 SHA-1、SHA-256 等)。
- 由于缺乏盐和密钥加密机制,已被其他更强的加密方式所替代。
1. **MessageDigestPasswordEncoder** (已废弃)
> 选择密码转换器的建议:
>
> + 在现代应用中,推荐使用 **BCryptPasswordEncoder****Argon2PasswordEncoder**,这两种算法提供了强大的加密性。
> + **Pbkdf2PasswordEncoder****SCryptPasswordEncoder** 也可以作为备选方案,尤其是当你希望加密算法能够承受更多资源密集型攻击时。
> + **NoOpPasswordEncoder** 仅限于开发和测试环境。
>
- 基于 **MessageDigest** 算法进行加密(如 SHA-1、SHA-256 等)。
- 由于缺乏盐和密钥加密机制,已被其他更强的加密方式所替代。
选择密码转换器的建议:
- 在现代应用中,推荐使用 **BCryptPasswordEncoder****Argon2PasswordEncoder**,这两种算法提供了强大的加密性。
- **Pbkdf2PasswordEncoder****SCryptPasswordEncoder** 也可以作为备选方案,尤其是当你希望加密算法能够承受更多资源密集型攻击时。
- **NoOpPasswordEncoder** 仅限于开发和测试环境。
### 访问主页
需要使用`http://localhost:8080/index`来访问主页,可以在配置中配置,访问根路径直接跳转
```java
@ -122,6 +147,7 @@ public class WebConfiguration implements WebMvcConfigurer {
```
### 自定义登录
```java
package cn.bunny.springdemo.configuration;
@ -202,13 +228,13 @@ public class SecurityConfiguration {
```
### CSRF 伪造
通常在自定义登录页面中加入
```html
<label>
<input name="_csrf" placeholder="_csrf" th:value="${_csrf.token}" type="hidden"/>
</label>
```
如果需要禁用
@ -218,6 +244,7 @@ public class SecurityConfiguration {
```
### 开发授权服务器
```xml
<!--资源服务器-->
<dependency>
@ -234,6 +261,4 @@ public class SecurityConfiguration {
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-authorization-server</artifactId>
</dependency>
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@ -1,4 +1,9 @@
# Spring Security快速入门
---
dir:
order: 2
---
# 之前SpringSecurity
**官方文档:**[https://docs.spring.io/spring-security/reference/index.html](https://docs.spring.io/spring-security/reference/index.html)
**功能:**
@ -25,7 +30,7 @@
项目结构
![](./images/image-20240418205453354.png)![](https://cdn.nlark.com/yuque/0/2025/png/42943943/1739884981997-1f74d00b-4ba8-4716-927c-91b26aee6d16.png)
![](./images/1739884981997-1f74d00b-4ba8-4716-927c-91b26aee6d16.png)
#### 基本包
这里用到了数据库但是在项目刚开始启动时,是没有配置数据库的,这时候启动肯定会报错,所以我们现在启动类上排出连接数据库的类。
@ -165,7 +170,7 @@ HTML模板
比如点击下面按钮会自动匹配路径并退出。
![](./images/image-20240418205838912.png)![](https://cdn.nlark.com/yuque/0/2025/png/42943943/1739884994974-19080b87-0be5-4d8b-a2b4-e57e996da90b.png)
![](./images/1739884994974-19080b87-0be5-4d8b-a2b4-e57e996da90b.png)
## 自定义Security配置
SecurityProperties修改默认用户和密码。
@ -561,7 +566,7 @@ public class PasswordTest {
## 自定义登录页面
### 创建登录页
![](https://cdn.nlark.com/yuque/0/2025/png/42943943/1739885018440-f282a25f-0223-4c1f-9ae9-fa67b4f6dfa1.png)
![](./images/1739885018440-f282a25f-0223-4c1f-9ae9-fa67b4f6dfa1.png)
#### 第一步创建Controller
```java
@ -651,7 +656,7 @@ public class WebSecurityConfig {
}
```
![](./images/image-20240421033928592.png)![](https://cdn.nlark.com/yuque/0/2025/png/42943943/1739885055597-43b7107f-a1b2-462a-996b-fd3b4d66ccb0.png)
![](./images/1739885055597-43b7107f-a1b2-462a-996b-fd3b4d66ccb0.png)
### 登录页的细节
在`WebSecurityConfig`中自定义前端传递值,默认传递用户名和密码为`username`和`password`,在下面示例中可以修改为自定义的用户名和密码参数。
@ -720,7 +725,7 @@ public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHand
}
```
![](./images/image-20240421150905120.png)![](https://cdn.nlark.com/yuque/0/2025/png/42943943/1739885100104-b414e402-cf91-4713-8789-9570a53721c5.png)
![](./images/1739885100104-b414e402-cf91-4713-8789-9570a53721c5.png)
在`WebSecurityConfig`类中添加以下`.successHandler(new MyAuthenticationSuccessHandler());`表示成功的返回结果,自定义结果。
@ -784,7 +789,7 @@ httpSecurity.formLogin(form -> {
});
```
![](./images/image-20240421152001424.png)![](https://cdn.nlark.com/yuque/0/2025/png/42943943/1739885069146-d3cae399-23b5-447a-a025-fea765c1d8be.png)
![](./images/1739885069146-d3cae399-23b5-447a-a025-fea765c1d8be.png)
### 注销响应
和前面成功和失败过程相似,只需要在`from`中再添加即可。
@ -825,7 +830,7 @@ httpSecurity.logout(logout -> {
});
```
![](./images/image-20240421153527873.png)![](https://cdn.nlark.com/yuque/0/2025/png/42943943/1739885117054-f7b847c2-7948-485f-bbc4-b53da35671dc.png)
![](./images/1739885117054-f7b847c2-7948-485f-bbc4-b53da35671dc.png)
之后访问:[http://localhost/demo](http://localhost/demo)
@ -876,13 +881,13 @@ httpSecurity.exceptionHandling(exception -> {
访问:[http://localhost/demo/](http://localhost/demo/)
![](./images/image-20240421155408869.png)![](https://cdn.nlark.com/yuque/0/2025/png/42943943/1739885133278-ef71eb9a-48e4-423a-96b5-ccfe68b07a8f.png)
![](./images/1739885133278-ef71eb9a-48e4-423a-96b5-ccfe68b07a8f.png)
登录后
![](./images/image-20240421155428734.png)![](https://cdn.nlark.com/yuque/0/2025/png/42943943/1739885137700-67858269-4190-4596-a4be-7227280220f1.png)
![](./images/1739885137700-67858269-4190-4596-a4be-7227280220f1.png)
![](./images/image-20240421155441568.png)![](https://cdn.nlark.com/yuque/0/2025/png/42943943/1739885142319-b86e0961-1eef-4ef9-b42a-f7c9d9335ff7.png)
![](./images/1739885142319-b86e0961-1eef-4ef9-b42a-f7c9d9335ff7.png)
### 跨域访问
在`WebSecurityConfig`类中添加以下。
@ -1158,11 +1163,11 @@ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundEx
为`roles("ADMIN")`时全部可以访问。
![](./images/image-20240421171511955.png)![](https://cdn.nlark.com/yuque/0/2025/png/42943943/1739885159429-1ba4d90f-d7c2-4d48-9da1-2c11a9252233.png)
![](./images/1739885159429-1ba4d90f-d7c2-4d48-9da1-2c11a9252233.png)
为普通用户时。
![](./images/image-20240421171523847.png)![](https://cdn.nlark.com/yuque/0/2025/png/42943943/1739885163072-25c78a6a-56dc-46cf-9fc7-cbc36b2fe155.png)
![](./images/1739885163072-25c78a6a-56dc-46cf-9fc7-cbc36b2fe155.png)
@ -1344,5 +1349,5 @@ public class UserController {
> 如果当前用户不是admin用户访问不了。
>
![](https://cdn.nlark.com/yuque/0/2025/png/42943943/1739885175291-ac74528f-137c-4021-bd8d-4d925ff98d82.png)
![](./images/1739885175291-ac74528f-137c-4021-bd8d-4d925ff98d82.png)

View File

@ -1,3 +1,8 @@
---
dir:
order: 1
---
# 权限管理设计基础数据字典
https://www.cnblogs.com/myindex/p/9116177.html
@ -155,4 +160,3 @@ https://www.cnblogs.com/myindex/p/9116177.html

View File

@ -1,3 +1,5 @@
# HttpUtil
```java
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;

View File

@ -1,3 +1,5 @@
# Java递归示例
```java
/**
* * 构建权限树形结构

View File

@ -1,3 +1,5 @@
# Knife4j配置
```java
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;

View File

@ -1,3 +1,5 @@
# Minio工具类
```java
package cn.bunny.module.minio.utils;

View File

@ -0,0 +1,16 @@
---
title: Utils通用类
index: false
icon: vscode-icons:file-type-class
category:
- 笔记
- 记录
- 学习
- 问题
- Java
- utils
dir:
order: 4
---
<Catalog />

View File

@ -1,3 +1,5 @@
# 全局异常处理类
```java
import cn.bunny.dao.vo.result.Result;
import cn.bunny.dao.vo.result.ResultCodeEnum;

View File

@ -1,4 +1,11 @@
---
icon: ri:java-fill
---
# 并发容器使用
## ArrayBlockingQueue使用
```java
ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(8);

View File

@ -0,0 +1,49 @@
# MySQL高级
## 设置索引
```sql
ALTER TABLE router ADD INDEX idx_id_parent_id (id, parent_id);
ALTER TABLE router ADD INDEX idx_id (id);
ALTER TABLE router ADD INDEX idx_parent_id (parent_id);
SHOW INDEX FROM router;
ALTER TABLE router DROP INDEX id;
ALTER TABLE router DROP INDEX parent_id;
```
## 递归查询
```sql
WITH RECURSIVE my_table AS (
SELECT
*
FROM
router
WHERE
parent_id = 0 UNION ALL
SELECT
ung.*
FROM
router ung
INNER JOIN my_table ON ung.parent_id = my_table.id
) SELECT
*
FROM
my_table
```
**递归查询id直到查询到父级id为0**
```sql
WITH RECURSIVE ParentChain AS (
SELECT *
FROM router
WHERE id IN (1833698026257588226,1833698026257588228)
UNION ALL
SELECT r.*
FROM router r
INNER JOIN ParentChain pc ON r.id = pc.parent_id
)
SELECT DISTINCT * FROM ParentChain;
```

View File

@ -0,0 +1,15 @@
---
title: MySQL
index: true
icon: logos:mysql
headerDepth: 4
category:
- 笔记
- 记录
- 学习
- 问题
- SQL
- MySQL
---
<Catalog />

14
src/note/SQL/README.md Normal file
View File

@ -0,0 +1,14 @@
---
title: SQL笔记
index: false
icon: gravity-ui:abbr-sql
headerDepth: 4
category:
- 笔记
- 记录
- 学习
- 问题
- SQL
---
<Catalog />