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

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

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

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

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

3、將過濾器加入SpringSecurity過濾鏈
修改WebSecurityConfig:
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Slf4j
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Resource
private ValidateCodeFilter validateCodeFilter; //自動注入我定義的驗證碼過濾器
@Override
/**
* Security的http請求配置
*
* @param http
* @throws Exception
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
//設(shè)置登陸方式
http.formLogin()//使用用戶名和密碼的登陸方式
.usernameParameter("uname") //頁面表單的用戶名的name
.passwordParameter("pwd")//頁面表單的密碼的name
.loginPage("/login/toLogin") //自己定義登陸頁面的地址
.loginProcessingUrl("/login/doLogin")//配置登陸的url
.successForwardUrl("/index/toIndex") //登陸成功跳轉(zhuǎn)的頁面
.failureForwardUrl("/login/toLogin")//登陸失敗跳轉(zhuǎn)的頁面
.permitAll();
//配置退出方式
http.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/login/toLogin")
.permitAll();
//配置路徑攔截 的url的匹配規(guī)則
http.authorizeRequests().antMatchers("/code/image").permitAll()
//任何路徑要求必須認(rèn)證之后才能訪問
.anyRequest().authenticated();
// 禁用csrf跨站請求,注意不要寫錯了
http.csrf().disable();
// 配置登錄之前添加一個驗證碼的過濾器
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();
}
}以上注意兩點,一是別忘了放行生成二維碼的接口,這個不需要登錄鑒權(quán)。
http.authorizeRequests()
.antMatchers("/code/image")
.permitAll()
//任何路徑要求必須認(rèn)證之后才能訪問
.anyRequest().authenticated();而是在用戶名密碼驗證過濾器前加一個自定義的驗證碼過濾器,addFilter方法:
http.addFilterBefore(validateCodeFilter,UsernamePasswordAuthenticationFilter.class);
4、修改登錄頁
修改login.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用戶登陸</title>
</head>
<body>
<h2>登錄頁面</h2>
<!--${param.error}這個如果有值,就顯示帳號或密碼錯誤-->
<h4 th:if="${param.error}" style="color: #FF0000;">帳號或密碼錯誤,請重新輸入</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>驗證碼:</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集成圖片驗證碼的文章就介紹到這了,更多相關(guān)SpringSecurity圖片驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springsecurity實現(xiàn)登錄驗證以及根據(jù)用戶身份跳轉(zhuǎn)不同頁面
- SpringBoot整合SpringSecurity實現(xiàn)圖形驗證碼功能
- SpringBoot?SpringSecurity?詳細(xì)介紹(基于內(nèi)存的驗證)
- SpringSecurity添加圖形驗證碼認(rèn)證實現(xiàn)
- SpringBoot+SpringSecurity+jwt實現(xiàn)驗證
- Springboot+SpringSecurity實現(xiàn)圖片驗證碼登錄的示例
- SpringSecurity從數(shù)據(jù)庫中獲取用戶信息進(jìn)行驗證的案例詳解
- SpringSecurity實現(xiàn)圖形驗證碼功能的實例代碼
- SpringBoot + SpringSecurity 短信驗證碼登錄功能實現(xiàn)
- SpringSecurity實現(xiàn)多種身份驗證方式
相關(guān)文章
SpringBoot+Vue實現(xiàn)數(shù)據(jù)添加功能
這篇文章主要介紹了SpringBoot+Vue實現(xiàn)數(shù)據(jù)添加功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
SpringBoot+Hibernate實現(xiàn)自定義數(shù)據(jù)驗證及異常處理
這篇文章主要為大家介紹了SpringBoot如何整合Hibernate自定義數(shù)據(jù)驗證及多種方式異常處理,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-04-04
Java 向上轉(zhuǎn)型和向下轉(zhuǎn)型的詳解
這篇文章主要介紹了 Java 向上轉(zhuǎn)型和向下轉(zhuǎn)型的詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
Spring中bean的初始化和銷毀幾種實現(xiàn)方式詳解
這篇文章主要介紹了Spring中bean的初始化和銷毀幾種實現(xiàn)方式詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Struts2學(xué)習(xí)筆記(3)-DMI動態(tài)調(diào)用方式
本文主要介紹Struts2的DMI動態(tài)調(diào)用的兩種方式,簡單實用,希望能給大家做一個參考。2016-06-06

