" />

飞道的博客

SpringBoot+Mybatis +vue 实现登录 注册 还有验证码的。

469人阅读  评论(0)

SpringBoot+Mybatis +vue 实现登录 注册 还有验证码的。

不是前后端分离的

 

 

login.html


  
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>登录 </title>
  6. <link rel="stylesheet" href="../../css/base.css">
  7. <link rel="stylesheet" href="../../css/element.css">
  8. </head>
  9. <body style="background:url('../../images/bg.jpg') no-repeat; background-size: cover;">
  10. <div id="wrapper">
  11. <div style="width: 500px; margin: 150px auto; background-color:rgba(100,149,237,0.5); border-radius: 10px">
  12. <div style="width: 100%; height: 100px; font-size: 30px;line-height: 100px; text-align: center; color: darkblue">欢迎登录后台管理系统 </div>
  13. <div style="margin-top: 25px; width: 100%; height: 320px;">
  14. <div style="width: 60%; margin: 0 auto">
  15. <input v-model="user.username" type="text" style="border: 1px solid #ccc; height: 40px; padding: 10px; width: 100%" placeholder="请输入账号">
  16. <input v-model="user.password" type="password" style="border: 1px solid #ccc; height: 40px; padding: 10px; width: 100%; margin-top: 25px" placeholder="请输入密码">
  17. <div style="margin-top: 25px; line-height: 40px">
  18. <input v-model="user.code" type="text" style="border: 1px solid #ccc; padding: 10px; width: 65%; " placeholder="请输入验证码">
  19. <div id="picyzm" style="display: inline-block; height: 35px; vertical-align: middle;"> </div>
  20. </div>
  21. <button @click="login" style="height: 40px; padding: 10px; width: 100%; margin-top: 25px; background-color: steelblue; color: white">登录 </button>
  22. <a style="width: 100%; text-align: right; display: inline-block; margin-top: 25px; color: darkblue" href="register.html">还没账号?去注册 </a>
  23. </div>
  24. </div>
  25. </div>
  26. </div>
  27. <script src="../../js/jquery.min.js"> </script>
  28. <script src="../../js/vue.min.js"> </script>
  29. <script src="../../js/element.js"> </script>
  30. <script src="../../js/gVerify.js"> </script>
  31. <script>
  32. //初始化验证码
  33. let verifyCode;
  34. new Vue({
  35. el: "#wrapper",
  36. data: {
  37. user: {}
  38. },
  39. mounted() {
  40. verifyCode = new GVerify({
  41. id: "picyzm",
  42. type: "blend"
  43. });
  44. },
  45. methods: {
  46. login() {
  47. if (! this.user.username) {
  48. this.$message({
  49. message: "请输入用户名",
  50. type: "error"
  51. });
  52. return;
  53. }
  54. if (! this.user.password) {
  55. this.$message({
  56. message: "请输入密码",
  57. type: "error"
  58. });
  59. return;
  60. }
  61. if (! this.user.code) {
  62. this.$message({
  63. message: "请输入验证码",
  64. type: "error"
  65. });
  66. return;
  67. }
  68. let res = verifyCode.validate( this.user.code);
  69. if (!res) {
  70. this.$message({
  71. message: "验证码错误",
  72. type: "error"
  73. });
  74. return;
  75. }
  76. $.ajax({
  77. url: "/user/login",
  78. type: "POST",
  79. contentType: "application/json",
  80. data: JSON.stringify( this.user)
  81. }).then( res => {
  82. if (res.code === '0') {
  83. this.$message({
  84. message: "登录成功",
  85. type: "success"
  86. });
  87. sessionStorage.setItem( "user", JSON.stringify(res.data));
  88. setTimeout( () => {
  89. location.href= "/page/end/index.html"
  90. }, 1000)
  91. } else {
  92. this.$message({
  93. message: res.msg,
  94. type: "error"
  95. })
  96. }
  97. })
  98. }
  99. }
  100. })
  101. </script>
  102. </body>
  103. </html>

