该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
@Controller
@RequestMapping("/login")
public class LoginController {
private TxtLogger logger = TxtLogger.getInstance(LoginController.class.getName());
@Autowired
IUserService iUserService;
@Autowired
IRolePermissionService iRolePermissionService;
@Autowired
IPubBtCmpMasService iPubBtCmpMasService;
/**
* 登录页面
*
* @param request
* @return
*/
@RequestMapping("/toLogin")
public String toLoginPage(HttpServletRequest request) {
Object obj = request.getSession().getAttribute(UserConstant.SESSION_LOGIN_UUSERMAS);
request.getSession().setAttribute("version", StaticParameters.sysVersion);
if (obj == null || (((UUserMas) obj).getWorkCode()).isEmpty()) {
return "/user/login";
}
return "/main/index";
}
/**
* 用户登录
*/
@RequestMapping("/login")
@ResponseBody
public String login(HttpServletRequest request, HttpServletResponse response, String workCode, String password) {
logger.info("用户登录 workCode = {}", workCode);
int flag = 0;
try {
if (workCode == null || password == null || workCode.isEmpty() || password.isEmpty()) {
return JsonUtil.toJson(flag);// 参数异常
}
// 根据输入用户名进行校验是否存在:用户工号/用户编号
UUserMas uUserMas = iUserService.queryCodeAndPassWord(workCode);
// 获取用户信息
if (null == uUserMas || uUserMas.getUserCode() == null || "".equals(uUserMas.getUserCode())) {
return JsonUtil.toJson(-4);// 用户不存在
}
// 密码已在前台做是否存在校验
if (uUserMas.getPassword().equals(password)) {
if (uUserMas.getStatus().equals("N")) {
// 设置登录日志对象
ULoginDetail uLoginDetail = new ULoginDetail();
uLoginDetail.setUserCode(uUserMas.getUserCode());// 登录用户
uLoginDetail.setLoginServer(request.getServerName());// 登录服务器
uLoginDetail.setLoginIp(RequestUtils.getIpAddr(request));// 登录ip
iUserService.updateLoginTime(uUserMas.getWorkCode(), uLoginDetail);
// 查询用户城市名称
// PubCityCompanyMas pubCityCompanyMas =
// pubCityAndBranchService.queryPubCityCompanyMas(null,
// uUserMas.getCityCode());
// request.getSession().setAttribute("userCityName",
// pubCityCompanyMas.getCityName());
// 查询用户部门名称
UDepartment department = UserConstant.U_DEPARTMENT.get(uUserMas.getDepartment());
request.getSession().setAttribute("userDeptName", department.getDeptName());
request.getSession().setAttribute("cityCode", uUserMas.getCityCode());
request.getSession().setAttribute(UserConstant.SESSION_LOGIN_UUSERMAS, uUserMas);
} else if (uUserMas.getStatus().equals("D")) {
return JsonUtil.toJson(-7);// 该用户被注销
}
} else {
return JsonUtil.toJson(-5);// 该用户密码输入不正确
}
// 当前用户的所有权限
List userMenuTree = iUserService.queryUserPermission(uUserMas.getWorkCode(),
uUserMas.getUserCode());
if (userMenuTree != null && !userMenuTree.isEmpty()) {
request.getSession().setAttribute(UserConstant.SESSION_USER_PERMISSION, userMenuTree);
// 操作权限:R1
List accessPermissions = new ArrayList<>();
for (UPermissionMas o : userMenuTree) {
if (null != o) {
if ("R1".equals(o.getPermissionType())) {
accessPermissions.add(o.getPageUrl());
}
}
}
request.getSession().setAttribute(UserConstant.SESSION_USER_ACCESS_TREE, accessPermissions);
}
// 用户菜单权限
request.getSession().setAttribute("menu", JsonUtil.toJson(generateMenuTree(userMenuTree)));
// 系统日期
request.getSession().setAttribute(UserConstant.SESSION_SYSTEM_DATE, DateUtils.getNowDate());
// 系统IP
String serverIp = getServerIp();
logger.info("登录服务器IP={}", serverIp);
request.getSession().setAttribute(UserConstant.SESSION_SYSTEM_IP, serverIp);
// 额度权限
Map mapBean = new HashMap<>();
mapBean.put("userCode", uUserMas.getUserCode());
mapBean.put("permissionType", "R2");
Map findUserPermission = iRolePermissionService
.findUserPermission(uUserMas.getWorkCode(), mapBean);
request.getSession().setAttribute(UserConstant.SESSION_USER_LIMIT_TREE, findUserPermission);
// 移除登出标识
request.getSession().removeAttribute("SYS_LOGIN_OUT");// 移除标识
// 默认代偿信息
PubBtCmpMas btCmpMas = new PubBtCmpMas();
btCmpMas.setBtcmpId(LoanConstant.COMP_DEFAULT_COMPANY_FLAG);
PubBtCmpMas pubBtCmpMas = iPubBtCmpMasService.selectByPrimaryKey(uUserMas.getUserCode(), btCmpMas);
if (null != pubBtCmpMas) {
LCmpensationMas lCmpensationMas = new LCmpensationMas();
lCmpensationMas.setBtcmpId(LoanConstant.COMP_DEFAULT_COMPANY_FLAG);
lCmpensationMas.setCpsCmpNm(pubBtCmpMas.getCpsCmpNm());
lCmpensationMas.setCpsAcctNm(pubBtCmpMas.getCpsAcctNm());
lCmpensationMas.setCpsBankNo(pubBtCmpMas.getCpsBankNo());
lCmpensationMas.setInternalBankCode(pubBtCmpMas.getInternalBankCode());
lCmpensationMas.setBankAcctProv(pubBtCmpMas.getBankAcctProv());
lCmpensationMas.setBankAcctCity(pubBtCmpMas.getBankAcctCity());
lCmpensationMas.setSysType(pubBtCmpMas.getSysType());
request.getSession().setAttribute("defaultCmp", lCmpensationMas);
}
if ("Y".equals(uUserMas.getChangePasswdFlag())) {
flag = 2;// 强制改密【被上级管理员修改过密码、第一次登陆系统】
} else {
flag = 1;// 登录成功
}
logger.info("用户登录成功,workCode={}", workCode);
} catch (Exception e) {
flag = -1;
logger.error("用户登录失败", e);
}
return JsonUtil.toJson(flag);
}
/**
* 用户退出
*/
@RequestMapping("/logout")
public ModelAndView logout(HttpServletRequest request, HttpServletResponse response) {
UUserMas uUserMas = (UUserMas) request.getSession().getAttribute(UserConstant.SESSION_LOGIN_UUSERMAS);
try {
request.getSession().invalidate();// session去除
ULoginDetail uLoginDetail = new ULoginDetail();
if (uUserMas != null) {
logger.info("用户退出;userCode={}", uUserMas.getUserCode());
uLoginDetail.setUserCode(uUserMas.getUserCode());// 登录用户
iUserService.updateLoginOutTime(uLoginDetail, uUserMas.getWorkCode());
}
} catch (Exception e) {
logger.error("用户登出失败!参数:uLoginDetail = {}", uUserMas, e);
}
request.getSession().setAttribute("SYS_LOGIN_OUT", "Y");
// 重定向跳转到登录页面
return new ModelAndView("redirect:/main/index.do");
}
private static final String TRUNK_MENU_ICON = "icon-home";
private static final String LEAF_MENU_ICON = "icon-glass";
private List generateMenuTree(List permissionMasList) {
List list = new ArrayList<>();
if (null == permissionMasList || permissionMasList.isEmpty())
return list;
List menus = new ArrayList<>();
Map map = new HashMap<>();
// printList(permissionMasList);
for (UPermissionMas mas : permissionMasList) {
if (null == mas)
continue;
if ("M".equals(mas.getPermissionType())) {
if (null == mas.getParentMenuCode()) {
MenuTree mainMenu = new MenuTree();
mainMenu.setId(mas.getPermissionCode());
mainMenu.setText(mas.getPermissionName());
mainMenu.setIcon(TRUNK_MENU_ICON);
mainMenu.setUrl("");
list.add(mainMenu);
map.put(mas.getPermissionCode(), mainMenu);
} else {
menus.add(mas);
}
}
}
for (UPermissionMas mas : menus) {
MenuTree trunkMenu = map.get(mas.getParentMenuCode());
if (null == trunkMenu)
continue;
MenuTree leafMenu = new MenuTree();
leafMenu.setId(mas.getPermissionCode());
leafMenu.setText(mas.getPermissionName());
leafMenu.setIcon(LEAF_MENU_ICON);
leafMenu.setUrl(mas.getEsbPageUrl());
trunkMenu.addLeafMenu(leafMenu);
}
Collections.sort(list);
for (MenuTree menuTree : list)
Collections.sort(menuTree.getMenus());
return list;
}
private void printList(Collection t) {
if (null == t)
return;
for (T ts : t)
System.out.println(ts);
}
/**
* 获取服务器地址
*
* @return Ip地址
*/
public String getServerIp() {
// 获取操作系统类型
String sysType = System.getProperties().getProperty("os.name");
String ip;
if (sysType.toLowerCase().startsWith("win")) { // 如果是Windows系统,获取本地IP地址
String localIP = null;
try {
localIP = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
logger.error(e.getMessage(), e);
}
if (localIP != null) {
return localIP;
}
} else {
ip = getIpByEthNum("eth0"); // 兼容Linux
if (ip != null) {
return ip;
}
}
return "获取服务器IP错误";
}
/**
* 根据网络接口获取IP地址
*
* @param ethNum
* 网络接口名,Linux下是eth0
* @return
*/
private String getIpByEthNum(String ethNum) {
try {
Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
if (ethNum.equals(netInterface.getName())) {
Enumeration addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
ip = (InetAddress) addresses.nextElement();
if (ip != null && ip instanceof Inet4Address) {
return ip.getHostAddress();
}
}
}
}
} catch (SocketException e) {
logger.error(e.getMessage(), e);
}
return "获取服务器IP错误";
}
}