mirror of
https://gitee.com/bookshelfplus/bookshelfplus
synced 2025-10-30 02:13:10 +08:00
添加用户登录(还未验证)
This commit is contained in:
@@ -1,5 +1,40 @@
|
||||
package plus.bookshelf.Controller.Controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import plus.bookshelf.Service.Model.UserModel;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class BaseController {
|
||||
// content-type 常量
|
||||
public static final String CONTENT_TYPE_FORMED = "application/x-www-form-urlencoded";
|
||||
|
||||
@Autowired
|
||||
HttpServletRequest httpServletRequest;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
/**
|
||||
* 获取用户登陆状态
|
||||
*/
|
||||
public Boolean isLogin() {
|
||||
return (Boolean) httpServletRequest.getSession().getAttribute("IS_LOGIN");
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存用户的登录状态
|
||||
* @return String uuidToken
|
||||
*/
|
||||
public String onLogin(UserModel userModel) {
|
||||
String uuidToken = UUID.randomUUID().toString();
|
||||
redisTemplate.expire(uuidToken, 1, TimeUnit.HOURS);
|
||||
|
||||
// 建立token和用户登录态之间的联系
|
||||
redisTemplate.opsForValue().set(uuidToken, userModel);
|
||||
return uuidToken;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package plus.bookshelf.Controller.Controller;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import plus.bookshelf.Controller.VO.UserVO;
|
||||
import plus.bookshelf.Service.Impl.UserServiceImpl;
|
||||
import plus.bookshelf.Service.Model.UserModel;
|
||||
import plus.bookshelf.Service.Service.UserService;
|
||||
|
||||
import static plus.bookshelf.Controller.Controller.BaseController.CONTENT_TYPE_FORMED;
|
||||
|
||||
@Controller
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
UserServiceImpl userService;
|
||||
|
||||
@RequestMapping(value = "login", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
|
||||
@ResponseBody
|
||||
public UserVO login(@RequestParam(value = "username") String username,
|
||||
@RequestParam(value = "encryptpwd") String encryptPwd) {
|
||||
UserModel userModel = userService.userLogin(username, encryptPwd);
|
||||
UserVO userVO = convertFromService(userModel);
|
||||
return userVO;
|
||||
}
|
||||
|
||||
private UserVO convertFromService(UserModel userModel) {
|
||||
if (userModel == null) {
|
||||
return null;
|
||||
}
|
||||
UserVO userVO = new UserVO();
|
||||
BeanUtils.copyProperties(userModel, userVO);
|
||||
return userVO;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package plus.bookshelf.Controller.VO;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UserVO {
|
||||
|
||||
// 用户Id
|
||||
Integer id;
|
||||
|
||||
// 用户名
|
||||
String username;
|
||||
|
||||
// 用户昵称
|
||||
String nickname;
|
||||
|
||||
// 用户身份 NOT_LOGIN, ADMIN, LOGIN_USER;
|
||||
String userIdentity;
|
||||
|
||||
// 用户头像
|
||||
String avatar;
|
||||
|
||||
// 用户手机号
|
||||
String phone;
|
||||
}
|
||||
300
bookshelfplus/src/main/java/plus/bookshelf/Dao/DO/UserDO.java
Normal file
300
bookshelfplus/src/main/java/plus/bookshelf/Dao/DO/UserDO.java
Normal file
@@ -0,0 +1,300 @@
|
||||
package plus.bookshelf.Dao.DO;
|
||||
|
||||
public class UserDO {
|
||||
/**
|
||||
*
|
||||
* This field was generated by MyBatis Generator.
|
||||
* This field corresponds to the database column user_info.id
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
*
|
||||
* This field was generated by MyBatis Generator.
|
||||
* This field corresponds to the database column user_info.username
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
*
|
||||
* This field was generated by MyBatis Generator.
|
||||
* This field corresponds to the database column user_info.encript_pwd
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
private String encriptPwd;
|
||||
|
||||
/**
|
||||
*
|
||||
* This field was generated by MyBatis Generator.
|
||||
* This field corresponds to the database column user_info.nickname
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
private String nickname;
|
||||
|
||||
/**
|
||||
*
|
||||
* This field was generated by MyBatis Generator.
|
||||
* This field corresponds to the database column user_info.user_identity
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
private String userIdentity;
|
||||
|
||||
/**
|
||||
*
|
||||
* This field was generated by MyBatis Generator.
|
||||
* This field corresponds to the database column user_info.avatar
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
private String avatar;
|
||||
|
||||
/**
|
||||
*
|
||||
* This field was generated by MyBatis Generator.
|
||||
* This field corresponds to the database column user_info.phone
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
private String phone;
|
||||
|
||||
/**
|
||||
*
|
||||
* This field was generated by MyBatis Generator.
|
||||
* This field corresponds to the database column user_info.weixin_third_party_auth_code
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
private String weixinThirdPartyAuthCode;
|
||||
|
||||
/**
|
||||
*
|
||||
* This field was generated by MyBatis Generator.
|
||||
* This field corresponds to the database column user_info.qq_third_party_auth_code
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
private String qqThirdPartyAuthCode;
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method returns the value of the database column user_info.id
|
||||
*
|
||||
* @return the value of user_info.id
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method sets the value of the database column user_info.id
|
||||
*
|
||||
* @param id the value for user_info.id
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method returns the value of the database column user_info.username
|
||||
*
|
||||
* @return the value of user_info.username
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method sets the value of the database column user_info.username
|
||||
*
|
||||
* @param username the value for user_info.username
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
public void setUsername(String username) {
|
||||
this.username = username == null ? null : username.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method returns the value of the database column user_info.encript_pwd
|
||||
*
|
||||
* @return the value of user_info.encript_pwd
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
public String getEncriptPwd() {
|
||||
return encriptPwd;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method sets the value of the database column user_info.encript_pwd
|
||||
*
|
||||
* @param encriptPwd the value for user_info.encript_pwd
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
public void setEncriptPwd(String encriptPwd) {
|
||||
this.encriptPwd = encriptPwd == null ? null : encriptPwd.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method returns the value of the database column user_info.nickname
|
||||
*
|
||||
* @return the value of user_info.nickname
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
public String getNickname() {
|
||||
return nickname;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method sets the value of the database column user_info.nickname
|
||||
*
|
||||
* @param nickname the value for user_info.nickname
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
public void setNickname(String nickname) {
|
||||
this.nickname = nickname == null ? null : nickname.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method returns the value of the database column user_info.user_identity
|
||||
*
|
||||
* @return the value of user_info.user_identity
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
public String getUserIdentity() {
|
||||
return userIdentity;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method sets the value of the database column user_info.user_identity
|
||||
*
|
||||
* @param userIdentity the value for user_info.user_identity
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
public void setUserIdentity(String userIdentity) {
|
||||
this.userIdentity = userIdentity == null ? null : userIdentity.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method returns the value of the database column user_info.avatar
|
||||
*
|
||||
* @return the value of user_info.avatar
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
public String getAvatar() {
|
||||
return avatar;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method sets the value of the database column user_info.avatar
|
||||
*
|
||||
* @param avatar the value for user_info.avatar
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
public void setAvatar(String avatar) {
|
||||
this.avatar = avatar == null ? null : avatar.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method returns the value of the database column user_info.phone
|
||||
*
|
||||
* @return the value of user_info.phone
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method sets the value of the database column user_info.phone
|
||||
*
|
||||
* @param phone the value for user_info.phone
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone == null ? null : phone.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method returns the value of the database column user_info.weixin_third_party_auth_code
|
||||
*
|
||||
* @return the value of user_info.weixin_third_party_auth_code
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
public String getWeixinThirdPartyAuthCode() {
|
||||
return weixinThirdPartyAuthCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method sets the value of the database column user_info.weixin_third_party_auth_code
|
||||
*
|
||||
* @param weixinThirdPartyAuthCode the value for user_info.weixin_third_party_auth_code
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
public void setWeixinThirdPartyAuthCode(String weixinThirdPartyAuthCode) {
|
||||
this.weixinThirdPartyAuthCode = weixinThirdPartyAuthCode == null ? null : weixinThirdPartyAuthCode.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method returns the value of the database column user_info.qq_third_party_auth_code
|
||||
*
|
||||
* @return the value of user_info.qq_third_party_auth_code
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
public String getQqThirdPartyAuthCode() {
|
||||
return qqThirdPartyAuthCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method sets the value of the database column user_info.qq_third_party_auth_code
|
||||
*
|
||||
* @param qqThirdPartyAuthCode the value for user_info.qq_third_party_auth_code
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
public void setQqThirdPartyAuthCode(String qqThirdPartyAuthCode) {
|
||||
this.qqThirdPartyAuthCode = qqThirdPartyAuthCode == null ? null : qqThirdPartyAuthCode.trim();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package plus.bookshelf.Dao.Mapper;
|
||||
|
||||
import plus.bookshelf.Dao.DO.UserDO;
|
||||
|
||||
public interface UserDOMapper {
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table user_info
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
int deleteByPrimaryKey(Integer id);
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table user_info
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
int insert(UserDO record);
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table user_info
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
int insertSelective(UserDO record);
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table user_info
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
UserDO selectByPrimaryKey(Integer id);
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table user_info
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
int updateByPrimaryKeySelective(UserDO record);
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table user_info
|
||||
*
|
||||
* @mbg.generated Sun Mar 13 01:51:14 SGT 2022
|
||||
*/
|
||||
int updateByPrimaryKey(UserDO record);
|
||||
|
||||
UserDO selectByUsernameAndEncryptpwd(String username, String encryptPwd);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package plus.bookshelf.Service.Impl;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import plus.bookshelf.Dao.DO.UserDO;
|
||||
import plus.bookshelf.Dao.Mapper.UserDOMapper;
|
||||
import plus.bookshelf.Service.Model.UserModel;
|
||||
import plus.bookshelf.Service.Service.UserService;
|
||||
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
@Autowired
|
||||
UserDOMapper userDOMapper;
|
||||
|
||||
@Override
|
||||
public UserModel userLogin(String username, String encryptPwd) {
|
||||
UserDO userDO = userDOMapper.selectByUsernameAndEncryptpwd(username, encryptPwd);
|
||||
UserModel userModel = convertFromDataObject(userDO);
|
||||
|
||||
return userModel;
|
||||
}
|
||||
|
||||
private UserModel convertFromDataObject(UserDO userDO) {
|
||||
if (userDO == null) {
|
||||
return null;
|
||||
}
|
||||
UserModel userModel = new UserModel();
|
||||
userModel.setId(userDO.getId());
|
||||
userModel.setUsername(userDO.getUsername());
|
||||
userModel.setEncriptPwd(userDO.getEncriptPwd());
|
||||
userModel.setNickname(userDO.getNickname());
|
||||
userModel.setUserIdentity(userDO.getUserIdentity());
|
||||
userModel.setAvatar(userDO.getAvatar());
|
||||
userModel.setPhone(userDO.getPhone());
|
||||
userModel.setWeixinThirdPartyAuthCode(userDO.getWeixinThirdPartyAuthCode());
|
||||
userModel.setQqThirdPartyAuthCode(userDO.getQqThirdPartyAuthCode());
|
||||
|
||||
return userModel;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package plus.bookshelf.Service.Model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UserModel {
|
||||
|
||||
// 用户Id
|
||||
Integer id;
|
||||
|
||||
// 用户名
|
||||
String username;
|
||||
|
||||
// 用户加密后的密码
|
||||
String encriptPwd;
|
||||
|
||||
// 用户昵称
|
||||
String nickname;
|
||||
|
||||
// 用户身份 NOT_LOGIN, ADMIN, LOGIN_USER;
|
||||
String userIdentity;
|
||||
|
||||
// 用户头像
|
||||
String avatar;
|
||||
|
||||
// 用户手机号
|
||||
String phone;
|
||||
|
||||
// 微信第三方登录授权
|
||||
String weixinThirdPartyAuthCode;
|
||||
|
||||
// QQ第三方登录授权
|
||||
String qqThirdPartyAuthCode;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package plus.bookshelf.Service.Service;
|
||||
|
||||
import plus.bookshelf.Service.Model.UserModel;
|
||||
|
||||
public interface UserService {
|
||||
/**
|
||||
* 用户登录
|
||||
*
|
||||
* @param username
|
||||
* @param encryptPwd
|
||||
*/
|
||||
UserModel userLogin(String username, String encryptPwd);
|
||||
}
|
||||
Reference in New Issue
Block a user