register.html


  
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>注册 </title>
  6. <link rel="stylesheet" href="../../css/base.css">
  7. <link rel="stylesheet" href="../../css/element.css">
  8. </head>
  9. <body style="background:url('../../images/bg1.jpg') no-repeat; background-size: cover;">
  10. <div id="wrapper">
  11. <div style="width: 500px; margin: 150px auto;background-color:rgba(100,149,237,0.5); border-radius: 10px">
  12. <div style="width: 100%; height: 100px; color: darkblue; font-size: 30px;line-height: 100px; text-align: center">欢迎注册 </div>
  13. <div style="margin-top: 25px; width: 100%; height: 350px; text-align: center">
  14. <input v-model="user.username" type="text" style="border: 1px solid #ccc; height: 40px; padding: 10px; width: 60%" placeholder="请输入账号">
  15. <input v-model="user.password" type="password" style="border: 1px solid #ccc; height: 40px; padding: 10px; width: 60%; margin-top: 20px" placeholder="请输入密码">
  16. <input v-model="user.email" type="text" style="border: 1px solid #ccc; height: 40px; padding: 10px; width: 60%; margin-top: 20px" placeholder="请输入邮箱">
  17. <input v-model="user.phone" type="text" style="border: 1px solid #ccc; height: 40px; padding: 10px; width: 60%; margin-top: 20px" placeholder="请输入电话">
  18. <button @click="register" style="height: 40px; padding: 10px; width: 60%; margin-top: 25px; background-color: steelblue; color: white">注册 </button>
  19. <div style="margin-top: 25px">
  20. <a style="width: 60%; text-align: right; display: inline-block; color: darkblue" href="login.html">已有账号,去登录 </a>
  21. </div>
  22. </div>
  23. </div>
  24. </div>
  25. <script src="../../js/jquery.min.js"> </script>
  26. <script src="../../js/vue.min.js"> </script>
  27. <script src="../../js/element.js"> </script>
  28. <script>
  29. new Vue({
  30. el: "#wrapper",
  31. data: {
  32. user: {}
  33. },
  34. methods: {
  35. register() {
  36. if (! this.user.username) {
  37. this.$message({
  38. message: "请输入用户名",
  39. type: "error"
  40. });
  41. return;
  42. }
  43. if (! this.user.password) {
  44. this.$message({
  45. message: "请输入密码",
  46. type: "error"
  47. });
  48. return;
  49. }
  50. $.ajax({
  51. url: "/user/register",
  52. type: "POST",
  53. contentType: "application/json",
  54. data: JSON.stringify( this.user)
  55. }).then( res => {
  56. if (res.code === '0') {
  57. this.$message({
  58. message: "注册成功",
  59. type: "success"
  60. });
  61. sessionStorage.setItem( "user", JSON.stringify(res.data));
  62. setTimeout( () => {
  63. location.href= "/page/end/index.html"
  64. }, 1000)
  65. } else {
  66. this.$message({
  67. message: res.msg,
  68. type: "error"
  69. })
  70. }
  71. })
  72. }
  73. }
  74. })
  75. </script>
  76. </body>
  77. </html>

 

