1
0
mirror of https://gitee.com/bookshelfplus/bookshelfplus synced 2025-09-22 01:30:40 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

添加用户登录(还未验证)

This commit is contained in:
2022-03-13 02:05:29 +08:00
parent 1b78102d61
commit b49e720b3f
11 changed files with 756 additions and 1 deletions

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}