[TOC]
# 1. 關閉CSRF保護演示
**1. 關閉CSRF保護**
```java
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//關閉csrf
http.csrf().disable();
}
}
```
**2. controller層**
```java
/**
* post方式訪問
*/
@PostMapping("/v2/csrf/form")
@ResponseBody
public Account getAccount(Account account) {
return account;
}
```
**3. 表單以POST方式提交**
```html
<form action="/v2/csrf/form" method="post">
id:<input type="text" name="id"/><br/>
username:<input type="text" name="username"/><br/>
password:<input type="text" name="password"/><br/>
<input type="submit"/>
</form>
```
**4. 結果**
可以正常提交。
<br/>
# 2. 啟用CSRF保護演示
**1. 啟用CSRF保護**
```java
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//不調用代碼http.csrf().disable()就是開啟csrf保護了
//http.csrf().disable();
}
}
```
**2. controller層**
```java
/**
* post方式訪問。
* Spring Security CSRF 只針對 PATCH,POST,PUT 和 DELETE 方法進行防護,GET方法不防護。
*/
@PostMapping("/v2/csrf/form")
@ResponseBody
public Account getAccount(Account account) {
return account;
}
```
**3. 表單以POST方式提交**
```html
<form action="/v2/csrf/form" method="post">
id:<input type="text" name="id"/><br/>
username:<input type="text" name="username"/><br/>
password:<input type="text" name="password"/><br/>
<input type="submit"/>
</form>
```
**4. 結果:表單不能提交,重定向到403頁面**
```
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Jun 10 20:21:37 CST 2022
There was an unexpected error (type=Forbidden, status=403).
Forbidden
```
**5. 在表單內添加csrf的隱藏域便可提交了**
```html
<form action="/v2/csrf/form" method="post">
id:<input type="text" name="id"/><br/>
username:<input type="text" name="username"/><br/>
password:<input type="text" name="password"/><br/>
<input type="hidden" th:if="${_csrf}!=null" th:value="${_csrf.token}" th:name="${_csrf.parameterName}"/>
<input type="submit"/>
</form>
```
<br/>
- 跨域問題
- 跨域是什么
- 跨域解決方案
- 從后端解決
- nginx反向代理
- WebSocket
- websocket是什么
- websocket協議
- 使用場景
- 實現方式
- 注解與html5原生方式
- websocketAPI
- 實現步驟
- 文件上傳
- 文件下載
- 廣播通信
- 定時推送
- 編程與socketjs方式
- socketjs與stompjs框架
- 實現步驟
- 重載目的地
- SimpMessagingTemplate
- 定時向前端推送數據
- 5種監聽事件
- 點對點通信
- 攔截器
- HandshakeInterceptor
- ChannelInterceptor
- poi之excel表格
- 表格版本
- POI常用類
- POI依賴
- 寫表格
- 編寫表格過程
- 單元格邊框樣式
- 單元格背景色
- 凍結行或列
- 單元格合并
- 單元格內換行
- 文檔內跳轉
- 讀表格
- Web中的Excel操作
- 導出表格
- 讀取表格
- poi之word文檔
- word版本
- 寫word
- 基本使用
- 標題樣式
- 添加圖片
- EasyExcel表格
- EasyExcel是什么
- 與其他Excel工具對比
- EasyExcel依賴
- 讀Excel
- 簡單讀取
- 指定列位置
- 讀取多個sheet
- 格式轉換
- 多行表頭
- 同步讀
- 寫Excel
- 簡單寫入
- 單元格樣式
- 攔截器
- 列寬
- 凍結行或列
- 合并單元格
- 填充Excel
- SpringSecurity
- SpringSecurity是什么
- 同類型產品對比
- 環境搭建
- 相關概念
- 密碼加密
- Web權限控制
- UserDetailsService接口
- 登錄認證
- 自定義登錄頁
- 未授權跳轉登錄頁
- 權限控制
- 自定義403頁面
- 權限注解
- 記住我功能
- 注銷功能
- CSRF
- CSRF是什么
- CSRF保護演示
- 前后端分離權限控制
- 環境搭建
- 認證實現
- 會話管理
- 動態權限管理
- 微服務權限控制
- 權限控制方案
- SpringBoot整合RabbitMQ
- 整合步驟
- Fanout交換機演示
- Direct交換機演示
- Topic交換機演示
- @RabbitListener方法
- JWT認證與授權
- 環境搭建
- 密碼加密
- 認證與授權