⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 user.java

📁 银行项目为后台socket通信写的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
 * Package		: com.ecSolutions.ecAppServer.appSession
 * File			: User.java
 * Create Date  : 2007-7-20
 * Author		: Steven Chen
 * 
 * Copyright(C) 2006 ecSolutions(shanghai) Co.,Limited.All Rights Reserved.
 *			
 ***************************************************************************/
package com.ecSolutions.ecAppServer.appSession;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.WeakHashMap;

import com.ecSolutions.ecAppServer.Configuration;
import com.ecSolutions.ecAppServer.ecAppServerConfigException;
import com.ecSolutions.ecAppServer.config.PropertiesConfiguration;
import com.ecSolutions.ecAppServer.util.FileUtil;
import com.ecSolutions.ecAppServer.util.StringUtil;

import org.apache.log4j.Logger;

import com.ecSolutions.ecAppServer.util.DbUtil;

public class User {
	private String messageSize;

	private String messageType;

	private String localMachineName;

	private String userId;

	private String password;

	private String clientMachineName;

	private String usermenu;

	private Process process;
	
	

	private ReferenceQueue processQueue;

	public int port = 0;

	private static final String MENU_FITASMUN = "FITASMUN";

	private static Logger log = Logger.getLogger("User");

	private String date = "";

	private String sessionId = "";

	private final static String CONFIG_FILE = "ecAppConfig.properties";

	public User(String request) {
		this.messageSize = request.substring(0, 4);
		this.messageType = request.substring(4, 8);
		this.localMachineName = request.substring(8, 24);
		this.userId = request.substring(24, 34);
		this.password = request.substring(34, 64);
		this.clientMachineName = request.substring(64, 74).toUpperCase();
	}

	public String getClientMachineName() {
		return clientMachineName;
	}

	public void setClientMachineName(String clientMachineName) {
		this.clientMachineName = clientMachineName;
	}

	public String getLocalMachineName() {
		return localMachineName;
	}

	public void setLocalMachineName(String localMachineName) {
		this.localMachineName = localMachineName;
	}

	public String getMessageSize() {
		return messageSize;
	}

	public void setMessageSize(String messageSize) {
		this.messageSize = messageSize;
	}

	public String getMessageType() {
		return messageType;
	}

