Java多线程+网络编程登录管理实例
用户包含管理员和普通用户两个角色进行测试。 假定:管理员用户名:admin,密码:abc 普通用户名:user 密码:123
- 用户从客户端发起登录请求,客户端将数据传递到服务器端,由服务器做验证。服务器端保存用户数据。用户登录成功提示登录成功,并显示功能模块,登录失败提示:用户名或密码不正确。
- 在服务器端启用多线程模式,要求:每一个用户发送登录请求到服务器后,服务器端就分配一个线程去处理这个请求。以实现服务器端多线程模式下的用户登录。
代码实现:
Bean层
1、RequestBody 类请求体继承Serializable:
public class RequestBody implements Serializable {
public static final String LOGIN = "login";
private static final long serialVersionUID = -1593966669454243804L;
private String cmd;
private User user;
public String getCmd() {
return cmd;
}
public void setCmd(String cmd) {
this.cmd = cmd;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
ResponseBody 类响应体继承Serializable
public class ResponseBody implements Serializable {
private static final long serialVersionUID = -6315166158520343036L;
private int result;
private String status;
private boolean ok;
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public boolean isOk() {
return ok;
}
public void setOk(boolean ok) {
this.ok = ok;
}
}
User 登录者类
public class User implements Serializable {
private static final long serialVersionUID = 3646073779387514873L;
private String username,password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(username, user.username) &&
Objects.equals(password, user.password);
}
@Override
public int hashCode() {
return Objects.hash(username, password);
}
}
Dao层
UserDao 类数据层
public class UserDao {
public static final int ADMIN_SUCCESS = 1;
public static final int USER_SUCCESS = 2;
public static final int LOGIN_FAIL = 3;
private HashMap<User, Integer> map = new HashMap<>();
{
map.put(new User("admin", "abc"), ADMIN_SUCCESS);
map.put(new User("user", "123"), USER_SUCCESS);
}
public int login(User user) {
System.out.println(user);
Integer loginResult = map.get(user);
System.out.println(loginResult);
return loginResult == null ? LOGIN_FAIL : loginResult;
}
}
View层
Views 类视图层,展示菜单以及键值输入等提示信息
public class Views {
private Scanner sc = new Scanner(System.in);
public void welcome() {
System.out.println("欢迎使用网络版-快递e栈");
}
public void showMainMenu() {
System.out.println("请选择:1.登录,2.离开");
}
public int inputKey(int max) {
int num = 1;
System.out.println("请输入1~" + max + "的数字:");
String str = sc.nextLine();
try {
num = Integer.parseInt(str);
} catch (NumberFormatException e) {
}
if (num < 1 || num > max) {
num = inputKey(max);
}
return num;
}
public void bye() {
System.out.println("谢谢使用,再见!");
}
public User inputUser() {
String username = inputStr("请输入用户名:");
String password = inputStr("请输入密码:");
return new User(username, password);
}
private String inputStr(String cmd) {
System.out.println(cmd);
return sc.nextLine();
}
public void loginFail() {
System.out.println("登录失败!");
}
}
Controller层
Client 类客户端
public class Client {
public int login(User user) throws IOException, ClassNotFoundException {
Socket socket=new Socket("127.0.0.1",12345);
RequestBody body=new RequestBody();
body.setCmd(RequestBody.LOGIN);
body.setUser(user);
writeRequest(socket, body);
ResponseBody responseBody = getResponseBody(socket);
socket.close();
return responseBody.getResult();
}
private ResponseBody getResponseBody(Socket socket) throws IOException, ClassNotFoundException {
ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
return (ResponseBody) ois.readObject();
}
private void writeRequest(Socket socket, RequestBody body) throws IOException {
ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(body);
}
}
Server 服务器端
public class Server {
private static UserDao userDao=new UserDao();
public static void main(String[] args) throws IOException, ClassNotFoundException {
ServerSocket ss=new ServerSocket(12345);
while (true) {
Socket socket = ss.accept();
new Thread(() -> {
try {
handleSocket(socket);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}).start();
}
}
private static void handleSocket(Socket socket) throws IOException, ClassNotFoundException {
RequestBody requestBody = getRequestBody(socket);
switch (requestBody.getCmd()){
case RequestBody.LOGIN:
loginUser(socket,requestBody);
break;
}
}
private static void loginUser(Socket socket,RequestBody requestBody) throws IOException {
User user = requestBody.getUser();
int result=userDao.login(user);
ResponseBody body=new ResponseBody();
body.setResult(result);
writeResponse(socket, body);
}
private static void writeResponse(Socket socket, ResponseBody body) throws IOException {
ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(body);
}
private static RequestBody getRequestBody(Socket socket) throws IOException, ClassNotFoundException {
ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
return (RequestBody) ois.readObject();
}
}
Main
public class Main {
static Views v = new Views();
static Client c = new Client();
public static void main(String[] args) throws IOException, ClassNotFoundException {
v.welcome();
boolean flag = true;
while (flag) {
v.showMainMenu();
int key = v.inputKey(2);
switch (key) {
case 1:
loginUser();
break;
case 2:
v.bye();
flag = false;
break;
}
}
}
private static void loginUser() throws IOException, ClassNotFoundException {
User user = v.inputUser();
int login = c.login(user);
switch (login) {
case UserDao
.ADMIN_SUCCESS:
adminLoop();
break;
case UserDao.USER_SUCCESS:
userLoop();
break;
case UserDao.LOGIN_FAIL:
v.loginFail();
break;
}
}
private static void userLoop() {
System.out.println("用户的操作");
}
private static void adminLoop() {
System.out.println("管理员操作");
}
}
测试结果:
欢迎使用网络版-快递e栈
请选择:1.登录,2.离开
请输入1~2的数字:
1
请输入用户名:
admin
请输入密码:
abc
管理员操作
请选择:1.登录,2.离开
请输入1~2的数字:
1
请输入用户名:
user
请输入密码:
123
用户的操作
User{username='admin', password='abc'}
1
User{username='user', password='123'}
2
|