UserController.java


  
  1. package com.example.controller;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.io.IoUtil;
  4. import cn.hutool.poi.excel.ExcelUtil;
  5. import cn.hutool.poi.excel.ExcelWriter;
  6. import com.baomidou.mybatisplus.core.metadata.IPage;
  7. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import com.example.common.Result;
  10. import com.example.entity.User;
  11. import com.example.service.UserService;
  12. import org.springframework.web.bind.annotation.*;
  13. import javax.annotation.Resource;
  14. import javax.servlet.ServletOutputStream;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17. import java.io.IOException;
  18. import java.net.URLEncoder;
  19. import java.util.Collection;
  20. import java.util.LinkedHashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.concurrent.ConcurrentHashMap;
  24. @RestController
  25. @RequestMapping("/user")
  26. public class UserController {
  27. public static final ConcurrentHashMap<String, User> MAP = new ConcurrentHashMap<>();
  28. @Resource
  29. private UserService userService;
  30. /**
  31. * 登录
  32. *
  33. * @param user
  34. * @param request
  35. * @return
  36. */
  37. @PostMapping("/login")
  38. public Result<User> login(@RequestBody User user, HttpServletRequest request) {
  39. User res = userService.login(user);
  40. request.getSession().setAttribute( "user", user);
  41. MAP.put(res.getUsername(), res);
  42. return Result.success(res);
  43. }
  44. /**
  45. * 注册
  46. *
  47. * @param user
  48. * @param request
  49. * @return
  50. */
  51. @PostMapping("/register")
  52. public Result<User> register(@RequestBody User user, HttpServletRequest request) {
  53. if (user.getPassword() == null) {
  54. user.setPassword( "123456");
  55. }
  56. User dbUser = userService.register(user);
  57. request.getSession().setAttribute( "user", user);
  58. return Result.success(dbUser);
  59. }
  60. @GetMapping("/logout")
  61. public Result<?> logout(HttpServletRequest request) {
  62. User user = (User) request.getSession().getAttribute( "user");
  63. if (user != null) {
  64. request.getSession().removeAttribute( "user");
  65. MAP.remove(user.getUsername());
  66. }
  67. return Result.success();
  68. }
  69. @GetMapping("/online")
  70. public Result<Collection<User>> online(HttpServletRequest request) {
  71. return Result.success(MAP.values());
  72. }
  73. @PostMapping
  74. public Result<?> save( @RequestBody User user) {
  75. if (user.getPassword() == null) {
  76. user.setPassword( "123456");
  77. }
  78. return Result.success(userService.save(user));
  79. }
  80. @PutMapping
  81. public Result<?> update( @RequestBody User user) {
  82. return Result.success(userService.updateById(user));
  83. }
  84. @DeleteMapping("/{id}")
  85. public Result<?> delete( @PathVariable Long id) {
  86. userService.removeById(id);
  87. return Result.success();
  88. }
  89. @GetMapping("/{id}")
  90. public Result<User> findById(@PathVariable Long id) {
  91. return Result.success(userService.getById(id));
  92. }
  93. @GetMapping("/detail/{username}")
  94. public Result<User> findByUsername(@PathVariable String username) {
  95. return Result.success(userService.getbyUsername(username));
  96. }
  97. @GetMapping
  98. public Result<List<User>> findAll() {
  99. return Result.success(userService.list());
  100. }
  101. @GetMapping("/page")
  102. public Result<IPage<User>> findPage( @RequestParam(required = false, defaultValue = "") String name,
  103. @RequestParam(required = false, defaultValue = "1") Integer pageNum,
  104. @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
  105. return Result.success(userService.page( new Page<>(pageNum, pageSize),
  106. Wrappers.<User>lambdaQuery().like(User::getUsername, name).orderByDesc(User::getId)));
  107. }
  108. @GetMapping("/export")
  109. public void export(HttpServletResponse response) throws IOException {
  110. List<Map<String, Object>> list = CollUtil.newArrayList();
  111. List<User> all = userService.list();
  112. // 从userList集合中,依次取出一个对象
  113. for (User user : all) {
  114. Map<String, Object> row1 = new LinkedHashMap<>();
  115. row1.put( "名称", user.getUsername());
  116. row1.put( "手机", user.getPhone());
  117. row1.put( "邮箱", user.getEmail());
  118. list.add(row1);
  119. }
  120. // 2. 写excel
  121. ExcelWriter writer = ExcelUtil.getWriter( true);
  122. writer.write(list, true);
  123. response.setContentType( "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  124. String fileName = URLEncoder.encode( "用户信息", "UTF-8");
  125. response.setHeader( "Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
  126. ServletOutputStream out = response.getOutputStream();
  127. writer.flush(out, true);
  128. writer.close();
  129. IoUtil.close(System.out);
  130. }
  131. }

实体类

public class User extends Model<User> {
    /**
      * 主键
      */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    private String username;

    private String password;

    private String email;

    private String phone;

    private String avatar;

userService


  
  1. @Service
  2. public class UserService extends ServiceImpl<UserMapper, User> {
  3. @Resource
  4. private UserMapper userMapper;
  5. @Resource
  6. private RoleService roleService;
  7. public User login(User user) {
  8. User one = getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()).eq(User::getPassword, user.getPassword()));
  9. if (one == null) {
  10. throw new CustomException( "-1", "账号或密码错误");
  11. }
  12. setPermission(one);
  13. return one;
  14. }
  15. public User register(User user) {
  16. User one = getOne((Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername())));
  17. if (one != null) {
  18. throw new CustomException( "-1", "用户已注册");
  19. }
  20. if (user.getPassword() == null) {
  21. user.setPassword( "123456");
  22. }
  23. save(user);
  24. return getOne((Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername())));
  25. }

userMapper

package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.User;

public interface UserMapper extends BaseMapper<User> {

}

 

 

user.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper">

</mapper>

转载:https://blog.csdn.net/weixin_41290863/article/details/116806838
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场