有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javasocket身份验证服务器?

我目前正在使用java为桌面应用程序创建身份验证服务器,到目前为止,我已经能够使客户端和服务器像聊天服务器/客户端一样进行通信

我确实意识到我只完成了一小部分工作,还有很多东西需要学习,但现在在这个阶段,我想知道身份验证是如何完成的

例如,这是服务器代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class LoginServer
{
    public static void main(String[] args) throws Exception {
        int port = 7000;
        int id = 1;

        ServerSocket loginserver = null;
        try
        {
            loginserver = new ServerSocket(port);
            System.out.println("LoginServer started...");
        }
        catch (IOException e) {
            System.out.println("Could not listen on port: " + port);
            System.exit(-1);
        }

        while (true)
        {
            Socket clientSocket = loginserver.accept();
            ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
            cliThread.start();
        }
    }
}

class ClientServiceThread extends Thread
{
    Socket clientSocket;
    int clientID = -1;
    boolean running = true;

    ClientServiceThread(Socket s, int i)
    {
        clientSocket = s;
        clientID = i;
    }

    public void run()
    {
        System.out.println("Accepted Client : ID - " + clientID + " : Address - " + clientSocket.getInetAddress().getHostName());
        try
        {
            BufferedReader   in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            PrintWriter   out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
            while (running)
            {
                String clientCommand = in.readLine();
                System.out.println("Client Says :" + clientCommand);
                if (clientCommand.equalsIgnoreCase("quit"))
                {
                    running = false;
                    System.out.print("Stopping client thread for client : " + clientID);
                }
                else
                {
                    out.println(clientCommand);
                    out.flush();
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

如您所见,我确实读取了客户端发送的内容并将其输出到控制台,同时检查客户端是否发送了quit或not命令

  • 我想知道的是我会怎么做 这样客户端就只能进行连接 如果用户和密码为 发送

  • 还是我应该一直收到 初始连接和从那里开始 接收身份验证 信息,如果客户没有 3秒钟后就送来了 断开连接

  • 正确的接收方式是什么 新的连接和身份验证 用户对它感兴趣吗


共 (1) 个答案

  1. # 1 楼答案

    你的想法是对的,但你需要把它更多地看作一台状态机

    客户端连接后,需要进入登录状态,您希望它在该状态下发送身份验证信息。如果不正确或超时,请断开连接

    如果正确,请转到命令处理状态。如果收到quit命令,请移动到清理状态,或者干脆断开连接

    这里有一个大致的大纲:

    String line;
    while ((line = in.readLine()) != null) {
        switch (state) {
            case login:
                processLogin(line);
                break;
            case command:
                processCommand(line);
                break;
        }
    }
    

    您的processLogin函数可能如下所示:

    void processLogin(String line) {
        if (user == null) {
            user = line;
        }
        else if (password == null) {
            password = line;
        }
        else {
            if (validUser(user, password)) {
                state = command;
            }
            else {
                socket.close();
            }
        }
    }
    

    以及您的processCommand函数:

    void processCommand(String line) {
        if (line.equals(...)) {
            // Process each command like this
        }
        else if (line.equals("quit")) {
            socket.close();
        }
        else {
            System.err.println("Unrecognized command: " + line);
        }
    }
    

    state实例变量可能是枚举。您可以看到,使用此模型将提供一些非常简单的可扩展性。例如,命令可以使您进入不同的状态以处理特定的子命令