springboot+thymeleaf+druid+mybatis 多模块实现用户登录功能

时间:2021-05-19

项目代码:https://github.com/bruceq/supermarket

项目结构:

依赖关系:

common:公共层,无依赖

dao:数据层,依赖common

service:服务层,依赖dao、common

web:应用层,依赖dao、common、service

注:启动类在web层中

父依赖pom

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://.loan.supermarket.mapper.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import org.thymeleaf.util.StringUtils; import javax.imageio.ImageIO;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.awt.image.BufferedImage;import java.io.ByteArrayOutputStream;import java.util.ArrayList;import java.util.Date; @Controllerpublic class TemplateController { @RequestMapping("/index") public String index(Model model) { User user = new User(); user.setName("张三"); user.setId(1L); user.setTelephone("123456789"); user.setPassword("@#$%^&*()"); user.setRegisterTime(new Date()); user.setPopedom(1); User user1 = new User(); user1.setName("李四"); user1.setId(2L); user1.setTelephone("2222222"); user1.setPassword("最近是南风天"); user1.setRegisterTime(new Date()); user1.setPopedom(1); User user2 = new User(); user2.setName("王五"); user2.setId(3L); user2.setTelephone("33333333333"); user2.setPassword("妖姬"); user2.setRegisterTime(new Date()); user2.setPopedom(1); User user3 = new User(); user3.setName("赵六"); user3.setId(4L); user3.setTelephone("33333333333"); user3.setPassword("过河卒"); user3.setRegisterTime(new Date()); user3.setPopedom(0); ArrayList<User> list = new ArrayList<User>(); list.add(user); list.add(user1); list.add(user2); list.add(user3); model.addAttribute("users", list); return "index"; } @RequestMapping("/login") public String login1() { return "login"; } @PostMapping("/user_login") @ResponseBody public JsonObject<String> login(String username, String password, HttpServletRequest request) { JsonObject<String> obj = new JsonObject<String>(); HttpSession session = request.getSession(); if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) { obj.setCode("0"); User user = new User(); user.setName(username); session.setAttribute("loginUser", user); return obj; } obj.setCode("1"); obj.setMessage("用户名或密码错误"); return obj; } @Autowired private DefaultKaptcha captchaProducer; /** * 获取验证码 的 请求路径 * @param httpServletRequest * @param httpServletResponse * @throws Exception */ @RequestMapping("/defaultKaptcha") public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception{ byte[] captchaChallengeAsJpeg = null; ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream(); try { //生产验证码字符串并保存到session中 String createText = captchaProducer.createText(); httpServletRequest.getSession().setAttribute("vrifyCode", createText); //使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中 BufferedImage challenge = captchaProducer.createImage(createText); ImageIO.write(challenge, "jpg", jpegOutputStream); } catch (IllegalArgumentException e) { httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND); return; } //定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组 captchaChallengeAsJpeg = jpegOutputStream.toByteArray(); httpServletResponse.setHeader("Cache-Control", "no-store"); httpServletResponse.setHeader("Pragma", "no-cache"); httpServletResponse.setDateHeader("Expires", 0); httpServletResponse.setContentType("image/jpeg"); ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream(); responseOutputStream.write(captchaChallengeAsJpeg); responseOutputStream.flush(); responseOutputStream.close(); } /** * 验证的方法 * @param httpServletRequest * @param httpServletResponse * @return */ @RequestMapping("/imgvrifyControllerDefaultKaptcha") public ModelAndView imgvrifyControllerDefaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){ ModelAndView andView = new ModelAndView(); String captchaId = (String) httpServletRequest.getSession().getAttribute("vrifyCode"); String parameter = httpServletRequest.getParameter("vrifyCode"); System.out.println("Session vrifyCode "+captchaId+" form vrifyCode "+parameter); if (!captchaId.equals(parameter)) { andView.addObject("info", "错误的验证码"); andView.setViewName("index"); } else { andView.addObject("info", "登录成功"); andView.setViewName("success"); } return andView; }}

搭配前台的代码一起分析:

<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head> <title>显示学生信息</title> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1"/> <link th:href="@{/bootstrap/bootstrap.css}" href="../static/bootstrap/bootstrap.css" rel="stylesheet"/> <link th:href="@{/bootstrap/bootstrap-theme.css}" href="../static/bootstrap/bootstrap-theme.css" rel="stylesheet"/> <script th:src="@{/js/jquery.min.js}" src="../static/js/jquery.min.js"></script> <script th:src="@{/js/bootstrap.min.js}" src="../static/js/bootstrap.min.js"></script> <script type="text/javascript"> $(function () { $("#submit").click(function () { $.post("/user_login", $("#loginForm").serialize(), function (data) { if (data.code == "0") { window.location.href = "/index"; } else if (data.code == "1") { $("#tick").text(data.message).css("color", "red"); } }) }); }); </script></head><body><div class="container"> <div class="row"> <form class="form-horizontal" id="loginForm"> <div class="col-md-4"> <div class="panel panel-primary"> <div class="panel-heading text-center"> <span class="panel-title">学生信息</span> </div> <div class="form-group"> <label for="username" class="col-sm-3 control-label" style="color: red">账&nbsp;号:</label> <div class="col-sm-8"> <input type="text" class="form-control" id="username" name="username" placeholder="请输入账号"> </div> </div> <div class="form-group"> <label for="password" class="col-sm-3 control-label" style="color: red">密&nbsp;码:</label> <div class="col-sm-8"> <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码"> </div> </div> <div class="form-group"> <label class="col-sm-10 control-label" style="color:red;text-align: center" id="tick">&nbsp;</label> </div> <div class="form-group"> <form action="imgvrifyControllerDefaultKaptcha"> <div class="col-sm-8"> <input type="text" name="vrifyCode" class="form-control"/> </div> <img alt="验证码" onclick="this.src='defaultKaptcha?d='+new Date()*1" src="defaultKaptcha"> </br> </form> </div> <div class="form-group"> <div class="col-sm-8"> <button type="button" id="submit" class="btn btn-default">登录</button> </div> </div> <div class="panel-footer text-right"> <span class="panel-title">大侠工作室@2108</span> </div> </div> </div> </form> </div></div></body></html>

在项目启动后,访问http://localhost:8000/login,进入到登录页面,首先加载必要的css和js资源,在填写好用户名、密码后,点击登录,会执行user_login的接口方法, 简单的对用户进行校验,即可进入首页。

其次要说的是这个项目中,添加了验证码的功能,使用了kaptcha的依赖。在登录界面中,增加了验证码的显示,点击验证码图片后,可以切换验证码。

登录界面

到此这篇关于springboot+thymeleaf+druid+mybatis 多模块实现用户登录功能的文章就介绍到这了,更多相关springboot+thymeleaf+druid+mybatis 多模块登录内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章