mirror of
https://gitee.com/bookshelfplus/bookshelfplus
synced 2025-09-10 02:41:38 +08:00
后端:用户登录、退出登录、获取用户状态相关功能完成;引入SessionManager;数据库:user_identity改为group;前端:添加登录页面、后台管理页面框架,axios POST请求Content-Type问题修复,引入md5、sha1 js库;小问题调整
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package plus.bookshelf.Controller.Controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
@@ -10,14 +9,18 @@ import plus.bookshelf.Common.Error.BusinessErrorCode;
|
||||
import plus.bookshelf.Common.Error.BusinessException;
|
||||
import plus.bookshelf.Common.Response.CommonReturnType;
|
||||
import plus.bookshelf.Common.Response.CommonReturnTypeStatus;
|
||||
import plus.bookshelf.Common.SessionManager.LocalSessionManager;
|
||||
import plus.bookshelf.Common.SessionManager.SessionManager;
|
||||
import plus.bookshelf.Service.Model.UserModel;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class BaseController {
|
||||
|
||||
@Autowired
|
||||
HttpServletRequest httpServletRequest;
|
||||
|
||||
// content-type 常量
|
||||
public static final String CONTENT_TYPE_FORMED = "application/x-www-form-urlencoded";
|
||||
|
||||
@@ -25,9 +28,6 @@ public class BaseController {
|
||||
public static final Integer COMMON_START_PAGE = 1;
|
||||
public static final Integer COMMON_PAGE_SIZE = 10;
|
||||
|
||||
@Autowired
|
||||
HttpServletRequest httpServletRequest;
|
||||
|
||||
// @Autowired
|
||||
// private RedisTemplate redisTemplate;
|
||||
|
||||
@@ -35,7 +35,8 @@ public class BaseController {
|
||||
* 获取用户登陆状态
|
||||
*/
|
||||
public Boolean isLogin() {
|
||||
return (Boolean) httpServletRequest.getSession().getAttribute("IS_LOGIN");
|
||||
SessionManager sessionManager = LocalSessionManager.getInstance(httpServletRequest);
|
||||
return (Boolean) sessionManager.getValue("IS_LOGIN");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,32 +44,47 @@ public class BaseController {
|
||||
*
|
||||
* @return String uuidToken
|
||||
*/
|
||||
public String onLogin(UserModel userModel) {
|
||||
String uuidToken = UUID.randomUUID().toString();
|
||||
public void onLogin(UserModel userModel) {
|
||||
// String uuidToken = UUID.randomUUID().toString();
|
||||
// redisTemplate.expire(uuidToken, 1, TimeUnit.HOURS);
|
||||
|
||||
// // 建立token和用户登录态之间的联系
|
||||
// redisTemplate.opsForValue().set(uuidToken, userModel);
|
||||
return uuidToken;
|
||||
// return uuidToken;
|
||||
|
||||
SessionManager sessionManager = LocalSessionManager.getInstance(httpServletRequest);
|
||||
sessionManager.setValue("IS_LOGIN", true);
|
||||
sessionManager.setValue("user", userModel);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户退出登录
|
||||
*/
|
||||
public void onLogout() {
|
||||
SessionManager sessionManager = LocalSessionManager.getInstance(httpServletRequest);
|
||||
sessionManager.setValue("IS_LOGIN", false);
|
||||
sessionManager.remove("user");
|
||||
return;
|
||||
}
|
||||
|
||||
// 定义ExceptionHandler解决未被Controller层吸收的Exception
|
||||
// @ExceptionHandler(Exception.class)
|
||||
// @ResponseStatus(HttpStatus.OK)
|
||||
// @ResponseBody
|
||||
// public Object handlerException(HttpServletRequest request, Exception ex) {
|
||||
// HashMap<Object, Object> responseData = new HashMap<>();
|
||||
//
|
||||
// if (ex instanceof BusinessException) {
|
||||
// BusinessException businessException = (BusinessException) ex;
|
||||
// responseData.put("errCode", businessException.getErrCode());
|
||||
// responseData.put("errMsg", businessException.getErrMsg());
|
||||
// } else {
|
||||
// // 生产环境输出格式化信息
|
||||
// responseData.put("errCode", BusinessErrorCode.UNKNOWN_ERROR.getErrCode());
|
||||
// responseData.put("errMsg", BusinessErrorCode.UNKNOWN_ERROR.getErrMsg());
|
||||
// }
|
||||
//
|
||||
// return CommonReturnType.create(responseData, CommonReturnTypeStatus.FAILED);
|
||||
// }
|
||||
@ExceptionHandler(Exception.class)
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
@ResponseBody
|
||||
public Object handlerException(HttpServletRequest request, Exception ex) {
|
||||
HashMap<Object, Object> responseData = new HashMap<>();
|
||||
|
||||
if (ex instanceof BusinessException) {
|
||||
BusinessException businessException = (BusinessException) ex;
|
||||
responseData.put("errCode", businessException.getErrCode());
|
||||
responseData.put("errMsg", businessException.getErrMsg());
|
||||
} else {
|
||||
// 生产环境输出格式化信息
|
||||
responseData.put("errCode", BusinessErrorCode.UNKNOWN_ERROR.getErrCode());
|
||||
responseData.put("errMsg", BusinessErrorCode.UNKNOWN_ERROR.getErrMsg());
|
||||
}
|
||||
|
||||
return CommonReturnType.create(responseData, CommonReturnTypeStatus.FAILED);
|
||||
}
|
||||
}
|
||||
|
@@ -20,7 +20,7 @@ import plus.bookshelf.Service.Service.CategoryService;
|
||||
@Api(value = "书籍分类")
|
||||
@Controller("category")
|
||||
@RequestMapping("/category")
|
||||
public class CategoryController {
|
||||
public class CategoryController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
CategoryService categoryService;
|
||||
|
@@ -17,7 +17,7 @@ import java.util.Map;
|
||||
@Api(value = "状态检测")
|
||||
@Controller("status")
|
||||
@RequestMapping("/status")
|
||||
public class StatusController {
|
||||
public class StatusController extends BaseController {
|
||||
|
||||
// @ApiOperation(value = "线程CPU占用时间", notes = "获取服务器当前线程CPU占用时间。此方法通过统计线程CPU占用时间来统计当前进程占用CPU情况。")
|
||||
// @RequestMapping(value = "getProcessCpu", method = {RequestMethod.GET})
|
||||
|
@@ -10,27 +10,70 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import plus.bookshelf.Common.Response.CommonReturnType;
|
||||
import plus.bookshelf.Common.SessionManager.LocalSessionManager;
|
||||
import plus.bookshelf.Common.SessionManager.SessionManager;
|
||||
import plus.bookshelf.Controller.VO.UserVO;
|
||||
import plus.bookshelf.Service.Impl.UserServiceImpl;
|
||||
import plus.bookshelf.Service.Model.UserModel;
|
||||
|
||||
import static plus.bookshelf.Controller.Controller.BaseController.CONTENT_TYPE_FORMED;
|
||||
|
||||
@Api(value = "用户")
|
||||
@Controller
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
public class UserController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
UserServiceImpl userService;
|
||||
|
||||
@ApiOperation(value = "用户登录",notes = "传入用户名,以及密码的MD5值,进行登录")
|
||||
@ApiOperation(value = "用户登录", notes = "传入用户名,以及密码的MD5值,进行登录")
|
||||
@RequestMapping(value = "login", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
|
||||
@ResponseBody
|
||||
public CommonReturnType login(@RequestParam(value = "username") String username,
|
||||
@RequestParam(value = "encryptpwd") String encryptPwd) {
|
||||
@RequestParam(value = "encryptpwd") String encryptPwd) {
|
||||
if (username == null || encryptPwd == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
UserModel userModel = userService.userLogin(username, encryptPwd);
|
||||
UserVO userVO = convertFromService(userModel);
|
||||
|
||||
if (userModel != null) {
|
||||
onLogin(userModel);
|
||||
}
|
||||
return CommonReturnType.create(userVO);
|
||||
}
|
||||
|
||||
// @ApiOperation(value = "用户注册", notes = "传入用户名,以及密码的MD5值,进行注册")
|
||||
// @RequestMapping(value = "register", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
|
||||
// @ResponseBody
|
||||
// public CommonReturnType register(@RequestParam(value = "username") String username,
|
||||
// @RequestParam(value = "encryptpwd") String encryptPwd) {
|
||||
// if (username == null || encryptPwd == null) {
|
||||
// return null;
|
||||
// }
|
||||
// UserModel userModel = userService.userRegister(username, encryptPwd);
|
||||
// UserVO userVO = convertFromService(userModel);
|
||||
// return CommonReturnType.create(userVO);
|
||||
// }
|
||||
|
||||
@ApiOperation(value = "用户登出", notes = "用户退出登录")
|
||||
@RequestMapping(value = "logout", method = {RequestMethod.GET})
|
||||
@ResponseBody
|
||||
public CommonReturnType logout() {
|
||||
onLogout();
|
||||
return CommonReturnType.create("success");
|
||||
}
|
||||
|
||||
@ApiOperation(value = "获取用户登录状态", notes = "获取用户登录状态")
|
||||
@RequestMapping(value = "getUserStatus", method = {RequestMethod.GET})
|
||||
@ResponseBody
|
||||
public CommonReturnType getUserStatus() {
|
||||
Object userModelObject = LocalSessionManager.getInstance(httpServletRequest).getValue("user");
|
||||
if (userModelObject == null) {
|
||||
return CommonReturnType.create(null);
|
||||
}
|
||||
|
||||
UserModel userModel = (UserModel) userModelObject;
|
||||
UserVO userVO = convertFromService(userModel);
|
||||
return CommonReturnType.create(userVO);
|
||||
}
|
||||
|
||||
|
@@ -15,7 +15,7 @@ public class UserVO {
|
||||
String nickname;
|
||||
|
||||
// 用户身份 NOT_LOGIN, ADMIN, LOGIN_USER;
|
||||
String userIdentity;
|
||||
String group;
|
||||
|
||||
// 用户头像
|
||||
String avatar;
|
||||
|
Reference in New Issue
Block a user