简体   繁体   English

如何确定传入连接来自本地计算机

[英]How to determine an incoming connection is from local machine

I have a SocketServer accepting incoming connections. 我有一个SocketServer接受传入的连接。 For security reasons I should only allow local connections (connections from the machine on which server is running). 出于安全原因,我应该只允许本地连接(来自运行服务器的计算机的连接)。

How can I determine if an incoming connection is from another machine? 如何确定传入连接是否来自其他计算机? Is the following code safe for this? 以下代码是否安全?

Socket socket = someServerSocket.accept();
String remoteAddress = socket .getInetAddress().getHostAddress();
if (!fromThisMachine(remoteAddress)) {
    // Not from this machine.
}

while fromThisMachine() method is like this: fromThisMachine()方法是这样的:

public boolean fromThisMachine(String remoteAddress) {
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface networkInterface = interfaces.nextElement();
            Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress inetAddress = addresses.nextElement();
                String hostName = inetAddress.getHostName();
                String hostAddr = inetAddress.getHostAddress();
                if (hostName.equals(remoteAddress) || hostAddr.equals(remoteAddress)) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    log("Unauthorized request to server from: " + remoteAddress);
    return false;
}

Thanks, Mohsen 谢谢,Mohsen

InetAddress.getByName( null ) always returns the loopback address. InetAddress.getByName(null)始终返回环回地址。 See the javadoc 请参阅javadoc

    int port = .....
    SocketAddress socketAddress =
        new InetSocketAddress( InetAddress.getByName( null ), port);
    ServerSocket serverSocket = new ServerSocket();
    serverSocket.bind(socketAddress);
    serverSocket.accept();

If you want to limit connections from the localhost, then specify that when you open the ServerSocket. 如果要限制来自localhost的连接,请在打开ServerSocket时指定。 If you only listen on localhost, then you'll only get connections from localhost. 如果您只在localhost上侦听,那么您将只从localhost获取连接。

    int port = .....
    SocketAddress socketAddress = new InetSocketAddress("127.0.0.1", port);
    ServerSocket serverSocket = new ServerSocket();
    serverSocket.bind(socketAddress);
    serverSocket.accept();

Thanks skaffman. 谢谢skaffman。 The following code worked with a little manipulation (hard-coding 127.0.0.1). 以下代码使用了一点操作(硬编码127.0.0.1)。

int port = .....
SocketAddress socketAddress = new InetSocketAddress("127.0.0.1", port);
ServerSocket serverSocket = new ServerSocket();
serverSocket.bind(socketAddress);
serverSocket.accept();

If I read local address from InetAddress.getLocalHost(), other network users on the same subnet are still able to see my server. 如果我从InetAddress.getLocalHost()读取本地地址,则同一子网上的其他网络用户仍然可以看到我的服务器。

Mohsen. 穆赫辛。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以确定发出 api 请求的机器的本地时间? - is it possible to determine local time of the machine from which an api request is made? 如何确定另一台本地计算机是否还活着? - How can I determine if another local machine is alive? 如何从Connection对象中确定DBMS - How to determine the DBMS from Connection object java ssl连接被拒绝,但是它可以在本地计算机上使用 - java ssl connection refused, but it works on local machine 与本地计算机中的MS SQL数据库建立连接 - Make a connection to a MS SQL database in the local machine 从本地机器到 AWS RDS PostgreSQL 数据库的连接速度非常慢 - Painfully slow connection speed from local machine to AWS RDS PostgreSQL database 如何确定EJB调用是来自远程还是本地客户端 - How to determine whether an EJB call is from a remote or local client 如何从数据源连接确定有关WebLogic数据源的信息? - How to determine info about WebLogic datasource from datasource connection? 如何配置 Jenkins,从本地运行代码到从机? - How to configure Jenkins, running code from Local to slave machine? 如何从Web应用程序获取本地计算机中文件的路径 - How to get path of a file in local machine from webapplication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM