• 登录注册实现


    一、前端页面注册到Vue

    1.创建登录和注册组件

    1. <template>
    2. <div>
    3. login
    4. </div>
    5. </template>
    6. <script>
    7. export default {
    8. name: 'HomeView',
    9. data() {
    10. return {
    11. }
    12. },
    13. methods: {
    14. },
    15. }
    16. </script>
    1. <template>
    2. <div>
    3. register
    4. </div>
    5. </template>
    6. <script>
    7. export default {
    8. name: 'Register',
    9. data() {
    10. return {}
    11. },
    12. methods: {},
    13. }
    14. </script>

    2.组件路由注册

    在router/index.js文件中注册 登录和注册的vue组件路由

    1. import Vue from 'vue'
    2. import VueRouter from 'vue-router'
    3. import HomeView from '../views/HomeView.vue'
    4. import Login from '../views/Login.vue'
    5. import Register from '../views/Register.vue'
    6. Vue.use(VueRouter)
    7. const routes = [
    8. {
    9. path: '/',
    10. name: 'Login',
    11. component: Login
    12. },
    13. {
    14. path: '/register',
    15. name: 'Register',
    16. component: Register
    17. },
    18. {
    19. path: '/home',
    20. name: 'home',
    21. component: HomeView
    22. },
    23. {
    24. path: '/about',
    25. name: 'about',
    26. // route level code-splitting
    27. // this generates a separate chunk (about.[hash].js) for this route
    28. // which is lazy-loaded when the route is visited.
    29. component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
    30. }
    31. ]
    32. const router = new VueRouter({
    33. routes
    34. })
    35. export default router

    二、Login页面编写

    1. <template>
    2. <body id="poster">
    3. <el-form class="login-container" label-position="left" label-width="0px">
    4. <h2 class="login_title">
    5. 系统登录
    6. </h2>
    7. <el-form-item label="">
    8. <el-input type="text" v-model="loginFrom.loginName" placeholder="账号"></el-input>
    9. </el-form-item>
    10. <el-form-item label="">
    11. <el-input type="password" v-model="loginFrom.password" placeholder="密码"></el-input>
    12. </el-form-item>
    13. <el-form-item>
    14. <el-button type="primary" style="width: 100%;background:#505458;border:none" @click="login">登录</el-button>
    15. 没有账号?<el-button @click="toRegister" style="margin-top: 5px">点我注册</el-button>
    16. </el-form-item>
    17. </el-form>
    18. </body>
    19. </template>
    20. <script>
    21. export default {
    22. name: 'HomeView',
    23. data() {
    24. return {
    25. loginFrom: {
    26. name: '',
    27. loginName:'',
    28. password:''
    29. }
    30. }
    31. },
    32. methods: {
    33. login() {
    34. console.log('submit!',this.loginFrom);
    35. this.axios.post('http://localhost:3333/user/login',this.loginFrom).then((resp)=>{
    36. let data = resp.data;
    37. if(data.success){
    38. this.loginFrom = {},
    39. this.$message({
    40. message: '登录成功',
    41. type: 'success'
    42. });
    43. this.$router.push({
    44. path:'/home'
    45. })
    46. }
    47. })
    48. this.$router.push({
    49. path:'/home'
    50. })
    51. },
    52. toRegister(){
    53. //Vue跳转
    54. this.$router.push({
    55. path:'/register'
    56. })
    57. }
    58. },
    59. }
    60. </script>
    61. <style>
    62. #poster{
    63. background-position: center;
    64. height: 100%;
    65. width: 100%;
    66. background-size: cover;
    67. position: fixed;
    68. }
    69. body{
    70. margin: 0px;
    71. padding: 0px;
    72. }
    73. .login-container{
    74. border-radius: 15px;
    75. background-clip: padding-box;
    76. margin:150px auto;
    77. padding: 35px 35px 15px 35px;
    78. width: 600px;
    79. height: 290px;
    80. background:#fff;
    81. border:1px solid #eaeaea;
    82. box-shadow:0 0 1000px #cac6c6;
    83. }
    84. .login_title{
    85. margin:0px auto 30px auto;
    86. text-align: center;
    87. color:#505458
    88. }
    89. </style>

    三、Register页面编写

    1. <template>
    2. <div id="poster">
    3. <el-form :model="ruleForm" ref="ruleForm" :rules="rules" label-width="100px" class="register-container">
    4. <h2 class="register_title" style="display:inline;">
    5. 系统登录
    6. </h2>
    7. <el-button style="margin:0px 0px 10px 10px" @click="toLogin">去登录</el-button>
    8. <el-form-item label="账号" prop="account">
    9. <el-input v-model="ruleForm.account" placeholder="请输入账号" autocomplete="off" prefix-icon="el-icon-user-solid"></el-input>
    10. </el-form-item>
    11. <el-form-item label="姓名" prop="name">
    12. <el-input type="text" v-model="ruleForm.name" placeholder="请输入姓名" autocomplete="off" prefix-icon="el-icon-user"></el-input>
    13. </el-form-item>
    14. <el-form-item label="性别">
    15. <el-select v-model="ruleForm.sex" placeholder="性别" style="right: 140px">
    16. <el-option label="男" value="男"></el-option>
    17. <el-option label="女" value="女"></el-option>
    18. </el-select>
    19. </el-form-item>
    20. <el-form-item label="电话" prop="phone">
    21. <el-input v-model="ruleForm.phone" placeholder="请输入电话号码" autocomplete="off" prefix-icon="el-icon-phone"></el-input>
    22. </el-form-item>
    23. <el-form-item label="密码" prop="password">
    24. <el-input type="password" v-model="ruleForm.password" autocomplete="off" placeholder="请输入密码" prefix-icon="el-icon-lock"></el-input>
    25. </el-form-item>
    26. <el-form-item label="确认密码" prop="checkPass">
    27. <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" placeholder="请确认密码" prefix-icon="el-icon-lock"></el-input>
    28. </el-form-item>
    29. <el-form-item style="margin-right:100px">
    30. <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
    31. <el-button @click="resetForm('ruleForm')">重置</el-button>
    32. </el-form-item>
    33. </el-form>
    34. </div>
    35. </template>
    36. <script>
    37. export default {
    38. name: 'Register',
    39. data() {
    40. var validatePass = (rule, value, callback) => {
    41. if (value === '') {
    42. callback(new Error('请输入密码'));
    43. } else {
    44. if (this.ruleForm.checkPass !== '') {
    45. this.$refs.ruleForm.validateField('checkPass');
    46. }
    47. callback();
    48. }
    49. };
    50. var validatePass2 = (rule, value, callback) => {
    51. if (value === '') {
    52. callback(new Error('请再次输入密码'));
    53. } else if (value !== this.ruleForm.password) {
    54. callback(new Error('两次输入密码不一致!'));
    55. } else {
    56. callback();
    57. }
    58. };
    59. return {
    60. ruleForm: {
    61. account:'',
    62. password: '',
    63. name:'',
    64. checkPass: '',
    65. phone:'',
    66. sex: ''
    67. },
    68. rules: {
    69. account: [
    70. { required: true, message: '请输入账号', trigger: 'blur' },
    71. { min: 6, max:20, message: '长度在 6 到 15 个字符', trigger: 'blur' }
    72. ],
    73. name: [
    74. { required: true, message: '请输入用户姓名', trigger: 'blur' },
    75. { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
    76. ],
    77. phone: [
    78. //手机号格式校验规则
    79. { required: true, message: '请填写手机号', trigger: 'blur' },
    80. { pattern:/^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/
    81. , message: '手机号格式不正确', trigger: 'blur' }
    82. ],
    83. password: [
    84. { validator: validatePass, trigger: 'blur' }
    85. ],
    86. checkPass: [
    87. { validator: validatePass2, trigger: 'blur' }
    88. ],
    89. }
    90. };
    91. },
    92. methods: {
    93. submitForm(ruleForm) {
    94. //清除表单
    95. this.ruleForm = {};
    96. //axios请求
    97. this.axios.post('http://localhost:3333/user/register', this.ruleForm).then((resp) => {
    98. console.log(resp)
    99. let data = resp.data;
    100. console.log(data)
    101. if (data.success) {
    102. //消息提示
    103. this.$message({
    104. message:'注册成功',
    105. type:'success'
    106. });
    107. }
    108. })
    109. },
    110. resetForm(formName) {
    111. this.$refs[formName].resetFields();
    112. },
    113. toLogin(){
    114. this.$router.push({
    115. path:'/'
    116. })
    117. }
    118. }
    119. }
    120. </script>
    121. <style>
    122. #poster{
    123. background-position: center;
    124. height: 100%;
    125. width: 100%;
    126. background-size: cover;
    127. position: fixed;
    128. margin: 0px;
    129. padding: 0px;
    130. }
    131. .register-container{
    132. border-radius: 15px;
    133. background-clip: padding-box;
    134. margin:80px auto;
    135. padding: 35px 35px 15px 35px;
    136. width: 600px;
    137. height: 480px;
    138. background:#fff;
    139. border:1px solid #eaeaea;
    140. box-shadow:0 0 1000px #cac6c6;
    141. }
    142. .register_title{
    143. margin:0px auto 30px auto;
    144. text-align: center;
    145. color:#505458
    146. }
    147. </style>

    四、后端注册代码实现

    首先前端填写表单,发送post请求调接口

    1. submitForm(ruleForm) {
    2. //axios请求
    3. this.axios.post('http://localhost:3333/user/register', this.ruleForm).then((resp) => {
    4. console.log(resp)
    5. let data = resp.data;
    6. console.log(data)
    7. if (data.success) {
    8. //消息提示
    9. this.$message({
    10. message:'注册成功',
    11. type:'success'
    12. });
    13. }
    14. })
    15. },

    controller层

    registerUserVo对应前端传过来表单的实体类,然后对密码进行加盐处理

    1. @PostMapping("/register")
    2. public CommonDto register(@RequestBody RegisterUserVo registerUserVo){
    3. CommonDto<User> commonDto = new CommonDto<>();
    4. //密码加盐
    5. registerUserVo.setPassword(DigestUtils.md5DigestAsHex(registerUserVo.getPassword().getBytes()));
    6. userService.register(registerUserVo);
    7. commonDto.setMessage("注册成功");
    8. return commonDto;
    9. }

    service层

    service层首先判断传过来的数据数据库中id是否为空,如果不为空,再根据account字段查询数据库中有无相同account数据,若没有才可注册,接着用雪花算法设置一个id,把数据插入数据库中

    1. @Override
    2. public void register(RegisterUserVo registerUserVo) {
    3. User user = BeanCopyUtils.copyBean(registerUserVo, User.class);
    4. if (ObjectUtils.isEmpty(registerUserVo.getId())){
    5. User userDB = selectByAccount(registerUserVo.getAccount());
    6. if(ObjectUtils.isEmpty(userDB)){
    7. user.setId(snowflake.nextId());
    8. userMapper.insert(user);
    9. }
    10. }
    11. }
    1. //查询account是否被注册
    2. public User selectByAccount(String account){
    3. LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
    4. wrapper.eq(User::getAccount,account);
    5. List<User> userList = userMapper.selectList(wrapper);
    6. if(CollectionUtils.isEmpty(userList)){
    7. return null;
    8. }else {
    9. return userList.get(0);
    10. }
    11. }

    utils工具类

    雪花算法

    1. package com.like.utils;
    2. import lombok.extern.slf4j.Slf4j;
    3. import org.springframework.stereotype.Component;
    4. import java.text.MessageFormat;
    5. @Slf4j
    6. @Component
    7. public class Snowflake {
    8. // ==============================Fields===========================================
    9. /**
    10. * 开始时间戳 (2000-01-01 00:00:00)
    11. */
    12. private static final long TWEPOCH = 946656000000L;
    13. /**
    14. * 机器id所占的位数 5
    15. */
    16. private static final long WORKER_ID_BITS = 5L;
    17. /**
    18. * 数据标识id所占的位数 5
    19. */
    20. private static final long DATA_CENTER_ID_BITS = 5L;
    21. /**
    22. * 支持的最大机器id,结果是 31
    23. */
    24. private static final long MAX_WORKER_ID = ~(-1L << WORKER_ID_BITS);
    25. /**
    26. * 支持的最大数据标识id,结果是 31
    27. */
    28. private static final long MAX_DATA_CENTER_ID = ~(-1L << DATA_CENTER_ID_BITS);
    29. /**
    30. * 序列在id中占的位数
    31. */
    32. private static final long SEQUENCE_BITS = 12L;
    33. /**
    34. * 机器ID向左移12位
    35. */
    36. private static final long WORKER_ID_SHIFT = SEQUENCE_BITS;
    37. /**
    38. * 数据标识id向左移17位(12+5)
    39. */
    40. private static final long DATA_CENTER_ID_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS;
    41. /**
    42. * 时间戳向左移22位(5+5+12)
    43. */
    44. private static final long TIMESTAMP_LEFT_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS + DATA_CENTER_ID_BITS;
    45. /**
    46. * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095)
    47. */
    48. private static final long SEQUENCE_MASK = ~(-1L << SEQUENCE_BITS);
    49. /**
    50. * 步长 1024
    51. */
    52. private static final long STEP_SIZE = 1024;
    53. /**
    54. * unsigned int max value
    55. */
    56. private static final long UINT_MAX_VALUE = 0xffffffffL;
    57. /**
    58. * 工作机器ID(0~31)
    59. */
    60. private long workerId;
    61. /**
    62. * 工作机器ID 计数器
    63. */
    64. private long workerIdFlags = 0L;
    65. /**
    66. * 数据中心ID(0~31)
    67. */
    68. private long dataCenterId;
    69. /**
    70. * 数据中心ID 计数器
    71. */
    72. private long dataCenterIdFlags = 0L;
    73. /**
    74. * 毫秒内序列(0~4095)
    75. */
    76. private long sequence = 0L;
    77. /**
    78. * 毫秒内序列基数[0|1024|2048|3072]
    79. */
    80. private long basicSequence = 0L;
    81. /**
    82. * 上次生成ID的时间戳
    83. */
    84. private long lastTimestamp = -1L;
    85. /**
    86. * 工作模式
    87. */
    88. private final WorkMode workMode;
    89. public enum WorkMode { NON_SHARED, RATE_1024, RATE_4096; }
    90. //==============================Constructors=====================================
    91. public Snowflake() {
    92. this(0, 0, WorkMode.RATE_4096);
    93. }
    94. /**
    95. * 构造函数
    96. * @param workerId 工作ID (0~31)
    97. * @param dataCenterId 数据中心ID (0~31)
    98. */
    99. public Snowflake(long workerId, long dataCenterId) {
    100. this(workerId, dataCenterId, WorkMode.RATE_4096);
    101. }
    102. /**
    103. * 构造函数
    104. * @param workerId 工作ID (0~31)
    105. * @param dataCenterId 数据中心ID (0~31)
    106. * @param workMode 工作模式
    107. */
    108. public Snowflake(long workerId, long dataCenterId, WorkMode workMode) {
    109. this.workMode = workMode;
    110. if (workerId > MAX_WORKER_ID || workerId < 0) {
    111. throw new IllegalArgumentException(MessageFormat.format("worker Id can't be greater than {0} or less than 0", MAX_WORKER_ID));
    112. }
    113. if (dataCenterId > MAX_DATA_CENTER_ID || dataCenterId < 0) {
    114. throw new IllegalArgumentException(MessageFormat.format("datacenter Id can't be greater than {0} or less than 0", MAX_DATA_CENTER_ID));
    115. }
    116. this.workerId = workerId;
    117. this.workerIdFlags = setSpecifiedBitTo1(this.workerIdFlags, this.workerId);
    118. this.dataCenterId = dataCenterId;
    119. this.dataCenterIdFlags = setSpecifiedBitTo1(this.dataCenterIdFlags, this.dataCenterId);
    120. }
    121. // ==============================Methods==========================================
    122. /**
    123. * 获取机器id
    124. *
    125. * @return 所属机器的id
    126. */
    127. public long getWorkerId() {
    128. return workerId;
    129. }
    130. /**
    131. * 获取数据中心id
    132. *
    133. * @return 所属数据中心id
    134. */
    135. public long getDataCenterId() {
    136. return dataCenterId;
    137. }
    138. /**
    139. * 获得下一个ID (该方法是线程安全的)
    140. *
    141. * @return SnowflakeId
    142. */
    143. public synchronized long nextId() {
    144. long timestamp = timeGen();
    145. //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常
    146. if (timestamp < this.lastTimestamp) {
    147. if (timestamp > TWEPOCH) {
    148. if (WorkMode.NON_SHARED == this.workMode) {
    149. nonSharedClockBackwards(timestamp);
    150. } else if (WorkMode.RATE_1024 == this.workMode) {
    151. rate1024ClockBackwards(timestamp);
    152. } else {
    153. throw new RuntimeException(MessageFormat.format("Clock moved backwards. Refusing to generate id for {0} milliseconds", lastTimestamp - timestamp));
    154. }
    155. } else {
    156. throw new RuntimeException(MessageFormat.format("Clock moved backwards. Refusing to generate id for {0} milliseconds", lastTimestamp - timestamp));
    157. }
    158. }
    159. //如果是同一时间生成的,则进行毫秒内序列
    160. if (this.lastTimestamp == timestamp) {
    161. this.sequence = (this.sequence + 1) & SEQUENCE_MASK;
    162. //毫秒内序列溢出
    163. if (this.sequence == 0) {
    164. //阻塞到下一个毫秒,获得新的时间戳
    165. timestamp = tilNextMillis(this.lastTimestamp);
    166. }
    167. }
    168. //时间戳改变,毫秒内序列重置
    169. else {
    170. this.sequence = this.basicSequence;
    171. }
    172. //上次生成ID的时间戳
    173. this.lastTimestamp = timestamp;
    174. //移位并通过或运算拼到一起组成64位的ID
    175. return ((timestamp - TWEPOCH) << TIMESTAMP_LEFT_SHIFT)
    176. | (this.dataCenterId << DATA_CENTER_ID_SHIFT)
    177. | (this.workerId << WORKER_ID_SHIFT)
    178. | this.sequence;
    179. }
    180. /**
    181. * 阻塞到下一个毫秒,直到获得新的时间戳
    182. *
    183. * @param lastTimestamp 上次生成ID的时间戳
    184. * @return 当前时间戳
    185. */
    186. protected long tilNextMillis(long lastTimestamp) {
    187. long timestamp0;
    188. do {
    189. timestamp0 = timeGen();
    190. } while (timestamp0 <= lastTimestamp);
    191. return timestamp0;
    192. }
    193. /**
    194. * 返回以毫秒为单位的当前时间
    195. *
    196. * @return 当前时间(毫秒)
    197. */
    198. protected long timeGen() {
    199. return System.currentTimeMillis();
    200. }
    201. /**
    202. * 尝试解决时钟回拨
      【* 仅用于 单机生成不对外 的情况 *】
    203. *
    204. * @param timestamp 当前时间戳
    205. * @return void
    206. */
    207. private void nonSharedClockBackwards(long timestamp) {
    208. if (this.dataCenterIdFlags >= UINT_MAX_VALUE && this.workerIdFlags >= UINT_MAX_VALUE) {
    209. throw new RuntimeException(MessageFormat.format("Clock moved backwards. Refusing to generate id for {0} milliseconds", lastTimestamp - timestamp));
    210. } else {
    211. //如果仅用于生成不重复的数值,尝试变更 dataCenterId 或 workerId 修复时钟回拨问题
    212. log.warn("Clock moved backwards. Refusing to generate id for {} milliseconds", lastTimestamp - timestamp);
    213. //先尝试变更 dataCenterId,当 dataCenterId 轮询一遍之后,尝试变更 workerId 并重置 dataCenterId
    214. if (this.dataCenterIdFlags >= UINT_MAX_VALUE) {
    215. if (++this.workerId > MAX_WORKER_ID) { this.workerId = 0L; }
    216. this.workerIdFlags = setSpecifiedBitTo1(this.workerIdFlags, this.workerId);
    217. // 重置 dataCenterId 和 dataCenterIdFlags
    218. this.dataCenterIdFlags = this.dataCenterId = 0L;
    219. } else {
    220. if (++this.dataCenterId > MAX_DATA_CENTER_ID) { this.dataCenterId = 0L; }
    221. }
    222. this.dataCenterIdFlags = setSpecifiedBitTo1(this.dataCenterIdFlags, this.dataCenterId);
    223. this.lastTimestamp = -1L;
    224. log.warn("Try to fix the clock moved backwards. timestamp : {}, worker Id : {}, datacenter Id : {}", timestamp, workerId, dataCenterId);
    225. }
    226. }
    227. /**
    228. * 尝试解决时钟回拨
      【* 仅用于每毫秒生成量 不大于 1024 的情况 *】
    229. *
    230. * @param timestamp 当前时间戳
    231. * @return void
    232. */
    233. private void rate1024ClockBackwards(long timestamp) {
    234. if (this.basicSequence > (SEQUENCE_MASK - STEP_SIZE)) {
    235. throw new RuntimeException(MessageFormat.format("Clock moved backwards. Refusing to generate id for {0} milliseconds", lastTimestamp - timestamp));
    236. } else {
    237. log.warn("Clock moved backwards. Refusing to generate id for {} milliseconds", lastTimestamp - timestamp);
    238. this.basicSequence += STEP_SIZE;
    239. this.lastTimestamp = -1L;
    240. log.warn("Try to fix the clock moved backwards. timestamp : {}, basicSequence : {}", timestamp, basicSequence);
    241. }
    242. }
    243. /**
    244. * Set the specified bit to 1
    245. *
    246. * @param value raw long value
    247. * @param index bit index (From 0~31)
    248. * @return long value
    249. */
    250. private long setSpecifiedBitTo1(long value, long index) {
    251. return value |= (1L << index);
    252. }
    253. /**
    254. * Set the specified bit to 0
    255. *
    256. * @param value raw long value
    257. * @param index bit index (From 0~31)
    258. * @return long value
    259. */
    260. private long setSpecifiedBitTo0(long value, long index) {
    261. return value &= ~(1L << index);
    262. }
    263. /**
    264. * Get the specified bit
    265. * @param value raw long value
    266. * @param index bit index(From 0-31)
    267. * @return 0 or 1
    268. */
    269. private int getSpecifiedBit(long value, long index) {
    270. return (value & (1L << index)) == 0 ? 0 : 1;
    271. }
    272. }

    BeanCopy类

    1. package com.like.utils;
    2. import org.springframework.beans.BeanUtils;
    3. import java.util.List;
    4. import java.util.stream.Collectors;
    5. public class BeanCopyUtils {
    6. private BeanCopyUtils(){
    7. }
    8. public static V copyBean(Object source,Class clazz) {
    9. V result;
    10. try {
    11. result = clazz.getDeclaredConstructor().newInstance();
    12. BeanUtils.copyProperties(source, result);
    13. } catch (Exception e) {
    14. throw new RuntimeException(e);
    15. }
    16. return result;
    17. }
    18. public static List copyBeanList(List list,Class clazz){
    19. return list.stream().
    20. map(o -> copyBean(o, clazz))
    21. .collect(Collectors.toList());
    22. }
    23. }

    五、后端登录代码实现

    首先拿到前端传来的数据,用LoginVo接收,然后根据账号判断是否注册过,若已注册,则判断密码是否正确,从而放行

    前端login方法

    1. login() {
    2. console.log('submit!',this.loginForm);
    3. this.axios.post('http://localhost:3333/user/login',this.loginForm).then((resp)=>{
    4. let data = resp.data;
    5. if(data.success){
    6. this.loginForm = {},
    7. this.$message({
    8. message: data.message,
    9. type: 'success'
    10. });
    11. this.$router.push({
    12. path:'/home'
    13. })
    14. }
    15. })
    16. }

    后端代码

    LoginUserVo

    1. package com.like.common;
    2. import lombok.AllArgsConstructor;
    3. import lombok.Data;
    4. import lombok.EqualsAndHashCode;
    5. import lombok.NoArgsConstructor;
    6. @Data
    7. @AllArgsConstructor
    8. @NoArgsConstructor
    9. @EqualsAndHashCode(callSuper = false)
    10. public class LoginUserVo {
    11. /**
    12. * 账号
    13. */
    14. private String account;
    15. /**
    16. * 密码
    17. */
    18. private String password;
    19. }

    controller层 

    1. @PostMapping("/login")
    2. public CommonDto login(@RequestBody LoginUserVo loginUserVo){
    3. CommonDto commonDto = userService.login(loginUserVo);
    4. return commonDto;
    5. }

    service层

    1. @Override
    2. public CommonDto login(LoginUserVo loginUserVo) {
    3. CommonDto commonDto = new CommonDto();
    4. User userDB = selectByAccount(loginUserVo.getAccount());
    5. if(ObjectUtils.isEmpty(userDB)){
    6. commonDto.setSuccess(false);
    7. commonDto.setContent("账号不存在");
    8. return commonDto;
    9. }
    10. String loginUserVoPassword = DigestUtils.md5DigestAsHex(loginUserVo.getPassword().getBytes());
    11. String password = userDB.getPassword();
    12. if(loginUserVoPassword.equals(password)){
    13. commonDto.setMessage("恭喜你登录成功");
    14. }
    15. commonDto.setContent(loginUserVo);
    16. return commonDto;
    17. }

  • 相关阅读:
    Himall商城Web帮助类删除、获取设置指定名称的Cookie特定键的值(2)
    协程原理与设计
    py2neo 查询语句
    速看|期待已久的2022年广州助理检测工程师真题解析终于出炉
    Redis-服务器
    c# Dictionary vs SortedDictionary
    《Go Web 编程》之第3章 接收请求
    (NDK编译)详解使用Android.mk编译的C/C++程序过程
    get_post (攻防世界)(简单php)
    java计算机毕业设计智能化管理的仓库管理源码+mysql数据库+系统+lw文档+部署
  • 原文地址:https://blog.csdn.net/m0_63732435/article/details/133521173