網(wǎng)站首頁 編程語言 正文
前言
spring boot security默認(rèn)配置有一個登錄頁面,當(dāng)采用前后端分離的場景下,需要解決兩個問題:
- 前端有自己的登錄頁面,不需要使用spring boot security默認(rèn)的登錄頁面
- 登錄相關(guān)接口允許匿名訪問
因此需要自定義相關(guān)實現(xiàn)。
自定義配置
自定義配置的核心實現(xiàn)如下:
@Component
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// 在這里自定義配置
}
}
如上示例代碼,關(guān)鍵是重寫這個方法,spring boot security的擴(kuò)展方法不只這一種,化繁為簡,盡量采用最簡單直白的方式。
認(rèn)證失敗自定義處理
當(dāng)請求認(rèn)證失敗的時候,可以返回一個401的狀態(tài)碼,這樣前端頁面可以根據(jù)響應(yīng)做相關(guān)處理,而不是出現(xiàn)默認(rèn)的登錄頁面或者登錄表單:
@Override
protected void configure(HttpSecurity http) throws Exception {
// 在這里自定義配置
http.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.exceptionHandling()
// 認(rèn)證失敗返回401狀態(tài)碼,前端頁面可以根據(jù)401狀態(tài)碼跳轉(zhuǎn)到登錄頁面
.authenticationEntryPoint((request, response, authException) ->
response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase()))
.and().cors()
// csrf是否決定禁用,請自行考量
.and().csrf().disable()
// .antMatcher("/**")
// 采用http 的基本認(rèn)證.
.httpBasic();
}
登錄相關(guān)接口匿名訪問
登錄接口或者驗證碼需要匿名訪問,不應(yīng)該被攔截。
如下,提供獲取驗證碼和登錄的接口示例:
@RequestMapping("/login")
@RestController
public class LoginController {
@PostMapping()
public Object login() {
return "login success";
}
@GetMapping("/captcha")
public Object captcha() {
return "1234";
}
}
配置允許訪問這部分接口:
@Override
protected void configure(HttpSecurity http) throws Exception {
// 在這里自定義配置
http.authorizeRequests()
// 登錄相關(guān)接口都允許訪問
.antMatchers("/login/**").permitAll()
// 還有其它接口就這樣繼續(xù)寫
.antMatchers("/other/**").permitAll()
.anyRequest()
.authenticated()
... 省略下面的
}
前置文章
spring boot security快速使用示例
原文鏈接:https://blog.csdn.net/x763795151/article/details/131445388
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-05-26 python中的getter與setter你了解嗎_python
- 2022-09-24 python實現(xiàn)字母閃爍效果的示例代碼_python
- 2024-07-15 項目開發(fā)中使用Date和LocalDateTime處理日期
- 2023-01-07 Flutter?Widget開發(fā)之Focus組件圖文詳解_Android
- 2023-12-10 記錄兩個Excel導(dǎo)出出現(xiàn)的問題
- 2024-03-01 【Promise】promise關(guān)鍵問題和解決辦法
- 2022-09-03 React實現(xiàn)輪播圖效果_React
- 2022-04-04 elementui組件select選擇不中的問題(組件select選擇器無法顯示選中的內(nèi)容)
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支