SpringSecurity添加图形验证码认证功能

第一步:图形验证码接口

1.使用第三方的验证码生成工具Kaptcha

https://github.com/penggle/kaptcha

@Configuration
public class KaptchaImageCodeConfig {@Beanpublic 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.设置验证接口

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 {// 获得随机验证码String code = defaultKaptcha.createText();logger.info("验证码:{}",code);// 将验证码存入sessionrequest.getSession().setAttribute(SESSION_KEY,code);// 绘制验证码BufferedImage image = defaultKaptcha.createImage(code);// 输出验证码ServletOutputStream out = response.getOutputStream();ImageIO.write(image, "jpg", out);
}

3.模板表单设置

<div class="form-group"><label>验证码:</label><input type="text" class="form-control" placeholder="验证码" name="code"><img src="/code/image" th:src="@{/code/image}" onclick="this.src='/code/image?'+Math.random()">
</div>

第二步:设置图像验证过滤器

1.过滤器

@Component
public class ImageCodeValidateFilter extends OncePerRequestFilter {private MyAuthenticationFailureHandler myAuthenticationFailureHandler;// 失败处理器@Resourcepublic void setMyAuthenticationFailureHandler(MyAuthenticationFailureHandler myAuthenticationFailureHandler) {this.myAuthenticationFailureHandler = myAuthenticationFailureHandler;}@Overrideprotected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {try {if ("/login/form".equals(request.getRequestURI()) &&request.getMethod().equalsIgnoreCase("post")) {// 获取session的验证码String sessionCode = (String) request.getSession().getAttribute(PageController.SESSION_KEY);// 获取用户输入的验证码String inputCode = request.getParameter("code");// 判断是否正确if(sessionCode == null||!sessionCode.equals(inputCode)){throw new ValidateCodeException("验证码错误");}}}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

第三步:将图像验证过滤器添加到springsecurity过滤器链中

1.添加到过滤器链中,并设置在用户认证过滤器(UsernamePasswordAuthenticationFilter)前

@Resource
private ImageCodeValidateFilter imageCodeValidateFilter;
@Override
protected void configure(HttpSecurity http) throws Exception {// 前后代码略// 添加图形验证码过滤器链http.addFilterBefore(imageCodeValidateFilter, UsernamePasswordAuthenticationFilter.class)
}

2.一定不要忘记放行验证码接口

// 拦截设置
http.authorizeHttpRequests()//排除/login.antMatchers("/login","/code/image").permitAll();

SpringSecurity添加图形验证码认证功能相关推荐

  1. SpringBoot + SpringSecurity 短信验证码登录功能实现

    实现原理 在之前的文章中,我们介绍了普通的帐号密码登录的方式:SpringBoot + Spring Security 基本使用及个性化登录配置(http://www.deiniu.com/artic ...

  2. Spring Security添加图形验证码

    Spring Security添加图形验证码 大致思路: 1.根据随机数生成验证码图片 2.将验证码图片显示到登录页面 3.认证流程中加入验证码校验 依赖 <dependency>< ...

  3. IDaaS 系统 ArkID 一账通内置插件:图形验证码认证因素的配置流程

    图形验证码认证因素插件功能介绍 图形验证码认证因素插件对用户认证凭证表单进行扩充,插入图形验证码并实现相关验证功能,是 IDaaS 一账通 ArkID 系统内置功能插件之一. 注意:图形验证码认证因素 ...

  4. vue添加图形验证码功能

    上图看功能,每点击一次切换验证码!前端判断验证码是否输入,后端判断验证码是否正确! html <el-form-item label="验证码" prop="cod ...

  5. 4.Spring Security 添加图形验证码

    添加验证码大致可以分为三个步骤:根据随机数生成验证码图片:将验证码图片显示到登录页面:认证流程中加入验证码校验.Spring Security的认证校验是由UsernamePasswordAuthen ...

  6. SpringSecurity(一)核心功能

    前言 最近在潜心研究一下安全框架,并在此进行一下记录,如有不对还请不吝赐教. 对于一个安全管理管理框架而言,无论是Shiro还是SpringSecurity,最核心的功能,无非就是两个 认证:你是谁? ...

  7. SpringBoot生成图形验证码

    需求:验证码一码一用,验证之后,不管是成功还是失败,都需要重新获取或者刷新二维码. 大致思路:后端生成验证码后还需要生成一个UUID与之对应,存储到缓存(记得添加过期时间),把UUID和验证码反给前端 ...

  8. java实现图形验证码

    项目中可能会用到图形验证码的功能,源码分享给大家.以下是实际效果图: 一.后端JAVA代码 1.生成图形验证码工具类 public class imgVerifyCode {private int w ...

  9. 手把手带你在集成SpringSecurity的SpringBoot应用中添加短信验证码登录认证功能

    本文目录 前言 1 自定义AuthenticationToken类 2 自定义AuthenticationProvider类 3 自定义MobilePhoneAuthenticationFilter ...

最新文章

  1. 访问 Microsoft SQL Server 元数据的三种
  2. OK,让我们开始吧!
  3. 经典PID控制算法用C语言实现!
  4. Ubuntu18.04 MariaDB
  5. 饿了么口碑活跃用户增长近美团3倍,2020年行业竞争局势将扭转?
  6. Java内存组成GC算法
  7. FastTunnel - 打造人人都能搭建的内网穿透工具
  8. 商丘高中计算机考试成绩查询系统,2019商丘中考招生成绩查询时间及网站公布...
  9. 计算机应用基础专科作业二,电子科大18秋《计算机应用基础(专科)》在线作业2...
  10. 配置electron
  11. (42)Gulp在Yeoman脚手架工具中的应用
  12. maven只是经手,不是触发:org.apache.maven.lifecycle.LifecycleExecutionException
  13. 计算机绘图课程选用课本,机械制图课程学习指南
  14. Java 正则表达式匹配规则
  15. 推荐几个好用的 html5 游戏源码下载网站
  16. UniFi AP 5.5.20的基本使用与设置(普通漫游和无缝漫游)
  17. 使用帧相似度匹配编写无缝循环视频截取工具
  18. matlab sae模型,发动机平均值模型的三篇SAE论文
  19. Chrome 清除网站图标缓存,更新网站图标
  20. 地学计算方法/地统计学(第四章变异函数理论模型)

热门文章

  1. 毕业设计-基于Javaweb心理咨询预约管理系统
  2. 【论文阅读 WSDM‘21】PROP: Pre-training with Representative Words Prediction for Ad-hoc Retrieval
  3. 干货 | 利用OpenCV,Python和Ubidots来构建行人计数器程序(附代码解析)
  4. upupw修改到服务器上,如何用upupw添加云服务器
  5. 直播预告 | 如何迈向知识驱动的人工智能?
  6. Kali Linux渗透测试之提权(二)——WCE、Fgdump、Mimikatz
  7. 自动仓储系统(AS/RS)技术与装备
  8. java计算机毕业设计慧学IT精品课程网站源码+mysql数据库+系统+lw文档+部署
  9. word转换pdf的python代码_python word转pdf代码实例
  10. 澳门大学的计算机科学是强科吗,澳门大学计算机专业大学排名