mirror of
https://github.com/FreeeBird/hotel.git
synced 2025-05-06 19:49:26 +08:00
1.添加拦截器,完善登录注册权限
2.添加API统一返回格式
This commit is contained in:
parent
8619b7a52f
commit
29f1697810
@ -2,9 +2,11 @@
|
||||
|
||||
Demo: [不可用]
|
||||
|
||||
前端页面项目(vue): https://github.com/FreeeBird/hotel_app
|
||||
前端页面项目(vue):
|
||||
https://github.com/FreeeBird/hotel_app
|
||||
|
||||
后台管理项目(vue-admin):https://github.com/FreeeBird/hotel-manager
|
||||
后台管理项目(vue-admin):
|
||||
https://github.com/FreeeBird/hotel-manager
|
||||
|
||||
## 1 背景说明
|
||||
|
||||
|
@ -1,8 +1,10 @@
|
||||
package cn.mafangui.hotel;
|
||||
package cn.mafangui.hotel.config;
|
||||
|
||||
import cn.mafangui.hotel.tool.SessionInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
@ -29,6 +31,14 @@ public class GlobalCrosConfig {
|
||||
//暴露哪些头部信息(因为跨域访问默认不能获取全部头部信息)
|
||||
.exposedHeaders("Header1", "Header2");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(new SessionInterceptor())
|
||||
.addPathPatterns("/**")
|
||||
.excludePathPatterns("/login/**")
|
||||
.excludePathPatterns("/register/**");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package cn.mafangui.hotel.controller;
|
||||
|
||||
import cn.mafangui.hotel.entity.User;
|
||||
import cn.mafangui.hotel.entity.Worker;
|
||||
import cn.mafangui.hotel.response.AjaxResult;
|
||||
import cn.mafangui.hotel.response.ResponseUtil;
|
||||
import cn.mafangui.hotel.service.UserService;
|
||||
import cn.mafangui.hotel.service.WorkerService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.util.HashMap;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/login")
|
||||
public class LoginController {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@Autowired
|
||||
private WorkerService workerService;
|
||||
|
||||
@RequestMapping(value = "/user",method = RequestMethod.POST)
|
||||
public AjaxResult userLogin(String username, String password,
|
||||
HttpServletRequest request){
|
||||
if(StringUtils.isEmpty(username)){
|
||||
return ResponseUtil.failed("用户名不能为空");
|
||||
}else if(StringUtils.isEmpty(password)) {
|
||||
return ResponseUtil.failed("密码不能为空");
|
||||
}
|
||||
User user = userService.selectByUsernameAndPassword(username,password);
|
||||
if(user==null){
|
||||
return ResponseUtil.failed("用户名或密码不正确");
|
||||
}
|
||||
HttpSession session = request.getSession();
|
||||
session.setAttribute("userId",user.getUserId());
|
||||
return ResponseUtil.success(session.getId());
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/worker",method = RequestMethod.POST)
|
||||
public AjaxResult workerLogin(String username, String password,
|
||||
HttpServletRequest request){
|
||||
if(StringUtils.isEmpty(username)){
|
||||
return ResponseUtil.failed("用户名不能为空");
|
||||
}else if(StringUtils.isEmpty(password)) {
|
||||
return ResponseUtil.failed("密码不能为空");
|
||||
}
|
||||
Worker worker = workerService.login(username,password);
|
||||
if(worker==null){
|
||||
return ResponseUtil.failed("用户名或密码不正确");
|
||||
}
|
||||
HttpSession session = request.getSession();
|
||||
session.setAttribute("userId",worker.getWorkerId());
|
||||
session.setAttribute("role",worker.getRole());
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
map.put("sessionId",session.getId());
|
||||
map.put("role",worker.getRole());
|
||||
return ResponseUtil.success(map);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package cn.mafangui.hotel.controller;
|
||||
|
||||
import cn.mafangui.hotel.entity.User;
|
||||
import cn.mafangui.hotel.entity.Worker;
|
||||
import cn.mafangui.hotel.enums.Role;
|
||||
import cn.mafangui.hotel.response.AjaxResult;
|
||||
import cn.mafangui.hotel.response.ResponseUtil;
|
||||
import cn.mafangui.hotel.service.UserService;
|
||||
import cn.mafangui.hotel.service.WorkerService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/register")
|
||||
public class RegisterController {
|
||||
|
||||
@Autowired
|
||||
UserService userService;
|
||||
@Autowired
|
||||
WorkerService workerService;
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST,value = "/user")
|
||||
public AjaxResult userRegister(String username,String password,String name,String gender,String phone,String email,String address,String idcard){
|
||||
User user = new User(username,password,name,gender,phone,email,address,idcard);
|
||||
int result = userService.insertUser(user);
|
||||
if(result==1){
|
||||
return ResponseUtil.success(result);
|
||||
}
|
||||
else return ResponseUtil.failed("注册失败,请稍后再试");
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST,value = "/admin")
|
||||
public AjaxResult register(String username, String password, String name, String gender, String phone, String email, String address){
|
||||
Worker worker = new Worker(username,password,name,gender,phone,email,address);
|
||||
worker.setRole(Role.ADMIN.getValue());
|
||||
int result = workerService.insert(worker);
|
||||
if(result==1){
|
||||
return ResponseUtil.success(result);
|
||||
}
|
||||
else return ResponseUtil.failed("注册失败,请稍后再试");
|
||||
}
|
||||
|
||||
}
|
@ -3,10 +3,13 @@ package cn.mafangui.hotel.controller;
|
||||
import cn.mafangui.hotel.entity.User;
|
||||
import cn.mafangui.hotel.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@ -22,13 +25,16 @@ public class UserController {
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping(method = RequestMethod.POST,value = "/login")
|
||||
public int userLogin(String username,String password){
|
||||
public int userLogin(String username, String password, HttpServletRequest request){
|
||||
int result;
|
||||
if (username == null | username == "" | password == null | password == ""){
|
||||
return -1;
|
||||
}
|
||||
if (userService.selectByUsernameAndPassword(username,password) != null){
|
||||
result = 1;
|
||||
HttpSession session = request.getSession();
|
||||
session.setAttribute("userId",username);
|
||||
session.getId();
|
||||
}
|
||||
else result = 0;
|
||||
return result;
|
||||
|
76
src/main/java/cn/mafangui/hotel/response/AjaxResult.java
Normal file
76
src/main/java/cn/mafangui/hotel/response/AjaxResult.java
Normal file
@ -0,0 +1,76 @@
|
||||
package cn.mafangui.hotel.response;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 统一返回格式
|
||||
* Example:
|
||||
* {
|
||||
* code:100,
|
||||
* message: "NOT FOUND",
|
||||
* data:{}
|
||||
* }
|
||||
*/
|
||||
public class AjaxResult<T> implements Serializable {
|
||||
|
||||
// 状态码
|
||||
private Integer code;
|
||||
// 信息
|
||||
private String message;
|
||||
// 数据
|
||||
private T data;
|
||||
|
||||
public AjaxResult() {
|
||||
}
|
||||
|
||||
public AjaxResult(Integer code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public AjaxResult(Integer code, String message, T data) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public AjaxResult(T data) {
|
||||
this.code = MsgType.SUCCESS.getCode();
|
||||
this.message = MsgType.SUCCESS.getMessage();
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(Integer code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AjaxResult{" +
|
||||
"code='" + code + '\'' +
|
||||
", message='" + message + '\'' +
|
||||
", data=" + data +
|
||||
'}';
|
||||
}
|
||||
}
|
37
src/main/java/cn/mafangui/hotel/response/MsgType.java
Normal file
37
src/main/java/cn/mafangui/hotel/response/MsgType.java
Normal file
@ -0,0 +1,37 @@
|
||||
package cn.mafangui.hotel.response;
|
||||
|
||||
|
||||
/**
|
||||
* 消息类型描述
|
||||
*/
|
||||
public enum MsgType {
|
||||
SUCCESS(1000,"success"),
|
||||
FAILED(1001,"Failure"),
|
||||
|
||||
PARAM_IS_INVALID(1100,"参数非法")
|
||||
|
||||
;
|
||||
private Integer code;
|
||||
private String message;
|
||||
MsgType() {
|
||||
}
|
||||
MsgType(Integer code) {
|
||||
this.code = code;
|
||||
}
|
||||
MsgType(Integer code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
public void setCode(Integer code) {
|
||||
this.code = code;
|
||||
}
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
34
src/main/java/cn/mafangui/hotel/response/ResponseUtil.java
Normal file
34
src/main/java/cn/mafangui/hotel/response/ResponseUtil.java
Normal file
@ -0,0 +1,34 @@
|
||||
package cn.mafangui.hotel.response;
|
||||
|
||||
import cn.mafangui.hotel.response.AjaxResult;
|
||||
import cn.mafangui.hotel.response.MsgType;
|
||||
|
||||
/**
|
||||
* 接口数据返回工具类
|
||||
* 成功则调用success
|
||||
* 失败则调用failed
|
||||
*/
|
||||
public class ResponseUtil {
|
||||
|
||||
/**
|
||||
* 请求成功
|
||||
* @param object 数据
|
||||
* @return
|
||||
*/
|
||||
public static AjaxResult<Object> success(Object object){
|
||||
return new AjaxResult<>(object);
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求失败
|
||||
* @param msgType 消息描述
|
||||
* @return
|
||||
*/
|
||||
public static AjaxResult failed(MsgType msgType){
|
||||
return new AjaxResult(msgType.getCode(),msgType.getMessage());
|
||||
}
|
||||
|
||||
public static AjaxResult failed(String msg){
|
||||
return new AjaxResult(MsgType.FAILED.getCode(),msg);
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ package cn.mafangui.hotel.service.impl;
|
||||
import cn.mafangui.hotel.entity.Worker;
|
||||
import cn.mafangui.hotel.mapper.WorkerMapper;
|
||||
import cn.mafangui.hotel.service.WorkerService;
|
||||
import cn.mafangui.hotel.utils.MD5Utils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@ -16,6 +17,7 @@ public class WorkerServiceImpl implements WorkerService {
|
||||
|
||||
@Override
|
||||
public int insert(Worker worker) {
|
||||
worker.setPassword(MD5Utils.MD5Encode(worker.getPassword()));
|
||||
return workerMapper.insertSelective(worker);
|
||||
}
|
||||
|
||||
@ -51,11 +53,13 @@ public class WorkerServiceImpl implements WorkerService {
|
||||
|
||||
@Override
|
||||
public Worker login(String username, String password,String role) {
|
||||
return workerMapper.selectByUsernameAndPassword(username,password,role);
|
||||
String pass = MD5Utils.MD5Encode(password);
|
||||
return workerMapper.selectByUsernameAndPassword(username,pass,role);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Worker login(String username, String password) {
|
||||
return workerMapper.selectByUsernamePassword(username,password);
|
||||
String pass = MD5Utils.MD5Encode(password);
|
||||
return workerMapper.selectByUsernamePassword(username,pass);
|
||||
}
|
||||
}
|
||||
|
37
src/main/java/cn/mafangui/hotel/tool/SessionInterceptor.java
Normal file
37
src/main/java/cn/mafangui/hotel/tool/SessionInterceptor.java
Normal file
@ -0,0 +1,37 @@
|
||||
package cn.mafangui.hotel.tool;
|
||||
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
public class SessionInterceptor implements HandlerInterceptor {
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
System.out.println("Pre");
|
||||
HttpSession session = request.getSession();
|
||||
System.out.println(session.getAttribute("userId"));
|
||||
if(session!=null && session.getAttribute("userId")!=null){
|
||||
return true;
|
||||
}else {
|
||||
PrintWriter writer = response.getWriter();
|
||||
writer.write("Not Login.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
||||
System.out.println("post");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||
System.out.println("after");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user