mirror of
				https://github.com/FreeeBird/hotel.git
				synced 2025-10-31 12:34:54 +08:00 
			
		
		
		
	解决跨域问题
This commit is contained in:
		
							
								
								
									
										31
									
								
								src/main/java/cn/mafangui/hotel/GlobalCrosConfig.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								src/main/java/cn/mafangui/hotel/GlobalCrosConfig.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | |||||||
|  | package cn.mafangui.hotel; | ||||||
|  |  | ||||||
|  | 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.WebMvcConfigurer; | ||||||
|  |  | ||||||
|  | @Configuration | ||||||
|  | public class GlobalCrosConfig { | ||||||
|  |     @Bean | ||||||
|  |     public WebMvcConfigurer corsConfigurer() { | ||||||
|  |         return new WebMvcConfigurer() { | ||||||
|  |             @Override | ||||||
|  |             //重写父类提供的跨域请求处理的接口 | ||||||
|  |             public void addCorsMappings(CorsRegistry registry) { | ||||||
|  |                 //添加映射路径 | ||||||
|  |                 registry.addMapping("/**") | ||||||
|  |                         //放行哪些原始域 | ||||||
|  |                         .allowedOrigins("*") | ||||||
|  |                         //是否发送Cookie信息 | ||||||
|  |                         .allowCredentials(true) | ||||||
|  |                         //放行哪些原始域(请求方式) | ||||||
|  |                         .allowedMethods("GET","POST", "PUT", "DELETE") | ||||||
|  |                         //放行哪些原始域(头部信息) | ||||||
|  |                         .allowedHeaders("*") | ||||||
|  |                         //暴露哪些头部信息(因为跨域访问默认不能获取全部头部信息) | ||||||
|  |                         .exposedHeaders("Header1", "Header2"); | ||||||
|  |             } | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -2,13 +2,12 @@ package cn.mafangui.hotel.controller; | |||||||
|  |  | ||||||
| import cn.mafangui.hotel.entity.Admin; | import cn.mafangui.hotel.entity.Admin; | ||||||
| import cn.mafangui.hotel.service.AdminService; | import cn.mafangui.hotel.service.AdminService; | ||||||
| import org.hibernate.validator.constraints.pl.REGON; |  | ||||||
| import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||||
| import org.springframework.web.bind.annotation.RequestMapping; | import org.springframework.web.bind.annotation.RequestMapping; | ||||||
| import org.springframework.web.bind.annotation.RequestMethod; | import org.springframework.web.bind.annotation.RequestMethod; | ||||||
| import org.springframework.web.bind.annotation.RestController; | import org.springframework.web.bind.annotation.RestController; | ||||||
|  |  | ||||||
| import java.util.List; | import java.util.HashMap; | ||||||
|  |  | ||||||
| @RestController | @RestController | ||||||
| @RequestMapping(value = "/admin") | @RequestMapping(value = "/admin") | ||||||
| @@ -18,19 +17,52 @@ public class AdminController { | |||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * 管理员登录 |      * 管理员登录 | ||||||
|      * @param userName |      * @param username | ||||||
|      * @param password |      * @param password | ||||||
|      * @return |      * @return | ||||||
|      */ |      */ | ||||||
|     @RequestMapping(method = RequestMethod.POST, value = "/login") |     @RequestMapping(method = RequestMethod.POST, value = "/login") | ||||||
|     public int login(String userName, String password){ |     public HashMap login(String username, String password){ | ||||||
|  |         HashMap result = new HashMap(); | ||||||
|         Admin admin = new Admin(); |         Admin admin = new Admin(); | ||||||
|         admin.setUserName(userName); |         admin.setUserName(username); | ||||||
|         admin.setPassword(password); |         admin.setPassword(password); | ||||||
|         if (adminService.login(admin) == null){ |         System.out.println(admin); | ||||||
|             return -1; |         if (adminService.selectByUserName(username) == null){ | ||||||
|  |             result.put("data","用户名不存在!"); | ||||||
|  |         }else if (adminService.login(admin) == null){ | ||||||
|  |             result.put("data","用户名或密码不正确!"); | ||||||
|  |         }else { | ||||||
|  |             result.put("data","登录成功!"); | ||||||
|         } |         } | ||||||
|         return 0; |         result.put("code",20000); | ||||||
|  |         result.put("token","admin"); | ||||||
|  |         return result; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @RequestMapping(value = "/logout") | ||||||
|  |     public HashMap logout(String token) { | ||||||
|  |         HashMap result = new HashMap(); | ||||||
|  |         result.put("code",20000); | ||||||
|  |         result.put("data",token); | ||||||
|  |         return result; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @RequestMapping(value = "/info") | ||||||
|  |     public HashMap info(String token, String username) { | ||||||
|  |         HashMap result = new HashMap(); | ||||||
|  |         HashMap data = new HashMap(); | ||||||
|  |         Admin admin = adminService.selectByUserName(username); | ||||||
|  |         if (admin == null){ | ||||||
|  |             data.put("data","用户名不存在!"); | ||||||
|  |         }else { | ||||||
|  |             data.put("data",admin); | ||||||
|  |         } | ||||||
|  |         result.put("code",20000); | ||||||
|  |         String[] roles = {"admin"}; | ||||||
|  |         data.put("roles",roles); | ||||||
|  |         result.put("data",data); | ||||||
|  |         return result; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
| @@ -61,6 +93,21 @@ public class AdminController { | |||||||
|         return adminService.updateProfile(admin); |         return adminService.updateProfile(admin); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 更改密码 | ||||||
|  |      * @param userName | ||||||
|  |      * @param password | ||||||
|  |      * @param newPassword | ||||||
|  |      * @return | ||||||
|  |      */ | ||||||
|  |     @RequestMapping(method = RequestMethod.POST,value = "/updatePassword") | ||||||
|  |     public int updatePassword(String userName, String password,String newPassword){ | ||||||
|  |         Admin admin = new Admin(); | ||||||
|  |         admin.setUserName(userName); | ||||||
|  |         admin.setPassword(password); | ||||||
|  |         return adminService.updatePassword(admin,newPassword); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * 查找管理员 |      * 查找管理员 | ||||||
|      * @param userName |      * @param userName | ||||||
| @@ -76,7 +123,10 @@ public class AdminController { | |||||||
|      * @return |      * @return | ||||||
|      */ |      */ | ||||||
|     @RequestMapping(value = "/getAllAdmin") |     @RequestMapping(value = "/getAllAdmin") | ||||||
|     public List<Admin> getAllAdmin(){ |     public HashMap getAllAdmin(){ | ||||||
|         return adminService.findAll(); |         HashMap result = new HashMap(); | ||||||
|  |         result.put("code",1); | ||||||
|  |         result.put("data",adminService.findAll()); | ||||||
|  |         return result; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -52,4 +52,15 @@ public class Admin { | |||||||
|     public void setUpdateTime(Date updateTime) { |     public void setUpdateTime(Date updateTime) { | ||||||
|         this.updateTime = updateTime; |         this.updateTime = updateTime; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public String toString() { | ||||||
|  |         return "Admin{" + | ||||||
|  |                 "adminId=" + adminId + | ||||||
|  |                 ", userName='" + userName + '\'' + | ||||||
|  |                 ", password='" + password + '\'' + | ||||||
|  |                 ", createTime=" + createTime + | ||||||
|  |                 ", updateTime=" + updateTime + | ||||||
|  |                 '}'; | ||||||
|  |     } | ||||||
| } | } | ||||||
| @@ -9,5 +9,6 @@ public interface AdminService { | |||||||
|     int register(Admin admin); |     int register(Admin admin); | ||||||
|     Admin selectByUserName(String userName); |     Admin selectByUserName(String userName); | ||||||
|     int updateProfile(Admin admin); |     int updateProfile(Admin admin); | ||||||
|  |     int updatePassword(Admin admin,String newPassword); | ||||||
|     List<Admin> findAll(); |     List<Admin> findAll(); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -12,4 +12,6 @@ public interface WorkerService { | |||||||
|     Worker selectWorker(String userName); |     Worker selectWorker(String userName); | ||||||
|     List<Worker> findAllWorker(); |     List<Worker> findAllWorker(); | ||||||
|     Worker login(Worker worker); |     Worker login(Worker worker); | ||||||
|  |     int logout(Worker worker); | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -13,28 +13,78 @@ public class AdminServiceImpl implements AdminService { | |||||||
|     @Autowired |     @Autowired | ||||||
|     private AdminMapper adminMapper; |     private AdminMapper adminMapper; | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 登录 | ||||||
|  |      * @param admin | ||||||
|  |      * @return | ||||||
|  |      */ | ||||||
|     @Override |     @Override | ||||||
|     public Admin login(Admin admin) { |     public Admin login(Admin admin) { | ||||||
|         return adminMapper.selectByUserNameAndPassword(admin); |         return adminMapper.selectByUserNameAndPassword(admin); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 注册 | ||||||
|  |      * @param admin | ||||||
|  |      * @return | ||||||
|  |      */ | ||||||
|     @Override |     @Override | ||||||
|     public int register(Admin admin) { |     public int register(Admin admin) { | ||||||
|         return adminMapper.insertSelective(admin); |         return adminMapper.insertSelective(admin); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 根据username查找 | ||||||
|  |      * @param userName | ||||||
|  |      * @return | ||||||
|  |      */ | ||||||
|     @Override |     @Override | ||||||
|     public Admin selectByUserName(String userName) { |     public Admin selectByUserName(String userName) { | ||||||
|         return adminMapper.selectByUserName(userName); |         Admin result = adminMapper.selectByUserName(userName); | ||||||
|  |         if (result != null){ | ||||||
|  |             result.setPassword(null); | ||||||
|  |         } | ||||||
|  |         return result; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 更新信息 | ||||||
|  |      * @param admin | ||||||
|  |      * @return | ||||||
|  |      */ | ||||||
|     @Override |     @Override | ||||||
|     public int updateProfile(Admin admin) { |     public int updateProfile(Admin admin) { | ||||||
|         return adminMapper.updateByUserNameSelective(admin); |         return adminMapper.updateByUserNameSelective(admin); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 更改密码 | ||||||
|  |      * @param admin | ||||||
|  |      * @param newPassword | ||||||
|  |      * @return | ||||||
|  |      */ | ||||||
|  |     @Override | ||||||
|  |     public int updatePassword(Admin admin, String newPassword) { | ||||||
|  |         int result = 0; | ||||||
|  |         if(adminMapper.selectByUserNameAndPassword(admin) != null){ | ||||||
|  |             admin.setPassword(newPassword); | ||||||
|  |             result = adminMapper.updateByUserNameSelective(admin); | ||||||
|  |         } | ||||||
|  |         return result; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 列出所有管理员 | ||||||
|  |      * @return | ||||||
|  |      */ | ||||||
|     @Override |     @Override | ||||||
|     public List<Admin> findAll() { |     public List<Admin> findAll() { | ||||||
|         return adminMapper.findAll(); |         List<Admin> result; | ||||||
|  |         result = adminMapper.findAll(); | ||||||
|  |         for (Admin admin : result) { | ||||||
|  |             admin.setPassword(null); | ||||||
|  |         } | ||||||
|  |         return result; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -43,4 +43,9 @@ public class WorkerServiceImpl implements WorkerService { | |||||||
|     public Worker login(Worker worker) { |     public Worker login(Worker worker) { | ||||||
|         return workerMapper.selectByUserNameAndPassword(worker); |         return workerMapper.selectByUserNameAndPassword(worker); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public int logout(Worker worker) { | ||||||
|  |         return 0; | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user