SpringBoot中实现验证码功能

1、导入依赖jar包,该jar包提供了一个图形验证码工具类CaptchaUtil

<!--引入hutool-->
     <dependency>
         <groupId>cn.hutool</groupId>
         <artifactId>hutool-all</artifactId>
         <version>5.3.9</version>
     </dependency>

在这里插入图片描述

2、编写获取验证码的Controller

/*
 * 获取验证码的Controller
 */
@Controller
@RequestMapping("code")
public class CodeValidate {

    /**
     * 访问该路径生成验证码
     * @param req
     * @param resp
     */
    @GetMapping("image")
    public void code(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 创建一个验证码  长,宽,字符数,干扰元素个数
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100, 4, 10);
        // 将验证码放到session中以供前端获取展示
        String code = lineCaptcha.getCode();//真实验证码
        req.getSession().setAttribute("code",code);
        // 用流写出去
        lineCaptcha.write(resp.getOutputStream());
    }
}

3、设置验证码过滤器实现Filter接口,进行登陆前的验证

/*
 * 检查验证码的合法性
 */
@Component
public class CodeValidateFiler implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req =(HttpServletRequest)servletRequest;
        HttpServletResponse resp =(HttpServletResponse)servletResponse;
        //获取当前请求的Url
        String requestURI = req.getRequestURI();
        //判断 如果是登录的请求,需要验证code,不是登录 就放行
        if(requestURI.equals("/login/dologin")){
            // 获取存放到session中的验证码
            String codeVali =  req.getSession().getAttribute("code").toString();
            // 获取传过来的验证码
            String code = req.getParameter("code");
            //判断传过来的验证码是否存在
            if(StringUtils.hasText(code)){
                // 将传过来的验证码与生成的验证码进行比较
                if(code.equals(codeVali)){
                    // 相等放行
                    filterChain.doFilter(req,resp);
                }else{
                    // 不相等重新登录
                    req.setAttribute("error","验证码不正确");
                    resp.sendRedirect("/login/tologin");
                }
            }else{
                // 验证码为空重定向到登录页
                req.getSession().setAttribute("error","验证码不能为空");
                resp.sendRedirect("/login/tologin");
            }
        }else{
            // 其他路径放行
            filterChain.doFilter(req,resp);
        }
    }
}

4、注册进HttpSecurity中

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)//开启方法级别的权限认证  默认是false
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
  		        http.exceptionHandling().accessDeniedHandler(appAuthDenyHander);
        //在登录验证之前,先判断验证是否正确,因此验证码过滤器要在身份验证过滤器之前进行过滤
        http.addFilterBefore(codeValidateFiler, UsernamePasswordAuthenticationFilter.class);
        http.formLogin().successHandler(appAuthSuccessHandler).failureHandler(appAuthFailHandler)
                .usernameParameter("username")
                .passwordParameter("password")//自定义表单用户名和密码的参数名
                .loginPage("/login/toLogin")//自定义的,登录页面
                .loginProcessingUrl("/login/doLogin")//真正登录接口
                .successForwardUrl("/login/main")  //登录成功跳转的url
                .failureForwardUrl("/login/toLogin") //登录失败跳转登录页
                .permitAll();//全部放行
        //自定义退出
        http.logout()
                .logoutUrl("/lout")//自定义的退出路径
                .logoutSuccessUrl("/login/toLogin") //退出成功后的跳转路径
                .permitAll();
	}
}

5、编写登陆页

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/login/doLogin" method="post">
    <div>
        用户名: <input type="text" name="username">
    </div>
    <div>
        密碼: <input type="text" name="password">
    </div>
    <div>
        验证码: <input type="text" name="code">
        <img src="/code/image" alt="">
        <span style="color: red" th:text="${session.error}"></span>
    </div>
    <div>
        <input type="submit" value="登录">
    </div>
</form>
</body>
</html>

5、测试
在这里插入图片描述

实现验证码功能可以分为前端和后端两部分。 前端部分: 在 Vue3 ,可以使用第三方库如 vue-ant-design、vue-element-admin 等提供的验证码组件,或者使用原生 HTML5 canvas 绘制验证码。以下是一个使用 canvas 绘制验证码的示例: 1. 在 template 添加 canvas 标签: ```html <canvas ref="canvas" @click="drawCode"></canvas> ``` 2. 在 script 定义绘制验证码的方法: ```javascript export default { name: 'VerificationCode', data() { return { code: '' } }, methods: { drawCode() { let canvas = this.$refs.canvas let ctx = canvas.getContext('2d') let width = canvas.width let height = canvas.height // 绘制背景色 ctx.fillStyle = '#eee' ctx.fillRect(0, 0, width, height) // 绘制验证码 let code = '' let charArr = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.split('') for (let i = 0; i < 4; i++) { let char = charArr[Math.floor(Math.random() * charArr.length)] code += char ctx.font = 'bold 20px Arial' ctx.fillStyle = '#333' ctx.fillText(char, 20 + i * 20, 25) } this.code = code } } } ``` 3. 调用 drawCode 方法生成验证码。 后端部分: 在 Spring Boot ,可以使用第三方库 kaptcha 来生成验证码。以下是一个使用 kaptcha 生成验证码的示例: 1. 添加依赖: ```xml <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency> ``` 2. 在 Controller 添加生成验证码的方法: ```java @GetMapping("/captcha") public void captcha(HttpServletResponse response, HttpSession session) throws Exception { // 创建 kaptcha 对象 DefaultKaptcha kaptcha = new DefaultKaptcha(); // 设置配置 Properties props = new Properties(); props.put("kaptcha.border", "no"); props.put("kaptcha.textproducer.font.color", "black"); props.put("kaptcha.image.width", "100"); props.put("kaptcha.image.height", "40"); props.put("kaptcha.textproducer.font.size", "30"); kaptcha.setConfig(new Config(props)); // 生成验证码 String text = kaptcha.createText(); session.setAttribute("captcha", text); // 输出图片 BufferedImage image = kaptcha.createImage(text); OutputStream out = response.getOutputStream(); ImageIO.write(image, "jpg", out); out.close(); } ``` 3. 在前端页面使用 img 标签获取验证码图片: ```html <img src="/captcha" alt="验证码" @click="refreshCode"> ``` 4. 在提交表单时把用户输入的验证码和 session 验证码进行比较,判断是否正确。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值