SpringSecurity集成图片验证码的详细过程

 更新时间:2023年12月11日 15:03:39   作者:-代号9527  
SpringSecurity是通过过滤器链来完成的,接下来的验证码,可以尝试创建一个过滤器放到Security的过滤器链中,在自定义的过滤器中比较验证码,本文通过实例代码介绍SpringSecurity集成图片验证码的详细过程,感兴趣的朋友一起看看吧

SpringSecurity是通过过滤器链来完成的,接下来的验证码,可以尝试创建一个过滤器放到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 {
        //创建一个验证码
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(200, 100, 2, 20);
        //获取生成的图片二维码中的值,并放到session中
        String captchaCode=circleCaptcha.getCode();
        log.info("生成的验证码为:{}",captchaCode);
        request.getSession().setAttribute("LOGIN_CAPTCHA_CODE",captchaCode);
        //将图片写到响应流中,参数一是图片。参数二是图片格式,参数三是响应流
        ImageIO.write(circleCaptcha.getImage(),"JPEG",response.getOutputStream());
    }
}

此时,调用这个接口会在响应里返回一个图片。调用下接口看看效果:

在这里插入图片描述

2、创建验证码过滤器

很明显,校验验证码要先于校验用户名密码,验证码都不对,就不用往下验证用户名和密码了。新建自定义过滤器类ValidateCodeFilter,继承抽象类OncePerRequestFilter 。右键看下继承关系:

在这里插入图片描述

可以看到最终是实现了Filter接口。但这里别直接实现Filter,继承OncePerRequestFilter,里面的好多东西直接用,能省一点是一点。过滤器的实现思路为:

  • 从前端获取验证码
  • 从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);  //验证成功,放行
        }
    }
}

关于requset.getParameter("code")

关于在controller中接收前端数据的方式

关于直接在请求中获取参数的建议:

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 {
        //设置登陆方式
        http.formLogin()//使用用户名和密码的登陆方式
                .usernameParameter("uname") //页面表单的用户名的name
                .passwordParameter("pwd")//页面表单的密码的name
                .loginPage("/login/toLogin") //自己定义登陆页面的地址
                .loginProcessingUrl("/login/doLogin")//配置登陆的url
                .successForwardUrl("/index/toIndex") //登陆成功跳转的页面
                .failureForwardUrl("/login/toLogin")//登陆失败跳转的页面
                .permitAll();
        //配置退出方式
        http.logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login/toLogin")
                .permitAll();
        //配置路径拦截 的url的匹配规则
        http.authorizeRequests().antMatchers("/code/image").permitAll()
                //任何路径要求必须认证之后才能访问
                .anyRequest().authenticated();
        // 禁用csrf跨站请求,注意不要写错了
        http.csrf().disable();
  		// 配置登录之前添加一个验证码的过滤器      
  		http.addFilterBefore(validateCodeFilter,UsernamePasswordAuthenticationFilter.class);
    }
    /**
     * 资源服务匹配放行【静态资源文件】
     *
     * @param web
     * @throws Exception
     */
   // @Override
    //public void configure(WebSecurity web) throws Exception {
      //  web.ignoring().mvcMatchers("/resources/**");
    //}
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

以上注意两点,一是别忘了放行生成二维码的接口,这个不需要登录鉴权。

http.authorizeRequests()
	.antMatchers("/code/image")
	.permitAll()
    //任何路径要求必须认证之后才能访问
    .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>

效果:

在这里插入图片描述

到此这篇关于SpringSecurity集成图片验证码的文章就介绍到这了,更多相关SpringSecurity图片验证码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Spring异常捕获且回滚事务解决方案

    Spring异常捕获且回滚事务解决方案

    这篇文章主要介绍了Spring异常捕获且回滚事务解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • 详解SpringCloud Zuul过滤器返回值拦截

    详解SpringCloud Zuul过滤器返回值拦截

    Zuul作为网关服务,是其他各服务对外中转站,通过Zuul进行请求转发。这篇文章主要介绍了详解SpringCloud Zuul过滤器返回值拦截,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • java String 转成Double二维数组的方法

    java String 转成Double二维数组的方法

    下面小编就为大家带来一篇java String 转成Double二维数组的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-10-10
  • SpringBoot系列教程JPA之基础环境搭建的方法

    SpringBoot系列教程JPA之基础环境搭建的方法

    这篇文章主要介绍了SpringBoot系列教程JPA之基础环境搭建的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-06-06
  • Apache Log4j2 报核弹级漏洞快速修复方法

    Apache Log4j2 报核弹级漏洞快速修复方法

    Apache Log4j2 是一个基于Java的日志记录工具,是 Log4j 的升级,是目前最优秀的 Java日志框架之一,这篇文章主要介绍了突发Apache Log4j2 报核弹级漏洞快速修复方法,需要的朋友可以参考下
    2021-12-12
  • java 读取本地文件实例详解

    java 读取本地文件实例详解

    这篇文章主要介绍了java 读取本地文件实例详解的相关资料,需要的朋友可以参考下
    2017-05-05
  • springboot+idea热启动设置方法(自动加载)

    springboot+idea热启动设置方法(自动加载)

    这篇文章主要介绍了springboot+idea热启动设置方法(自动加载),本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-01-01
  • 使用 Spring Boot 2.0 + WebFlux 实现 RESTful API功能

    使用 Spring Boot 2.0 + WebFlux 实现 RESTful API功能

    什么是 Spring WebFlux, 它是一种异步的, 非阻塞的, 支持背压(Back pressure)机制的Web 开发框架.下面通过本文给大家介绍使用 Spring Boot 2.0 + WebFlux 实现 RESTful API功能,需要的朋友参考下吧
    2018-01-01
  • Java JNDI案例详解

    Java JNDI案例详解

    这篇文章主要介绍了Java JNDI案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • IDEA中使用jclasslib插件可视化方式查看类字节码的过程详解

    IDEA中使用jclasslib插件可视化方式查看类字节码的过程详解

    查看JAVA字节码有两种方式一种是使用 jdk命令 javap,还有一种就是 使用 插件了,今天给大家分享IDEA中使用jclasslib插件可视化方式查看类字节码的过程详解,感兴趣的朋友跟随小编一起看看吧
    2021-05-05

最新评论