【SSM 验证码】登录验证码
2024-08-31 23:15:09
LoginController
/**
* 登陆方法
*/
@ResponseBody
@RequestMapping("login2")
public Map<String, String> login2(UserVo userVo, Model model) {
Map<String, String> res = new HashMap();
String code = WebUtils.getHttpSession().getAttribute("code").toString();
if(!userVo.getCode().equals(code)){
model.addAttribute("error", SysConstast.USER_LOGIN_ERROR_MSG);
res.put("code", "400");
res.put("message", "验证码错误");
return res;
}
User user = this.userService.login(userVo);
if (null == user) {
model.addAttribute("error", SysConstast.USER_LOGIN_ERROR_MSG);
res.put("code", "302");
res.put("address", "../login/toLogin.action");
return res;
}
//放到session
WebUtils.getHttpSession().setAttribute("user", user);
//记录登陆日志 向sys_login_log里面插入数据
LogInfoVo logInfoVo = new LogInfoVo();
logInfoVo.setLogintime(new Date());
logInfoVo.setLoginname(user.getRealname() + "-" + user.getLoginname());
logInfoVo.setLoginip(WebUtils.getHttpServletRequest().getRemoteAddr()); logInfoService.addLogInfo(logInfoVo);
res.put("code", "200");
res.put("address", "../login/mainIndex.action");
return res;
} @RequestMapping("mainIndex")
public String mainIndex() {
return "system/main/index";
} /**
* 得到登陆验证码
*
* @throws IOException
*/
@RequestMapping("getCode")
public void getCode(HttpServletResponse response, HttpSession session) throws IOException {
// 定义图形验证码的长和宽
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36, 4, 5);
session.setAttribute("code", lineCaptcha.getCode());
ServletOutputStream outputStream = response.getOutputStream();
ImageIO.write(lineCaptcha.getImage(), "JPEG", outputStream);
}
login.jsp
<div class="layui-form-item input-item" id="imgCode">
<label for="code">验证码</label>
<input type="text" placeholder="请输入验证码" autocomplete="off" name="code" id="code" class="layui-input">
<img src="${ctx}/login/getCode.action" onclick="this.src=this.src+'?'">
</div>
最新文章
- WPF 子窗体关闭时显示父窗体
- springMVC下集成active MQ发送邮件
- js鼠标滑轮滚动事件绑定(兼容主流浏览器)
- storm如何分配任务和负载均衡?
- 安卓Json介绍(转)。
- C#去除HTML标签(转)
- .Net高级技术
- 解决手机浏览器和微信中select中border:none;无用和去掉小三角
- Oracle笔记之对象权限与系统权限总结
- Link方式导入java项目
- I - Intersection HDU - 5120(圆环相交面积)
- [bzoj4240] 有趣的家庭菜园
- Spark2.4.0伪分布式环境搭建
- 远程连接centos7 上的mysql报(ERROR 2003 (HY000): Can&#39;t connect to MySQL server on &#39;168.x.x.x&#39; (10060) )
- 钉钉C# 使用数据接口要注意的问题
- spring+springMvc+struts的SSH框架整合
- vue教程2-03 vue计算属性的使用 computed
- activiti工作流
- &hellip;gen already exists but is not a source folder. Convert to a source folder or rename it [closed]
- Spring Boot 系统要求