用户密码登录功能完成
This commit is contained in:
@@ -1,9 +1,12 @@
|
|||||||
package com.bao.dating.controller;
|
package com.bao.dating.controller;
|
||||||
|
|
||||||
|
import com.bao.dating.common.Result;
|
||||||
|
import com.bao.dating.common.ResultCode;
|
||||||
|
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||||
|
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||||
import com.bao.dating.service.UserService;
|
import com.bao.dating.service.UserService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/user")
|
@RequestMapping("/user")
|
||||||
@@ -11,4 +14,14 @@ public class UserController {
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserService userService;
|
private UserService userService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录
|
||||||
|
* @param userLoginDTO 登录参数
|
||||||
|
*/
|
||||||
|
@PostMapping("/login")
|
||||||
|
public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
|
||||||
|
UserLoginVO userloginVO = userService.userLogin(userLoginDTO);
|
||||||
|
return Result.success(ResultCode.SUCCESS, "登录成功", userloginVO);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,16 @@
|
|||||||
package com.bao.dating.mapper;
|
package com.bao.dating.mapper;
|
||||||
|
|
||||||
|
import com.bao.dating.pojo.entity.User;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface UserMapper {
|
public interface UserMapper {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据用户名查询用户
|
||||||
|
*
|
||||||
|
* @param username 用户名
|
||||||
|
* @return 用户
|
||||||
|
*/
|
||||||
|
User getByUsername(String username);
|
||||||
}
|
}
|
||||||
|
|||||||
14
src/main/java/com/bao/dating/pojo/dto/UserLoginDTO.java
Normal file
14
src/main/java/com/bao/dating/pojo/dto/UserLoginDTO.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package com.bao.dating.pojo.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户登录数据传输对象
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class UserLoginDTO implements Serializable {
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
}
|
||||||
@@ -1,4 +1,13 @@
|
|||||||
package com.bao.dating.service;
|
package com.bao.dating.service;
|
||||||
|
|
||||||
|
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||||
|
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||||
|
|
||||||
public interface UserService {
|
public interface UserService {
|
||||||
|
/**
|
||||||
|
* 登录
|
||||||
|
* @param userLoginDTO 登录参数
|
||||||
|
* @return 登录结果
|
||||||
|
*/
|
||||||
|
UserLoginVO userLogin(UserLoginDTO userLoginDTO);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,52 @@
|
|||||||
package com.bao.dating.service.impl;
|
package com.bao.dating.service.impl;
|
||||||
|
|
||||||
import com.bao.dating.mapper.UserMapper;
|
import com.bao.dating.mapper.UserMapper;
|
||||||
|
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||||
|
import com.bao.dating.pojo.entity.User;
|
||||||
|
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||||
import com.bao.dating.service.UserService;
|
import com.bao.dating.service.UserService;
|
||||||
|
import com.bao.dating.util.JwtUtil;
|
||||||
|
import com.bao.dating.util.MD5Util;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserServiceImpl implements UserService {
|
public class UserServiceImpl implements UserService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserMapper userMapper;
|
private UserMapper userMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UserLoginVO userLogin(UserLoginDTO userLoginDTO) {
|
||||||
|
// 参数校验
|
||||||
|
if (userLoginDTO == null || userLoginDTO.getUsername() == null || userLoginDTO.getPassword() == null) {
|
||||||
|
throw new RuntimeException("用户名或密码不能为空");
|
||||||
|
}
|
||||||
|
// 查询用户
|
||||||
|
User user = userMapper.getByUsername(userLoginDTO.getUsername());
|
||||||
|
if (user == null){
|
||||||
|
throw new RuntimeException("用户不存在");
|
||||||
|
}
|
||||||
|
// 密码加密
|
||||||
|
boolean match = MD5Util.verifyWithSalt(
|
||||||
|
userLoginDTO.getPassword(),
|
||||||
|
user.getSalt(),
|
||||||
|
user.getPasswordHash()
|
||||||
|
);
|
||||||
|
// 密码校验
|
||||||
|
if (!match){
|
||||||
|
throw new RuntimeException("密码错误");
|
||||||
|
}
|
||||||
|
// 生成token
|
||||||
|
String token = JwtUtil.generateToken(String.valueOf(user.getUserId()));
|
||||||
|
// 封装返回
|
||||||
|
UserLoginVO userLoginVO = new UserLoginVO();
|
||||||
|
userLoginVO.setUserId(user.getUserId());
|
||||||
|
userLoginVO.setNickname(user.getNickname());
|
||||||
|
userLoginVO.setToken(token);
|
||||||
|
return userLoginVO;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,5 +4,8 @@
|
|||||||
|
|
||||||
<mapper namespace="com.bao.dating.mapper.UserMapper">
|
<mapper namespace="com.bao.dating.mapper.UserMapper">
|
||||||
|
|
||||||
|
<select id="getByUsername" resultType="com.bao.dating.pojo.entity.User">
|
||||||
|
SELECT * FROM user WHERE user_name = #{userName}
|
||||||
|
</select>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
Reference in New Issue
Block a user