SpringSecurity添加圖形驗(yàn)證碼認(rèn)證實(shí)現(xiàn)
第一步:圖形驗(yàn)證碼接口
1.使用第三方的驗(yàn)證碼生成工具Kaptcha
https://github.com/penggle/kaptcha
@Configuration
public class KaptchaImageCodeConfig {
@Bean
public DefaultKaptcha getDefaultKaptcha(){
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
Properties properties = new Properties();
properties.setProperty(Constants.KAPTCHA_BORDER, "yes");
properties.setProperty(Constants.KAPTCHA_BORDER_COLOR, "192,192,192");
properties.setProperty(Constants.KAPTCHA_IMAGE_WIDTH, "110");
properties.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT, "36");
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, "blue");
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, "28");
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_NAMES, "宋體");
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
// 圖片效果
properties.setProperty(Constants.KAPTCHA_OBSCURIFICATOR_IMPL,
"com.google.code.kaptcha.impl.ShadowGimpy");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}2.設(shè)置驗(yàn)證接口
Logger logger = LoggerFactory.getLogger(getClass());
public static final String SESSION_KEY = "SESSION_KEY_IMAGE_CODE";
@GetMapping("/code/image")
public void codeImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
// 獲得隨機(jī)驗(yàn)證碼
String code = defaultKaptcha.createText();
logger.info("驗(yàn)證碼:{}",code);
// 將驗(yàn)證碼存入session
request.getSession().setAttribute(SESSION_KEY,code);
// 繪制驗(yàn)證碼
BufferedImage image = defaultKaptcha.createImage(code);
// 輸出驗(yàn)證碼
ServletOutputStream out = response.getOutputStream();
ImageIO.write(image, "jpg", out);
}
3.模板表單設(shè)置
<div class="form-group">
<label>驗(yàn)證碼:</label>
<input type="text" class="form-control" placeholder="驗(yàn)證碼" name="code">
<img src="/code/image" th:src="@{/code/image}" onclick="this.src='/code/image?'+Math.random()">
</div>
第二步:設(shè)置圖像驗(yàn)證過濾器
1.過濾器
@Component
public class ImageCodeValidateFilter extends OncePerRequestFilter {
? ? private MyAuthenticationFailureHandler myAuthenticationFailureHandler;
? ? // 失敗處理器
? ? @Resource
? ? public void setMyAuthenticationFailureHandler(MyAuthenticationFailureHandler myAuthenticationFailureHandler) {
? ? ? ? this.myAuthenticationFailureHandler = myAuthenticationFailureHandler;
? ? }
? ? @Override
? ? protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
? ? ? ? try {
? ? ? ? ? ? if ("/login/form".equals(request.getRequestURI()) &&
? ? ? ? ? ? ? ? ? ? request.getMethod().equalsIgnoreCase("post")) {
? ? ? ? ? ? ? ? // 獲取session的驗(yàn)證碼
? ? ? ? ? ? ? ? String sessionCode = (String) request.getSession().getAttribute(PageController.SESSION_KEY);
? ? ? ? ? ? ? ? // 獲取用戶輸入的驗(yàn)證碼
? ? ? ? ? ? ? ? String inputCode = request.getParameter("code");
? ? ? ? ? ? ? ? // 判斷是否正確
? ? ? ? ? ? ? ? if(sessionCode == null||!sessionCode.equals(inputCode)){
? ? ? ? ? ? ? ? ? ? throw new ValidateCodeException("驗(yàn)證碼錯(cuò)誤");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }catch (AuthenticationException e){
? ? ? ? ? ? myAuthenticationFailureHandler.onAuthenticationFailure(request,response,e);
? ? ? ? ? ? //e.printStackTrace();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? filterChain.doFilter(request, response);
? ? }
}異常類
public class ValidateCodeException extends AuthenticationException {
public ValidateCodeException(String msg) {
super(msg);
}
}
注意:一定是繼承AuthenticationException
第三步:將圖像驗(yàn)證過濾器添加到springsecurity過濾器鏈中
1.添加到過濾器鏈中,并設(shè)置在用戶認(rèn)證過濾器(UsernamePasswordAuthenticationFilter)前
@Resource
private ImageCodeValidateFilter imageCodeValidateFilter;
@Override
protected void configure(HttpSecurity http) throws Exception {
// 前后代碼略
// 添加圖形驗(yàn)證碼過濾器鏈
http.addFilterBefore(imageCodeValidateFilter, UsernamePasswordAuthenticationFilter.class)
}
2.一定不要忘記放行驗(yàn)證碼接口
// 攔截設(shè)置
http
.authorizeHttpRequests()
//排除/login
.antMatchers("/login","/code/image").permitAll();
到此這篇關(guān)于SpringSecurity添加圖形驗(yàn)證碼認(rèn)證實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringSecurity 圖形驗(yàn)證碼認(rèn)證內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springsecurity實(shí)現(xiàn)登錄驗(yàn)證以及根據(jù)用戶身份跳轉(zhuǎn)不同頁面
- SpringBoot整合SpringSecurity實(shí)現(xiàn)圖形驗(yàn)證碼功能
- SpringSecurity集成圖片驗(yàn)證碼的詳細(xì)過程
- SpringBoot?SpringSecurity?詳細(xì)介紹(基于內(nèi)存的驗(yàn)證)
- SpringBoot+SpringSecurity+jwt實(shí)現(xiàn)驗(yàn)證
- Springboot+SpringSecurity實(shí)現(xiàn)圖片驗(yàn)證碼登錄的示例
- SpringSecurity從數(shù)據(jù)庫中獲取用戶信息進(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中的線程安全集合CopyOnWriteArrayList解析
這篇文章主要介紹了Java中的線程安全CopyOnWriteArrayList解析,CopyOnWriteArrayList是ArrayList的線程安全版本,從他的名字可以推測(cè),CopyOnWriteArrayList是在有寫操作的時(shí)候會(huì)copy一份數(shù)據(jù),然后寫完再設(shè)置成新的數(shù)據(jù),需要的朋友可以參考下2023-12-12
java如何獲取request中json數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于java如何獲取request中json數(shù)據(jù)的相關(guān)資料,文中通過代碼示例以及圖文將獲取的方法介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08
偵聽消息隊(duì)列的Message Listener類示例詳解
Spring AMQP 是基于 Spring 框架的AMQP消息解決方案,提供模板化的發(fā)送和接收消息的抽象層,提供基于消息驅(qū)動(dòng)的 POJO的消息監(jiān)聽等,簡化了我們對(duì)于RabbitMQ相關(guān)程序的開發(fā),本文給大家介紹偵聽消息隊(duì)列的Message Listener類,感興趣的朋友一起看看吧2023-12-12
深入理解Java動(dòng)態(tài)代理與靜態(tài)代理
這篇文章主要介紹了深入理解Java動(dòng)態(tài)代理與靜態(tài)代理,靜態(tài)代理,代理類和被代理的類實(shí)現(xiàn)了同樣的接口,代理類同時(shí)持有被代理類的引用,動(dòng)態(tài)代理的根據(jù)實(shí)現(xiàn)方式的不同可以分為JDK動(dòng)態(tài)代理和CGlib動(dòng)態(tài)代理2022-06-06
mybatis和mybatis-plus設(shè)置值為null不起作用問題及解決
Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查詢時(shí)對(duì)空值的處理策略,通過配置不同的策略類型,可以靈活地處理實(shí)體對(duì)象的空值問題2025-02-02
Mybatis實(shí)現(xiàn)自定義類型轉(zhuǎn)換器TypeHandler的方法
Mybatis實(shí)現(xiàn)自定義的轉(zhuǎn)換器非常的簡單,只需要三步就可以實(shí)現(xiàn)自定義類型轉(zhuǎn)換器TypeHandler,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看下吧2016-07-07
Idea中如何調(diào)出Run dashboard 或services窗口
這篇文章主要介紹了Idea中如何調(diào)出Run dashboard 或services窗口問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Java如何實(shí)現(xiàn)多個(gè)線程之間共享數(shù)據(jù)
這篇文章主要介紹了Java如何實(shí)現(xiàn)多個(gè)線程之間共享數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11

