005--验证码及应用于登录案例,idea技巧之开启热部署..

发布时间 2023-07-26 20:29:18作者: Mr_zzl

参考:SpringBoot | 稀客大大 (heyige.cn)

自勉:勤则百病皆消,懒则诸事不顺

1. idea技巧之-定位已经打开的代码,在文件夹中的位置

2. idea技巧之-在idea上改变数据库表

最后别忘记点击OK

3. 验证码使用
依赖
<dependency>
    <groupId>com.github.whvcse</groupId>
    <artifactId>easy-captcha</artifactId>
    <version>1.6.2</version>
</dependency>
参考使用
@GetMapping("/captcha")
public void getCaptcha(HttpServletResponse response) throws IOException {
    ServletOutputStream outputStream = response.getOutputStream();
    // 算术验证码
    // ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(120,40);

    // 中文验证码
    // ChineseCaptcha captcha =new ChineseCaptcha(120, 40);

    // 英文与数字验证码
    // SpecCaptcha captcha = new SpecCaptcha(120, 40);

    // 英文与数字动态验证码
    // GifCaptcha captcha = new GifCaptcha(120, 40);

    // 中文动态验证码
    ChineseGifCaptcha chineseCaptcha = new ChineseGifCaptcha();//直接生成验证码对象;
    chineseCaptcha.setLen(2); //生成的验证码位数
    System.out.println(chineseCaptcha.text()); //控制台打印验证码文本内容
    chineseCaptcha.out(outputStream);//使用输出流生成验证码图片
}
实际使用
@GetMapping("captcha")
    public void captcha(HttpServletResponse response, HttpSession session) throws IOException {
        ServletOutputStream out = response.getOutputStream();//从响应对象获取一个输出流对象;用于输出图片文件

        // ArithmeticCaptcha captcha = new ArithmeticCaptcha();//数字验证码对象
        SpecCaptcha captcha = new SpecCaptcha();//生成一个验证码对象
        // captcha.setLen(3);
        String text = captcha.text();//提取验证码文本内容
        session.setAttribute("captcha", text);//将验证码的值存放在session中
        System.out.println("获取的验证码文字是:" + text);//打印验证码的文字内容
        captcha.out(out);//使用响应对象的输出流输出到前端
    }
改变验证码尺寸
SpecCaptcha captcha = new SpecCaptcha(400,50);

4. idea开启热部署注:只有开启debug功能才能进行热部署,直接启动项目不会热部署

5. 验证码应用案例,登录使用验证码案例
验证码接口代码
@GetMapping("captcha")
    public void captcha(HttpServletResponse response, HttpSession session) throws IOException {
        ServletOutputStream out = response.getOutputStream();//从响应对象获取一个输出流对象;用于输出图片文件

        // ArithmeticCaptcha captcha = new ArithmeticCaptcha();//数字验证码对象
        SpecCaptcha captcha = new SpecCaptcha();//生成一个验证码对象
        // captcha.setLen(3);
        String text = captcha.text();//提取验证码文本内容
        session.setAttribute("captcha", text);//将验证码的值存放在session中
        System.out.println("获取的验证码文字是:" + text);//打印验证码的文字内容
        captcha.out(out);//使用响应对象的输出流输出到前端
    }

以上代码中将验证码的值塞到了session中;login接口的代码才能够应用session中存储的验证码值和前端传回的值进行对比从而让进行登陆验证

login代码:
@PostMapping("login")
    public String login(String username, String password, String code, HttpSession session, HttpServletRequest request) {
        // 进行登录
        String captcha = (String) session.getAttribute("captcha");
        // 比对session中的验证码和用户传过来的验证码
        if (captcha.equalsIgnoreCase(code)) {
            // 验证码正确,开始登录
            R r = userService.login(username, password);//login执行成功之后;R自动塞进去200
            if (Objects.equals(r.getCode(), R.SUCCESS)) { //SUCCESS静态的200
                
                // 登录成功后 重定向到日报列表接口
                return "redirect:jsp/daily/list";
            } else {
                request.setAttribute("error", "用户名或密码错误");
            }
        } else {
            request.setAttribute("error", "验证码错误");
        }
        return "index";
    }

上图中,${error}元素值取自request对象;该元素是在login时;通过setAttribute添加的

前端页面中显示验证码图片

上图中有代码如下

<img src="/captcha" alt="验证码"><br/>

此时,不同于异步请求;img标签的src会自动发送get请求(在页面加载的时候);