<form action="<%=basePath %>user/login" method="post">
用户名:<input type="text" name="userName">
<br>
密码:<input type="password" name="password">
<br>
验证码:<input type="text" name="code" ><img id="codeImg" title="点击获取新验证码"
src="<%=path%>/verifycode/getVerifyCodeImage"
onclick="changeimg()" width="62" height="28" alt=""
style="display:inline-block;">
<input type="submit" value="登陆">
</form>
登陆逻辑
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String pcLogin(String userName, String password, String code, HttpSession session) {
String sessionCode = (String) session.getAttribute(Constants.SESSION_LOGIN_CODE);
System.out.println(userName);
if (!StringUtil.isNullOrEmpty(userName) && !StringUtil.isNullOrEmpty(password)
&& !StringUtil.isNullOrEmpty(sessionCode) && sessionCode.equalsIgnoreCase(code)) {
// 通过用户名查找用户
UserInfo user = userInfoService.getUserByLoginName(userName);
System.out.println(user);
System.out.println(user.getLoginName());
if (user != null) {
if (user.getPwd().equals(password) && user.getStatus() == 1) {
session.setAttribute(Constants.CURRENT_USER, user);
return "demo";
}
}
}
return "userinfo";
}
userInfoMapper.xml
<select id="selectByLoginName" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from user_info
where login_name = #{loginName,jdbcType=VARCHAR}
</select>
验证码
/**
* 验证码
*
*/
@Controller
@RequestMapping(value = "/verifycode")
public class VerifyCodeController {
@RequestMapping(value = "/getVerifyCodeImage")
public void getVerifyCodeImage(String type,HttpServletRequest request, HttpServletResponse response) throws IOException {
// 设置验证码字符的字体和字号。
Font mFont = new Font("Arial Black", Font.PLAIN, 22);
//清除缓存,每次访问该页面时都从服务器端读取
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
// 设置验证码图片的长度和高度。
int width = 86, height = 40;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
//画图画板
Graphics g = image.getGraphics();
//定义一个随机数
Random random = new Random();
//设置画板背景颜色
// g.setColor(getRandColor(200, 250));
g.setColor(new Color(160, 177, 185));
//设置画板的填充范围
g.fillRect(1, 1, width - 1, height - 1);
// g.setColor(new Color(102, 102, 102));
g.drawRect(0, 0, width - 1, height - 1);
//设置字体
g.setFont(mFont);
//显示字符串,4位长度。
RandomValidateCode randomValidateCode = new RandomValidateCode();
String sRand = randomValidateCode.generate();
for (int i = 0; i < 4; i++) {
String tmp = sRand.substring(i, i+1);
//设置每个数字的颜色
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
//在画板上写数字,起始位置
g.drawString(tmp, 20 * i + 5, 27);
}
HttpSession session = request.getSession();
// 把验证码防到session中,用来前台对比。
session.setAttribute((type ==null || type.equals("1"))?Constants.SESSION_LOGIN_CODE:Constants.SESSION_REGISTER_CODE, sRand);
//显示图片
g.dispose();
//转换成一张图片,格式为JPEG
ImageIO.write(image, "JPEG", response.getOutputStream());
}
/**
* 随机获得颜色,RGB格式
*
* @param fc
* @param bc
* @return
*/
Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
}
过滤机制
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
UserInfo user = (UserInfo) request.getSession().getAttribute(Constants.CURRENT_USER);
String url = request.getRequestURI() != null ? request.getRequestURI().toString() : null;
System.out.println("url-->" + url);
// System.out.println("user-->" + user.toString());
if (user == null) {
System.out.println("user is null");
response.sendRedirect(request.getContextPath() + "/user/toLogin");
return false;
} else {
return true;
}
}
3、遇到的问题
4、解决问题
5、项目燃尽图更新
原文:https://www.cnblogs.com/mhwl-wll/p/13330551.html