基于springboot的登录功能
源代码链接:https://pan.baidu.com/s/1QHP0Wwux-iuhV7ATb8k-Yw
提取码:frw2
- 项目文件截图
- 建立用户数据库
- 在application.properties文件中连接数据库,配置mybaits和thymeleaf,在连接数据库时,MySQL在高版本需要指明是否进行SSL连接,所有建议一般都要加上useSSL=false。
#连接数据库
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/students?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=061700
#扫描实体类
mybatis.type-aliases-package=com.student.stu.Entity
#配置thymeleaf
#缓存设置为false, 这样修改之后马上生效,便于调试
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=utf-8
- 在配置mybaits时,还需要在启动类上添加注解@MapperScan(“com.student.stu.Dao”),然后是在DAO层添加注解@Mapper。
package com.student.stu.Dao;
import com.student.stu.Entity.Admin;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface UserDao {
@Select("select * from student where stu_name = #{username}")
Student SelectStuAll(@Param("username") String username);//添加一个查询学生方法,用于验证登录
@Select("select * from worker where worker_name = #{username}")
Worker SelectWorkerAll(@Param("username") String username);//添加一个查询维修工方法,用于验证登录
@Select("select * from admin where admin_name = #{username}")
Admin SelectAdminAll(@Param("username") String username);//添加一个查询管理员方法,用于验证登录
}
- 前端页面login.html,主要是form表单提交。
<form th:action="@{/home}" method="post">
<div id="box_name">
<input id="username" name="username" class="form-control" style="width: 300px" placeholder="请输入用户名" required/>
</div>
<div id="box_pwd">
<input id="pwd" name="pwd" class="form-control" style="width: 300px;" type="password" placeholder="请输入密码" required/>
</div>
<div id="box-role">
<select id="rolename" name="rolename" class="form-control" style="width: 300px">
<option>学生</option>
<option>维修工</option>
<option>管理员</option>
</select>
</div>
<div style="margin-top: 5px">
<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
</div>
<div id="foot" style="margin-left: 7%">
<button class="btn btn-primary" type="submit" onclick="onform()">登 录</button>
<a type="" class="btn btn-success" style="margin-left: 30px" th:href="@{/logon}">注 册</a>
</div>
</form>
- 控制层Controller中,验证登录的实现主要是通过form表单的提交传递数据给控制层,控制层在经过判断登录的角色类型,在验证不同用户的登录。当用户不存在时,控制类会返回map值给前端,前端页面
<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
判断msg值是否为空,不为空则会显示标签的内容;当用户存在时,再判断密码是否是正确的,如果正确的话,通过modelandview返回页面并且将该用户信息存储在username中
session.setAttribute("username", username);
如果不正确的话会返回原页面。
package com.student.stu.Controller;
import com.student.stu.Dao.UserDao;
import com.student.stu.Entity.Admin;
import com.student.stu.Entity.Student;
import com.student.stu.Entity.Worker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
import java.util.Map;
@Controller
public class LoginController {
@Autowired(required = false)
UserDao userDao;
@RequestMapping("/login")
/* @ResponseBody 使得页面返回return里面的值*/
public String login(){
return "login/login";
}
@RequestMapping("/home")
public ModelAndView home(@RequestParam("username") String username,
@RequestParam("pwd") String pwd,
@RequestParam("rolename") String rolename,
Map<String, Object> map, HttpSession session, ModelMap modelMap)throws Exception{
ModelAndView mv = null;
try {
if ("学生".equals(rolename)){
Student student = userDao.SelectStuAll(username);
if (student != null){
if (student.getStu_pwd().equals(pwd)){
mv = new ModelAndView("Home/StuHome");
String name = student.getStu_title();
modelMap.put("m",name);
session.setAttribute("username", username);
}else {
map.put("msg", "输入密码错误");
mv = new ModelAndView("login/login");
}
}else {
map.put("msg", "该用户不存在");
mv = new ModelAndView("login/login");
}
}
if ("维修工".equals(rolename)){
Worker worker = userDao.SelectWorkerAll(username);
if (worker != null){
if ("通过".equals(worker.getStatus())){
if (worker.getWorker_pwd().equals(pwd)){
mv = new ModelAndView("Home/WorkerHome");
String name = worker.getWorker_title();
modelMap.put("m",name);
session.setAttribute("username", username);
}else {
map.put("msg", "输入密码错误");
mv = new ModelAndView("login/login");
}
}
else {
map.put("msg", "该账户未通过审核!");
mv = new ModelAndView("login/login");
}
}else {
map.put("msg", "该用户不存在");
mv = new ModelAndView("login/login");
}
}
if ("管理员".equals(rolename)){
Admin admin = userDao.SelectAdminAll(username);
if (admin != null){
if (admin.getAdmin_pwd().equals(pwd)){
mv = new ModelAndView("Home/AdminHome");
String name = admin.getAdmin_name();
modelMap.put("m",name);
session.setAttribute("username", username);
}else {
map.put("msg", "输入密码错误");
mv = new ModelAndView("login/login");
}
}else {
map.put("msg", "该用户不存在");
mv = new ModelAndView("login/login");
}
}
}catch (Exception e){
e.printStackTrace();
}
return mv;
}
}