mirror of
https://gitee.com/bookshelfplus/bookshelfplus
synced 2025-09-05 00:21:38 +08:00
后端:用户登录、退出登录、获取用户状态相关功能完成;引入SessionManager;数据库:user_identity改为group;前端:添加登录页面、后台管理页面框架,axios POST请求Content-Type问题修复,引入md5、sha1 js库;小问题调整
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package plus.bookshelf.Common.SessionManager;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
public class LocalSessionManager implements SessionManager {
|
||||
|
||||
/**
|
||||
* 私有化构造函数
|
||||
*/
|
||||
private LocalSessionManager(HttpServletRequest httpServletRequest) {
|
||||
this.httpServletRequest = httpServletRequest;
|
||||
}
|
||||
|
||||
static SessionManager sessionManager = null;
|
||||
// static SessionManager sessionManager = new LocalSessionManager();
|
||||
|
||||
/**
|
||||
* 通过此方法获取当前类的实例
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static SessionManager getInstance(HttpServletRequest httpServletRequest) {
|
||||
if (sessionManager == null)
|
||||
sessionManager = new LocalSessionManager(httpServletRequest);
|
||||
return sessionManager;
|
||||
}
|
||||
|
||||
HttpServletRequest httpServletRequest;
|
||||
|
||||
@Override
|
||||
public Object getValue(String key) {
|
||||
try {
|
||||
return httpServletRequest.getSession().getAttribute(key);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(String key, Object value) {
|
||||
httpServletRequest.getSession().setAttribute(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(String key) {
|
||||
httpServletRequest.getSession().removeAttribute(key);
|
||||
}
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
package plus.bookshelf.Common.SessionManager;
|
||||
|
||||
public abstract interface SessionManager {
|
||||
/**
|
||||
* 获取 Session
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
Object getValue(String key);
|
||||
|
||||
/**
|
||||
* 设置 Session
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
void setValue(String key, Object value);
|
||||
|
||||
/**
|
||||
* 移除 Session
|
||||
* @param key
|
||||
*/
|
||||
void remove(String key);
|
||||
}
|
@@ -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;
|
||||
|
@@ -40,11 +40,11 @@ public class UserDO {
|
||||
/**
|
||||
*
|
||||
* This field was generated by MyBatis Generator.
|
||||
* This field corresponds to the database column user_info.user_identity
|
||||
* This field corresponds to the database column user_info.group
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
private String userIdentity;
|
||||
private String group;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -180,26 +180,26 @@ public class UserDO {
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method returns the value of the database column user_info.user_identity
|
||||
* This method returns the value of the database column user_info.group
|
||||
*
|
||||
* @return the value of user_info.user_identity
|
||||
* @return the value of user_info.group
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
public String getUserIdentity() {
|
||||
return userIdentity;
|
||||
public String getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method sets the value of the database column user_info.user_identity
|
||||
* This method sets the value of the database column user_info.group
|
||||
*
|
||||
* @param userIdentity the value for user_info.user_identity
|
||||
* @param group the value for user_info.group
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
public void setUserIdentity(String userIdentity) {
|
||||
this.userIdentity = userIdentity == null ? null : userIdentity.trim();
|
||||
public void setGroup(String group) {
|
||||
this.group = group == null ? null : group.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -30,7 +30,7 @@ public class UserServiceImpl implements UserService {
|
||||
userModel.setUsername(userDO.getUsername());
|
||||
userModel.setEncriptPwd(userDO.getEncriptPwd());
|
||||
userModel.setNickname(userDO.getNickname());
|
||||
userModel.setUserIdentity(userDO.getUserIdentity());
|
||||
userModel.setGroup(userDO.getGroup());
|
||||
userModel.setAvatar(userDO.getAvatar());
|
||||
userModel.setPhone(userDO.getPhone());
|
||||
userModel.setWeixinThirdPartyAuthCode(userDO.getWeixinThirdPartyAuthCode());
|
||||
|
@@ -2,6 +2,8 @@ package plus.bookshelf.Service.Model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
@Data
|
||||
public class UserModel {
|
||||
|
||||
@@ -9,6 +11,7 @@ public class UserModel {
|
||||
Integer id;
|
||||
|
||||
// 用户名
|
||||
@NotBlank(message = "用户名不能为空")
|
||||
String username;
|
||||
|
||||
// 用户加密后的密码
|
||||
@@ -18,7 +21,7 @@ public class UserModel {
|
||||
String nickname;
|
||||
|
||||
// 用户身份 NOT_LOGIN, ADMIN, LOGIN_USER;
|
||||
String userIdentity;
|
||||
String group;
|
||||
|
||||
// 用户头像
|
||||
String avatar;
|
||||
|
Reference in New Issue
Block a user