package com.yihu.hos.system.service; import com.yihu.hos.common.constants.ContextAttributes; import com.yihu.hos.interceptor.LocalContext; import com.yihu.hos.system.model.SystemUser; import com.yihu.hos.tenant.model.TenantSession; import com.yihu.hos.web.framework.model.ActionResult; import org.apache.commons.codec.digest.DigestUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.servlet.http.HttpSession; /** * Created by HZP on 2016/02/26. */ @Service("systemManager") public class SystemManager { public static final String BEAN_ID = "systemManager"; @Resource(name = UserManager.BEAN_ID) private UserManager userManager; @Value("${spring.administrators}") private String saasAdmin; /* 登录操作 */ public ActionResult loginAction(HttpSession session,String user,String password) throws Exception { ActionResult result = null; TenantSession tenantSession = (TenantSession)session.getAttribute(ContextAttributes.TENANT_SESSION); //特殊账户 if(user.equals("admin") && password.equals("JKZL")) { SystemUser userInfo = new SystemUser(); userInfo.setLoginCode("admin"); userInfo.setUserName("管理员"); session.setAttribute("userInfo",userInfo); tenantSession.setUserCode("admin");//设置租户code tenantSession.setRole("admin");//标识为管理员账号 session.setAttribute(ContextAttributes.TENANT_SESSION, tenantSession); LocalContext.getContext().setAttachment(ContextAttributes.TENANT_NAME,tenantSession.getUserCode()); LocalContext.getContext().setAttachment(ContextAttributes.SCHEMA, tenantSession.getSchema()); result = new ActionResult(true,"登录成功!"); result.setData(tenantSession); return result; } //根据用户名/密码到总平台校验 SystemUser userInfo = userManager.getUserByLoginCode(user); if(userInfo==null) { result = new ActionResult(false,"登录失败!用户不存在!"); return result; } else{ String saltValue =userInfo.getSaltValue(); String userPassword = userInfo.getPassword(); if(userPassword.equals(DigestUtils.md5Hex(password + saltValue))) { if (saasAdmin.equals(user)){ tenantSession.setRole("admin");//标识为管理员账号 } tenantSession.setUserCode(user); session.setAttribute(ContextAttributes.TENANT_SESSION, tenantSession); session.setAttribute("userInfo",userInfo); result = new ActionResult(true,"登录成功!"); result.setData(tenantSession); return result; } else{ result = new ActionResult(false,"登录失败!密码错误!"); return result; } } } }