SpringSecurity集成圖片驗(yàn)證碼的詳細(xì)過(guò)程
SpringSecurity是通過(guò)過(guò)濾器鏈來(lái)完成的,接下來(lái)的驗(yàn)證碼,可以嘗試創(chuàng)建一個(gè)過(guò)濾器放到Security的過(guò)濾器鏈中,在自定義的過(guò)濾器中比較驗(yàn)證碼。
1、生成圖片驗(yàn)證碼
引入hutool依賴,用于生成驗(yàn)證碼。(這個(gè)單詞怎么讀?糊涂?)
<!--引入hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.9</version>
</dependency>寫個(gè)用于生成驗(yàn)證碼的接口:
//HttpServletRequest和HttpServletResponse對(duì)象使用自動(dòng)注入或者寫在controller方法的形參都行
@Controller
@Slf4j
public class CaptchaController {
@GetMapping("/code/image")
public void getCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
//創(chuàng)建一個(gè)驗(yàn)證碼
CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(200, 100, 2, 20);
//獲取生成的圖片二維碼中的值,并放到session中
String captchaCode=circleCaptcha.getCode();
log.info("生成的驗(yàn)證碼為:{}",captchaCode);
request.getSession().setAttribute("LOGIN_CAPTCHA_CODE",captchaCode);
//將圖片寫到響應(yīng)流中,參數(shù)一是圖片。參數(shù)二是圖片格式,參數(shù)三是響應(yīng)流
ImageIO.write(circleCaptcha.getImage(),"JPEG",response.getOutputStream());
}
}此時(shí),調(diào)用這個(gè)接口會(huì)在響應(yīng)里返回一個(gè)圖片。調(diào)用下接口看看效果:

2、創(chuàng)建驗(yàn)證碼過(guò)濾器
很明顯,校驗(yàn)驗(yàn)證碼要先于校驗(yàn)用戶名密碼,驗(yàn)證碼都不對(duì),就不用往下驗(yàn)證用戶名和密碼了。新建自定義過(guò)濾器類ValidateCodeFilter,繼承抽象類OncePerRequestFilter 。右鍵看下繼承關(guān)系:

可以看到最終是實(shí)現(xiàn)了Filter接口。但這里別直接實(shí)現(xiàn)Filter,繼承OncePerRequestFilter,里面的好多東西直接用,能省一點(diǎn)是一點(diǎn)。過(guò)濾器的實(shí)現(xiàn)思路為:
- 從前端獲取驗(yàn)證碼
- 從session中獲取驗(yàn)證碼(生成驗(yàn)證碼的時(shí)候塞session里了)
- 判斷是否相等
@Component
@Slf4j
public class ValidateCodeFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//因?yàn)樽詈笫荈ilter接口,所以所有請(qǐng)求都過(guò)這個(gè)過(guò)濾器
//這里要先判斷接口是不是登錄接口,不是就別對(duì)比session和前端傳來(lái)的驗(yàn)證碼了
String requestURI = request.getRequestURI(); //URI即去掉IP、PORT那串
log.info("請(qǐng)求的URI為:{}", requestURI);
if (!requestURI.equals("/login/doLogin")) {
doFilter(request, response, filterChain); //不是登錄接口直接放行
} else {
validateCode(request, response,filterChain); //是登錄接口則校驗(yàn)
}
}
private void validateCode(HttpServletRequest request, HttpServletResponse response,FilterChain filterChain) throws IOException, ServletException {
String enterCaptchaCode = request.getParameter("code"); //從請(qǐng)求中拿傳過(guò)來(lái)的驗(yàn)證碼的值(dto好些)
HttpSession session = request.getSession();
String captchaCodeInSession = (String) session.getAttribute("LOGIN_CAPTCHA_CODE"); //保存在session中的驗(yàn)證碼
log.info("用戶輸入的驗(yàn)證碼為:{},session中的驗(yàn)證碼為:{}",enterCaptchaCode,captchaCodeInSession);
//移除session中之前可能存在的錯(cuò)誤信息
session.removeAttribute("captchaCodeErrorMsg");
if (!StringUtils.hasText(captchaCodeInSession)) {
session.removeAttribute("LOGIN_CAPTCHA_CODE");
}
if (!StringUtils.hasText(enterCaptchaCode) || !StringUtils.hasText(captchaCodeInSession) || !enterCaptchaCode.equalsIgnoreCase(captchaCodeInSession)) {
//說(shuō)明驗(yàn)證碼不正確,返回登陸頁(yè)面
session.setAttribute("captchaCodeErrorMsg", "驗(yàn)證碼不正確");
//驗(yàn)證失敗,重定向到登錄頁(yè)面
response.sendRedirect("/login/toLogin");
}else{
filterChain.doFilter(request,response); //驗(yàn)證成功,放行
}
}
}關(guān)于requset.getParameter("code")