	public void setMessageType(String messageType) {
		this.messageType = messageType;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUsermenu() {
		return usermenu;
	}
	public Process getProcess() {
		return process;
	}

	public void setProcess(Process process) {
		this.process = process;
	}

	public boolean checkUserExists(Connection conn) throws UserLoginFailException {
		String sql = "select FLUSRID from T56M1XXX where FLUSRID = ? and WRKSTN <>?";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, userId);
			pstmt.setString(2, clientMachineName.trim());
			rs = pstmt.executeQuery();
			if (rs.next()) {
				return true;
			}
		} catch (SQLException e) {
			log.error("user login query exception:" + e.toString());
			throw new UserLoginFailException("check User query exception");
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
				}
				rs = null;
			}
			if (pstmt != null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
				}
				pstmt = null;
			}

		}

		return false;
	}

	public boolean checkClientMachineExists(Connection conn) throws UserLoginFailException {
		String sql = "select FLUSRID from T56M1XXX where WRKSTN =?";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, clientMachineName.trim());
			rs = pstmt.executeQuery();
			if (rs.next()) {
				return true;
			}
		} catch (SQLException e) {
			log.error("user login query exception:" + e.toString());
			throw new UserLoginFailException("check Client Machine query exception");
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
				}
				rs = null;
			}
			if (pstmt != null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
				}
				pstmt = null;
			}

		}

		return false;
	}

	public boolean checkUserLocked(Connection conn) throws UserLoginFailException {
		String sql = "select FLUSRID from T57M0XXX where FLUSRID =? and FLSTSCD=?";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, userId);
			pstmt.setString(2, "L");
			rs = pstmt.executeQuery();
			if (rs.next()) {
				return true;
			}
		} catch (SQLException e) {
			log.error("user login query exception:" + e.toString());
			throw new UserLoginFailException("check Client Machine query exception");
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
				}
				rs = null;
			}
			if (pstmt != null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
				}
				pstmt = null;
			}

		}

		return false;
	}

	public String userLogin(Connection conn) throws UserLoginFailException {
		String strMenu = null;

		String sql = "select ENPASS,MENU from TAUSRMAS where FLUSRID = ?";
		String historysql = "select PASDATE,PASTIME from T58M0XXX where FLUSRID = ?";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String passwd = "", menu = "";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, userId);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				passwd = rs.getString("ENPASS");
				menu = rs.getString("MENU");
				if (menu != null && menu.trim().equals("")) {
					menu = MENU_FITASMUN;
				}

				pstmt = conn.prepareStatement(historysql);

				pstmt.setString(1, StringUtil.stringFitToLength(userId, 20));
				rs = pstmt.executeQuery();
				if (rs.next()) {
					String padate = String.valueOf(rs.getInt("PASDATE"));
					int patime = rs.getInt("PASTIME");
					SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
					Date dt = sdf.parse(padate);

					Configuration config = PropertiesConfiguration.getInstance();
					int expiredmonth = config.getInt("PASSPER", 1);
					Date currentdate = new Date();
					long periodTime = currentdate.getTime() - dt.getTime() - transferTime(patime);
					if (periodTime / 24 / 60 / 60 / 1000 > expiredmonth * 30) {
						throw new UserLoginFailException("Password expiried");
					}
				}
			} else {
				throw new UserLoginFailException("EA01621 Invalid User");
			}

		} catch (SQLException e) {
			log.error("user login query exception:" + e.toString());
			throw new UserLoginFailException("user login query exception");
		} catch (ParseException e) {
			log.error("Parse date exception:" + e.toString());
			throw new UserLoginFailException("Parse date exception");
		} catch (ecAppServerConfigException e) {
			log.info("config file reading failure......");
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
				}
				rs = null;
			}
			if (pstmt != null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
				}
				pstmt = null;
			}
			/*
			 * if (conn != null) { try { conn.close(); } catch (SQLException e) { }
			 * conn = null; }
			 */
		}
		if (passwd.trim().equals(password.trim())) {
			strMenu = menu;
		} else {
			// throw User Login Fail Exception with Code "EA01622";
			log.info("login user: " + userId + "with password: " + password + "  login failure, user password is invalid.");
			if (passwordWrong()) {
				//user locked
				throw new UserLoginFailException("TAM0030");
			}
			throw new UserLoginFailException("EA01622 Invalid Password");
		}
		this.usermenu = strMenu;
		return strMenu;
	}

	private long transferTime(int patime) {
		String patm = StringUtil.stringAddZero(String.valueOf(patime), 8);
		int hour = Integer.parseInt(patm.substring(0, 2));
		int minute = Integer.parseInt(patm.substring(2, 4));
		int second = Integer.parseInt(patm.substring(4, 6));
		//int minsecond=Integer.parseInt(patm.substring(6,8));
		long time = hour * 60 * 60 + minute * 60 + second;
		return time;

	}

	private boolean passwordWrong() {
		boolean locked = false;
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			connection = DbUtil.getConnection();
			pstmt = connection.prepareStatement("select PWDTCNT from T57M0XXX where FLUSRID=?");
			pstmt.setString(1, userId);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				int pwdcnt = rs.getInt("PWDTCNT");
				Configuration config = PropertiesConfiguration.getInstance();
				int tryNo = config.getInt("RETRYTM", 3);
				if (pwdcnt >= tryNo) {//try extend number
					// user lock
					pstmt = connection.prepareStatement("update T57M0XXX set FLSTSCD = ?,PWDTCNT=? where FLUSRID=?");
					pstmt.setString(1, "L");
					pstmt.setInt(2, pwdcnt + 1);
					pstmt.setString(3, userId);
					pstmt.execute();
					insertToT60(connection, "5");
					locked = true;
				} else {
					pstmt = connection.prepareStatement("update T57M0XXX set PWDTCNT = ? where FLUSRID=?");
					pstmt.setInt(1, pwdcnt + 1);
					pstmt.setString(2,userId);
					pstmt.execute();
				}
			} else {
				pstmt = connection.prepareStatement("INSERT INTO T57M0XXX(FLUSRID,PWDTCNT,FLSTSCD) values(?,?,?)");
				pstmt.setString(1, userId);
				pstmt.setInt(2, 1);
				pstmt.setString(3, " ");
				pstmt.execute();
			}
			connection.commit();
		} catch (SQLException e) {
			log.error("update user status exception:" + e.toString());
			try {
				connection.rollback();
			} catch (SQLException e1) {
			}
		} catch (ecAppServerConfigException e) {
			log.info("config file reading failure......");
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					log.error("update user status exception:" + e.toString());
				}
				rs = null;
			}
			if (pstmt != null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
					log.error("update user status exception:" + e.toString());
				}
				pstmt = null;
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
				}
				connection = null;
			}
		}
		return locked;
	}

	public void createUserSession(Connection conn) throws UserLoginFailException {
		int sessionId = 0;
		int pid = 3000;
		boolean hasRecord = false;

		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "select PID from T56M0XXX where SESSID = ?";
		String updateSql = "UPDATE T56M0XXX set pid = ?,sessid=?, lochost=?, comport=?, FLUSRID=? where SESSID=?";
		String insertSql = "INSERT INTO T56M0XXX(SESSID,PID,LOCHOST,COMPORT,FLUSRID) values(?,?,?,?,?)";
		String insertM1Sql = "INSERT INTO T56M1XXX(SESSID,LOCHOST,COMPORT,WRKSTN,FLUSRID,PID) values(?,?,?,?,?,?)";

		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, 9999);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				pid = rs.getInt(1);
				pid++;
				if (pid > 4000) {
					pid = 3000;
				}
				sessionId = pid;
				port = 2000 + sessionId;
				hasRecord = true;
			} else {
				sessionId = 3000;
				port = 2000 + sessionId;
				hasRecord = false;
			}
			if (hasRecord) {
				pstmt = conn.prepareStatement(updateSql);
				pstmt.setInt(1, pid);
				pstmt.setInt(2, 9999);
				pstmt.setString(3, localMachineName);
				pstmt.setString(4, String.valueOf(port));
				pstmt.setString(5, userId);
				pstmt.setInt(6, 9999);
				pstmt.execute();
				pstmt = conn.prepareStatement(insertSql);
				pstmt.setInt(1, sessionId);
				pstmt.setInt(2, pid);
				pstmt.setString(3, localMachineName);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -