1.依赖坐标 pom.xml文件
cn.hutool
hutool-all
5.4.3
2.启动类LoginCodeApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class LoginCodeApplication {
public static void main(String[] args) {
SpringApplication.run(LoginCodeApplication .class, args);
3.配置文件application.yml
# 应用服务 WEB 访问端口
server.port=8989
4.web层代码:生成验证码
package com.logincode.web;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
private HttpServletResponse response;
private HttpSession session;
void getCode() throws IOException {
CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 5);
String code = captcha.getCode();
session.setAttribute("code", code);
captcha.write(response.getOutputStream());
5.前端页面inder.html
<form action="#" method="post">
用户username:<input type="text" name="username"><br/>
密码password:<input type="password" name="password"><br/>
验证码code:<input type="code" name="code"><br/>
<img src="/user/code" id="code" onclick="refresh()">
<input type="submit" value="登入/注册">
<a href="/logout.do">退 出a>
document.getElementById("code").src = "/user/code?time" + new Date().getTime();
6.展示界面---生成验证码成功
