AuthenticateService.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package com.yihu.hos.tenant.service;
  2. import com.yihu.hos.common.constants.ContextAttributes;
  3. import com.yihu.hos.interceptor.LocalContext;
  4. import com.yihu.hos.tenant.model.TenantModel;
  5. import com.yihu.hos.tenant.model.TenantSession;
  6. import com.yihu.hos.system.dao.UserDao;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import javax.annotation.Resource;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import javax.servlet.http.HttpSession;
  13. /**
  14. * @created Airhead 2016/11/16.
  15. */
  16. @Service
  17. public class AuthenticateService {
  18. @Autowired
  19. private UserDao userDao;
  20. @Resource(name = TenantService.BEAN_ID)
  21. private TenantService tenantService;
  22. public boolean isAuth(HttpServletRequest request, HttpServletResponse response) throws Exception {
  23. HttpSession session = request.getSession();
  24. TenantSession tenantSession = (TenantSession) session.getAttribute(ContextAttributes.TENANT_SESSION);
  25. if (tenantSession == null) {
  26. return false;
  27. }
  28. LocalContext.getContext().setAttachment(ContextAttributes.SCHEMA, tenantSession.getSchema());
  29. return true;
  30. }
  31. /**
  32. * //TODO 需要进一步完善逻辑
  33. * 租户登录授权
  34. * @param httpSession
  35. * @param tenantName
  36. * @return
  37. * @throws Exception
  38. */
  39. public boolean auth(HttpSession httpSession, String tenantName) throws Exception {
  40. //授权租户,切换到指定数据库,需要先清除原先的数据库指定,切回global_db表,获取租户信息
  41. LocalContext.getContext().removeAttachment(ContextAttributes.SCHEMA);
  42. TenantModel tenantModel = tenantService.findTenantByName(tenantName);
  43. //TODO:现在只是简单逻辑,需要修改成安全逻辑
  44. if (tenantModel == null ) {
  45. return false;
  46. }
  47. //切换Schema到对应租户
  48. LocalContext.getContext().setAttachment(ContextAttributes.SCHEMA, tenantModel.getSchema());
  49. LocalContext.getContext().setAttachment(ContextAttributes.SCHEMA_TEMP, tenantModel.getSchema());
  50. // UserModel userModel = userDao.findOne(user);
  51. // //TODO:现在只是简单逻辑,需要修改成安全逻辑
  52. // if (userModel == null || !password.equals(userModel.getPassword())) {
  53. // return "User Auth Failed";
  54. // }
  55. LocalContext.getContext().setAttachment(ContextAttributes.TENANT_NAME,tenantModel.getName());
  56. httpSession.setAttribute(ContextAttributes.TENANT_SESSION, new TenantSession(tenantModel.getName(), tenantModel.getSchema()));
  57. return true;
  58. }
  59. /**
  60. * 登出 - 移除租户授权信息
  61. * @param httpSession
  62. * @return
  63. * @throws Exception
  64. */
  65. public String logout(HttpSession httpSession) throws Exception {
  66. //移除Schema及缓存
  67. LocalContext.getContext().removeAttachment(ContextAttributes.SCHEMA);
  68. LocalContext.getContext().removeAttachment(ContextAttributes.SCHEMA_TEMP);
  69. httpSession.removeAttribute(ContextAttributes.TENANT_SESSION);
  70. return "Tenan Logout Success";
  71. }
  72. }