一、用戶服務搭建
用戶業務模型後端服務:
- 用戶註冊,由於手機號的唯一性,最靠譜的就是使用數據庫的唯一索引保證,同時catch duplicatedkey異常,拋出有意義的手機號已註冊異常
- 用戶註冊,使用密碼加密的方式以祕文方式入庫,保證用戶信息的安全性
- 用戶登錄,通過無狀態的http session機制,通過cookie傳遞sessionId信息並保存登錄憑證
實現用戶的登錄註冊登出服務
點評dianpingdb數據庫 用戶表user
/*
Navicat Premium Data Transfer
Source Server : 本地測試
Source Server Type : MySQL
Source Server Version : 50728
Source Host : localhost:3306
Source Schema : dianpingdb
Target Server Type : MySQL
Target Server Version : 50728
File Encoding : 65001
Date: 05/05/2020 12:53:23
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`created_at` datetime(0) NOT NULL,
`updated_at` datetime(0) NOT NULL,
`telphone` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
`password` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
`nick_name` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
`gender` int(11) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `telphone_unique_index`(`telphone`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, '2020-02-01 11:46:46', '2020-05-05 11:46:56', '10987654321', '123456', '菜雞文', 1);
SET FOREIGN_KEY_CHECKS = 1;
實現用戶的登錄註冊服務
-
用戶服務
-
用戶模型
-
用戶行爲 - 登錄註冊
-
用戶登錄態管理
可用代碼生成器mybatis-generator自動生成,生成PO類,生成mapper映射文件(其中包括基本的增刪改查功能)、生成mapper接口等。要做些修改,添加下自己需要的功能或刪除自己不需要的功能即可。mybatis-generator.xml
實現用戶的登錄註冊服務
在src/main/java目錄下,創建一個com.awen.dianping.service包,並在包中創建接口UserService,接口中定義一系列方法,代碼如下所示。
interface UserService.java
public interface UserService {
UserModel getUser(Integer id);
UserModel register(UserModel registerUser) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException;
UserModel login(String telphone,String password) throws UnsupportedEncodingException, NoSuchAlgorithmException, BusinessException;
Integer countAllUser();
}
在src/main/java目錄下,創建一個com.awen.dianping.dal包,並在包中創建接口UserModelMapper,接口中定義一系列方法,代碼如下所示。
UserModelMapper.java
public interface UserModelMapper {
int deleteByPrimaryKey(Integer id);
int insert(UserModel record);
int insertSelective(UserModel record);
UserModel selectByPrimaryKey(Integer id);
UserModel selectByTelphoneAndPassword(@Param("telphone") String telphone, @Param("password")String password);
int updateByPrimaryKeySelective(UserModel record);
int updateByPrimaryKey(UserModel record);
Integer countAllUser();
}
在src/main/resources/mapping目錄下,UserModelMapper.xml 代碼生成就不寫了,加了點添自己需要的功能,最後有源碼。
在com.awen.dianping.service包下,創建UserService接口的實現類UserServiceImpl,該類需要實現接口中的所有方法,代碼如下所示。
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserModelMapper userModelMapper;
@Override
public UserModel getUser(Integer id) {
return userModelMapper.selectByPrimaryKey(id);
}
@Override
@Transactional
public UserModel register(UserModel registerUser) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
registerUser.setPassword(encodeByMd5(registerUser.getPassword()));
registerUser.setCreatedAt(new Date());
registerUser.setUpdatedAt(new Date());
try{
userModelMapper.insertSelective(registerUser);
}catch (DuplicateKeyException ex){
throw new BusinessException(EmBusinessError.REGISTER_DUP_FAIL);
}
return getUser(registerUser.getId());
}
@Override
public UserModel login(String telphone, String password) throws UnsupportedEncodingException, NoSuchAlgorithmException, BusinessException {
UserModel userModel = userModelMapper.selectByTelphoneAndPassword(telphone,encodeByMd5(password));
if(userModel == null){
throw new BusinessException(EmBusinessError.LOGIN_FAIL);
}
return userModel;
}
@Override
public Integer countAllUser() {
return userModelMapper.countAllUser();
}
private String encodeByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
//確認計算方法MD5
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
BASE64Encoder base64Encoder = new BASE64Encoder();
return base64Encoder.encode(messageDigest.digest(str.getBytes("utf-8")));
}
}
在com.awen.dianping.request包下,創建LoginReq,RegisterReq封裝登錄、註冊請求數據,代碼如下所示。
RegisterReq
public class RegisterReq {
@NotBlank(message = "手機號不能爲空")
private String telphone;
@NotBlank(message = "密碼不能爲空")
private String password;
@NotBlank(message = "暱稱不能爲空")
private String nickName;
@NotNull(message = "性別不能爲空")
private Integer gender;
public String getTelphone() {
return telphone;
}
public void setTelphone(String telphone) {
this.telphone = telphone;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
}
LoginReq.java
public class LoginReq {
@NotBlank(message = "手機號不能爲空")
private String telphone;
@NotBlank(message = "密碼不能爲空")
private String password;
public String getTelphone() {
return telphone;
}
public void setTelphone(String telphone) {
this.telphone = telphone;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
在com.awen.dianping.controller包下,創建UserController,代碼如下所示。
UserController.java
@Controller("/user")
@RequestMapping("/user")
public class UserController {
public static final String CURRENT_USER_SESSION = "currentUserSession";
@Autowired
private HttpServletRequest httpServletRequest;
@Autowired
private UserService userService;
//get用戶信息
@RequestMapping("/get")
@ResponseBody
public CommonRes getUser(@RequestParam(name="id")Integer id) throws BusinessException {
UserModel userModel = userService.getUser(id);
if(userModel == null){
//return CommonRes.create(new CommonError(EmBusinessError.NO_OBJECT_FOUND),"fail");
throw new BusinessException(EmBusinessError.NO_OBJECT_FOUND);
}else{
return CommonRes.create(userModel);
}
}
//註冊
@RequestMapping("/register")
@ResponseBody
public CommonRes register(@Valid @RequestBody RegisterReq registerReq, BindingResult bindingResult) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
if(bindingResult.hasErrors()){
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, CommonUtil.processErrorString(bindingResult));
}
UserModel registerUser = new UserModel();
registerUser.setTelphone(registerReq.getTelphone());
registerUser.setPassword(registerReq.getPassword());
registerUser.setNickName(registerReq.getNickName());
registerUser.setGender(registerReq.getGender());
UserModel resUserModel = userService.register(registerUser);
return CommonRes.create(resUserModel);
}
//登錄
@RequestMapping("/login")
@ResponseBody
public CommonRes login(@RequestBody @Valid LoginReq loginReq,BindingResult bindingResult) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
if(bindingResult.hasErrors()){
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,CommonUtil.processErrorString(bindingResult));
}
UserModel userModel = userService.login(loginReq.getTelphone(),loginReq.getPassword());
httpServletRequest.getSession().setAttribute(CURRENT_USER_SESSION,userModel);
return CommonRes.create(userModel);
}
//註銷
@RequestMapping("/logout")
@ResponseBody
public CommonRes logout() throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
httpServletRequest.getSession().invalidate();
return CommonRes.create(null);
}
//獲取當前用戶信息
@RequestMapping("/getcurrentuser")
@ResponseBody
public CommonRes getCurrentUser(){
UserModel userModel = (UserModel) httpServletRequest.getSession().getAttribute(CURRENT_USER_SESSION);
return CommonRes.create(userModel);
}
}
運行測試
GET http://localhost:8010/user/get?id=1
POST 註冊 http://localhost:8080/user/register
POST 登錄 http://localhost:8080/user/login
GET 獲取當前用戶信息 http://localhost:8080/user/getcurrentuser
GET 退出 http://localhost:8080/user/logout
代碼 Github:https://github.com/liuawen/dianping
SpringBoot、ElasticSearch、Spark 完成一個點評搜索推薦項目 用戶服務