|  | @ -1,7 +1,14 @@
 | 
	
		
			
				|  |  | package com.yihu.hos.saas.services.impl;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.yihu.hos.saas.models.UserSession;
 | 
	
		
			
				|  |  | import com.yihu.hos.saas.constants.ContextAttributes;
 | 
	
		
			
				|  |  | import com.yihu.hos.saas.daos.UserDao;
 | 
	
		
			
				|  |  | import com.yihu.hos.saas.daos.UserTenantDao;
 | 
	
		
			
				|  |  | import com.yihu.hos.saas.models.UserModel;
 | 
	
		
			
				|  |  | import com.yihu.hos.saas.models.UserTenantModel;
 | 
	
		
			
				|  |  | import com.yihu.hos.saas.models.bo.UserSession;
 | 
	
		
			
				|  |  | import com.yihu.hos.saas.services.AuthenticateService;
 | 
	
		
			
				|  |  | import com.yihu.hos.saas.utils.LocalContext;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import javax.servlet.http.HttpServletRequest;
 | 
	
		
			
				|  |  | import javax.servlet.http.HttpServletResponse;
 | 
	
	
		
			
				|  | @ -11,10 +18,39 @@ import javax.servlet.http.HttpSession;
 | 
	
		
			
				|  |  |  * @created Airhead 2016/11/16.
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | public class AuthenticateServiceImpl implements AuthenticateService {
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private UserDao userDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private UserTenantDao userTenantDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Override
 | 
	
		
			
				|  |  |     public boolean isAuth(HttpServletRequest request, HttpServletResponse response) throws Exception {
 | 
	
		
			
				|  |  |         HttpSession session = request.getSession();
 | 
	
		
			
				|  |  |         UserSession userSession = (UserSession) session.getAttribute("userSession");
 | 
	
		
			
				|  |  |         return userSession != null;
 | 
	
		
			
				|  |  |         if (userSession == null) {
 | 
	
		
			
				|  |  |             return false;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         UserTenantModel userTenantModel = userTenantDao.findOne(userSession.getUserCode());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         LocalContext.getContext().setAttachment(ContextAttributes.SCHEMA, userTenantModel.getTenantSchema());
 | 
	
		
			
				|  |  |         return true;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Override
 | 
	
		
			
				|  |  |     public String auth(HttpServletRequest request, HttpServletResponse response) {
 | 
	
		
			
				|  |  |         String name = request.getParameter("name");
 | 
	
		
			
				|  |  |         String password = request.getParameter("password");
 | 
	
		
			
				|  |  |         UserModel userModel = userDao.findOne(name);
 | 
	
		
			
				|  |  |         if (userModel == null) {
 | 
	
		
			
				|  |  |             return "Auth Failed";
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         if (password.equals(userModel.getPassword())) {     //简单逻辑
 | 
	
		
			
				|  |  |             HttpSession session = request.getSession();
 | 
	
		
			
				|  |  |             session.setAttribute("userSession", new UserSession(userModel.getCode()));
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return "Auth Success";
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 |