• SpringBoot登入页面图片验证码


    1.依赖坐标 pom.xml文件

    
    
        cn.hutool
        hutool-all
        5.4.3
    

    2.启动类LoginCodeApplication 

    1. package com.logincode;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. @SpringBootApplication
    5. public class LoginCodeApplication {
    6. public static void main(String[] args) {
    7. SpringApplication.run(LoginCodeApplication .class, args);
    8. }
    9. }

    3.配置文件application.yml 

    # 应用服务 WEB 访问端口
    server.port=8989
    

    4.web层代码:生成验证码

    1. package com.logincode.web;
    2. import cn.hutool.captcha.CaptchaUtil;
    3. import cn.hutool.captcha.CircleCaptcha;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.web.bind.annotation.GetMapping;
    6. import org.springframework.web.bind.annotation.RequestMapping;
    7. import org.springframework.web.bind.annotation.RestController;
    8. import javax.servlet.http.HttpServletResponse;
    9. import javax.servlet.http.HttpSession;
    10. import java.io.IOException;
    11. @RestController
    12. @RequestMapping("/user")
    13. public class LoginCode {
    14. @Autowired
    15. private HttpServletResponse response;
    16. @Autowired
    17. private HttpSession session;
    18. /**
    19. * 功能:生成验证码
    20. *
    21. */
    22. @GetMapping("/code")
    23. void getCode() throws IOException {
    24. // 利用hutool工具,生成验证码图片资源
    25. CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 5);
    26. // 获得生成的验证码字符串
    27. String code = captcha.getCode();
    28. // 利用session来存储 验证码
    29. session.setAttribute("code", code);
    30. //将验证码图片的二进制数据响应体中response
    31. captcha.write(response.getOutputStream());
    32. }
    33. }

    5.前端页面inder.html 

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>用户登入界面title>
    6. head>
    7. <body>
    8. 登入界面
    9. <form action="#" method="post">
    10. 用户username:<input type="text" name="username"><br/>
    11. 密码password:<input type="password" name="password"><br/>
    12. 验证码code:<input type="code" name="code"><br/>
    13. <img src="/user/code" id="code" onclick="refresh()">
    14. <br/>
    15. <input type="submit" value="登入/注册">
    16. <br/>
    17. form>
    18. <a href="/logout.do">退 出a>
    19. body>
    20. <script>
    21. function refresh() {
    22. document.getElementById("code").src = "/user/code?time" + new Date().getTime();
    23. }
    24. script>
    25. html>

    6.展示界面---生成验证码成功

  • 相关阅读:
    我的创作纪念日
    必看!2023年最新MSP开源应用程序指南电子书大揭秘
    【深入浅出设计模式--状态模式】
    NOSQL Redis 数据持久化 RDB、AOF(二) 恢复
    结构型模式-桥接模式
    英伟达与斯坦福携手,打造未来全息XR眼镜:头带时代的终结
    C++ 指针和引用引用详解
    四大含金量高的算法证书考试
    蓝桥杯刷题-约数的个数
    【python高级】设计模式、类工厂、对象工厂
  • 原文地址:https://blog.csdn.net/m0_61601521/article/details/126373558