關(guān)于在controller中接收前端數(shù)據(jù)的方式:

關(guān)于直接在請(qǐng)求中獲取參數(shù)的建議:

3、將過(guò)濾器加入SpringSecurity過(guò)濾鏈
修改WebSecurityConfig:
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Slf4j
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Resource
private ValidateCodeFilter validateCodeFilter; //自動(dòng)注入我定義的驗(yàn)證碼過(guò)濾器
@Override
/**
* Security的http請(qǐng)求配置
*
* @param http
* @throws Exception
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
//設(shè)置登陸方式
http.formLogin()//使用用戶名和密碼的登陸方式
.usernameParameter("uname") //頁(yè)面表單的用戶名的name
.passwordParameter("pwd")//頁(yè)面表單的密碼的name
.loginPage("/login/toLogin") //自己定義登陸頁(yè)面的地址
.loginProcessingUrl("/login/doLogin")//配置登陸的url
.successForwardUrl("/index/toIndex") //登陸成功跳轉(zhuǎn)的頁(yè)面
.failureForwardUrl("/login/toLogin")//登陸失敗跳轉(zhuǎn)的頁(yè)面
.permitAll();
//配置退出方式
http.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/login/toLogin")
.permitAll();
//配置路徑攔截 的url的匹配規(guī)則
http.authorizeRequests().antMatchers("/code/image").permitAll()
//任何路徑要求必須認(rèn)證之后才能訪問(wèn)
.anyRequest().authenticated();
// 禁用csrf跨站請(qǐng)求,注意不要寫錯(cuò)了
http.csrf().disable();
// 配置登錄之前添加一個(gè)驗(yàn)證碼的過(guò)濾器
http.addFilterBefore(validateCodeFilter,UsernamePasswordAuthenticationFilter.class);
}
/**
* 資源服務(wù)匹配放行【靜態(tài)資源文件】
*
* @param web
* @throws Exception
*/
// @Override
//public void configure(WebSecurity web) throws Exception {
// web.ignoring().mvcMatchers("/resources/**");
//}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}以上注意兩點(diǎn),一是別忘了放行生成二維碼的接口,這個(gè)不需要登錄鑒權(quán)。
http.authorizeRequests()
.antMatchers("/code/image")
.permitAll()
//任何路徑要求必須認(rèn)證之后才能訪問(wèn)
.anyRequest().authenticated();而是在用戶名密碼驗(yàn)證過(guò)濾器前加一個(gè)自定義的驗(yàn)證碼過(guò)濾器,addFilter方法:
http.addFilterBefore(validateCodeFilter,UsernamePasswordAuthenticationFilter.class);
4、修改登錄頁(yè)
修改login.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用戶登陸</title>
</head>
<body>
<h2>登錄頁(yè)面</h2>
<!--${param.error}這個(gè)如果有值,就顯示帳號(hào)或密碼錯(cuò)誤-->
<h4 th:if="${param.error}" style="color: #FF0000;">帳號(hào)或密碼錯(cuò)誤,請(qǐng)重新輸入</h4>
<form action="/login/doLogin" method="post">
<table>
<tr>
<td>用戶名:</td>
<td><input type="text" name="uname" value="zhangsan"></td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td>驗(yàn)證碼:</td>
<td><input type="text" name="code"> <img src="/code/image" style="height:33px;cursor:pointer;" onclick="this.src=this.src">
<span th:text="${session.captchaCodeErrorMsg}" style="color: #FF0000;" >username</span>
</td>
</tr>
<tr>
<td colspan="2">
<button type="submit">登錄</button>
</td>
</tr>
</table>
</form>
</body>效果:

到此這篇關(guān)于SpringSecurity集成圖片驗(yàn)證碼的文章就介紹到這了,更多相關(guān)SpringSecurity圖片驗(yàn)證碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springsecurity實(shí)現(xiàn)登錄驗(yàn)證以及根據(jù)用戶身份跳轉(zhuǎn)不同頁(yè)面
- SpringBoot整合SpringSecurity實(shí)現(xiàn)圖形驗(yàn)證碼功能
- SpringBoot?SpringSecurity?詳細(xì)介紹(基于內(nèi)存的驗(yàn)證)
- SpringSecurity添加圖形驗(yàn)證碼認(rèn)證實(shí)現(xiàn)
- SpringBoot+SpringSecurity+jwt實(shí)現(xiàn)驗(yàn)證
- Springboot+SpringSecurity實(shí)現(xiàn)圖片驗(yàn)證碼登錄的示例
- SpringSecurity從數(shù)據(jù)庫(kù)中獲取用戶信息進(jìn)行驗(yàn)證的案例詳解
- SpringSecurity實(shí)現(xiàn)圖形驗(yàn)證碼功能的實(shí)例代碼
- SpringBoot + SpringSecurity 短信驗(yàn)證碼登錄功能實(shí)現(xiàn)
- SpringSecurity實(shí)現(xiàn)多種身份驗(yàn)證方式
相關(guān)文章
java新特性之for循環(huán)最全的用法總結(jié)
下面小編就為大家?guī)?lái)一篇java新特性之for循環(huán)最全的用法總結(jié)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
詳解Java如何實(shí)現(xiàn)FP-Growth算法
學(xué)校里的實(shí)驗(yàn),要求實(shí)現(xiàn)FP-Growth算法.FP-Growth算法比Apriori算法快很多(但是卻比不上時(shí)間)在網(wǎng)上搜索后發(fā)現(xiàn)Java實(shí)現(xiàn)的FP-Growth算法很少,且大多數(shù)不太能理解):太菜.所以就自己實(shí)現(xiàn)了一下.這篇文章重點(diǎn)介紹一下我的Java實(shí)現(xiàn) ,需要的朋友可以參考下2021-06-06
Java Stream API中實(shí)現(xiàn)數(shù)據(jù)的并行處理指南
在 Java Stream API 中,實(shí)現(xiàn)數(shù)據(jù)的并行處理非常簡(jiǎn)單,核心是通過(guò) ??parallelStream() ??? 方法獲取并行流,而非默認(rèn)的串行流(??stream()??),本文給大家介紹了Java Stream API中實(shí)現(xiàn)數(shù)據(jù)的并行處理的操作方法,需要的朋友可以參考下2025-10-10
springboot的jar能獨(dú)立運(yùn)行的原因解析
這篇文章主要介紹了springboot的jar能獨(dú)立運(yùn)行的原因解析,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
SpringBoot獲取配置文件值和環(huán)境變量的方式
本文介紹了SpringBoot配置文件的基礎(chǔ)知識(shí)、使用@Value和@ConfigurationProperties注入配置值、使用Environment接口獲取環(huán)境變量、配置文件的Profile支持、高級(jí)用法如條件化配置和配置刷新機(jī)制、以及配置的最佳實(shí)踐建議,需要的朋友可以參考下2026-04-04
一文徹底搞懂Java項(xiàng)目中DI注入失敗的六大常見原因與解法
在企業(yè)級(jí) Java 開發(fā)中,Spring 框架的依賴注入幾乎是每個(gè)項(xiàng)目的標(biāo)配,本文將結(jié)合真實(shí)項(xiàng)目經(jīng)驗(yàn),系統(tǒng)梳理依賴注入失敗的六大常見場(chǎng)景,幫助你快速定位問(wèn)題、理解本質(zhì)、避免踩坑2025-11-11

