登录页
login.htm 模板页
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户登录</title>
<link href="/css/common.css" rel="stylesheet" type="text/css" />
<link href="/css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="wrap">
<!-- main begin-->
<div class="main">
<div class="sidebar">
<p><img src="images/img1.jpg" width="443" height="314" alt="" /></p>
</div>
<div class="sidebarg">
<form action="/doLogin" method="post">
<div class="login">
<dl>
<dt class="blues">用户登陆</dt>
<dd><label for="name">用户名:</label><input type="text" class="inputh" name="username"/></dd>
<dd><label for="password">密 码:</label><input type="password" class="inputh" name="password"/></dd>
<dd>
<label class="lf" for="passwords">验证码:</label>
<input type="text" class="inputh inputs lf" name="valideCode"/>
<span class="wordp lf">
<img th:src="@{/defaultKaptcha}" onclick="this.src='/defaultKaptcha?time='+new Date()" width="96" height="27" alt="" />
</span>
</dd>
<dd>
<input name="" type="checkbox" id="rem_u" />
<span for="rem_u">下次自动登录</span>
<span style="color: red;font-size: 18px;font-weight: bold" th:text="${errorMsg}"></span>
</dd>
<dd class="buttom">
<input type="submit" value="登 录" class="spbg buttombg f14 lf" />
<input type="button" value="注 册" onclick="location='toRegister'" class="spbg buttombg f14 lf" />
<span class="blues lf"><a href="" title="">忘记密码?</a></span>
<div class="cl"></div>
</dd>
</dl>
</div>
</form>
</div>
<div class="cl"></div>
</div>
<!-- main end-->
<!-- footer begin-->
</div>
<!--footer end-->
</div>
</body>
</html>
新建一个 config 包
创建验证码配置
package com.web.action.config;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class KaptchaConfig {
@Bean
public DefaultKaptcha getDefaultKaptcha() {
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
Properties properties = new Properties();
// 图片边框
properties.setProperty("kaptcha.border", "yes");
// 边框颜色
properties.setProperty("kaptcha.border.color", "105,179,90");
// 字体颜色
properties.setProperty("kaptcha.textproducer.font.color", "red");
// 图片宽
properties.setProperty("kaptcha.image.width", "110");
// 图片高
properties.setProperty("kaptcha.image.height", "40");
// 字体大小
properties.setProperty("kaptcha.textproducer.font.size", "35");
// session key
properties.setProperty("kaptcha.session.key", "code");
// 验证码长度
properties.setProperty("kaptcha.textproducer.char.length", "4");
// 字体
properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
新建一个 controller 包
创建 UserController
获取验证码
package com.web.action.controller;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@Controller
public class UserController {
//自动注入 验证码配置
@Autowired
private DefaultKaptcha defaultKaptcha;
/**
* 获取验证码
* @param request
* @param response
*/
@RequestMapping("/defaultKaptcha")
public void defaultKaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
//定义一个 byte 数组
byte[] captcha = null;
//字节输出流
ByteArrayOutputStream jpeg = new ByteArrayOutputStream();
try{
//生成验证码字符串并保存到 session 中
String createText = defaultKaptcha.createText();
request.getSession().setAttribute("vrifyCode", createText);
//使用生成的验证码字符串返回一个 BufferedImage 对象并转为 byte 写入到 byte 数组中
BufferedImage challenge = defaultKaptcha.createImage(createText);
//将图片写如 字节输出流
ImageIO.write(challenge,"jpg",jpeg);
} catch (IOException e) {
//抛出异常 404
response.sendError(response.SC_NOT_FOUND);
//结束方法
return;
}
//定义 response 输出类型为 image/jpeg 类型,
//使用 response 输出流输出图片的 byte 数组
captcha = jpeg.toByteArray();
/**
* Cache-Control指定请求和响应遵循的缓存机制
* Public指示响应可被任何缓存区缓存。
* Private指示对于单个用户的整个或部分响应消息,不能被共享缓存处理。这允许服务器仅仅描述当用户的部分响应消息,此响应消息对于其他用户的请求无效。
* no-cache指示请求或响应消息不能缓存
* no-store用于防止重要的信息被无意的发布。在请求消息中发送将使得请求和响应消息都不使用缓存。
* max-age指示客户机可以接收生存期不大于指定时间(以秒为单位)的响应。
* min-fresh指示客户机可以接收响应时间小于当前时间加上指定时间的响应。
* max-stale指示客户机可以接收超出超时期间的响应消息。如果指定max-stale消息的值,那么客户机可以接收超出超时期指定值之内的响应消息。
*/
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
/**
* response.setContentType的作用是使客户端浏览器
* 区分不同种类的数据
* 并根据不同的MIME调用浏览器内不同的程序嵌入模块来处理相应的数据。
*/
response.setContentType("image/jpeg");
/**
* ServletOutputStream类提供了将二进制数据写入响应的流。这是一个抽象类。
* ServletResponse接口的getOutputStream()方法返回ServletOutputStream类的实例
*/
ServletOutputStream responseOutputStream = response.getOutputStream();
responseOutputStream.write(captcha);
//强制把数据输出
responseOutputStream.flush();
//关闭流
responseOutputStream.close();
}
}