【问题标题】:How to find which user is using a port in Java如何在 Java 中查找正在使用端口的用户
【发布时间】:2013-07-30 04:37:46
【问题描述】:

我想将端口号映射到用户(正在运行绑定到端口的进程的 Linux 用户)。 我怎么能在java中做到这一点?

我知道我可以进入 shell 并运行 bash 命令,将端口映射到 PID,然后将 PID 映射到用户,但如果可以的话,我想将其保留在 java 中。

更一般的问题是:我有一个从 localhost 接收请求的 webapp 应用程序,我想知道哪个本地用户执行了 HttpServletRequest,因此我可以为其附加适当的权限。

背景:

我正在为所有远程连接使用 Spring Security。但是,我有一小部分应用程序(与 webapp 分离)在本地与应用程序服务器一起运行,并且该应用程序使用 linux 用户机制进行身份验证。因此,出于这个原因,我绕过了 localhost 的服务器身份验证规则(假设允许所有 localhost 访问)。问题在于授权 - 我需要识别运行 localhost 请求的用户。知道如何实现这一目标吗?

【问题讨论】:

    标签: java web-applications localhost port


    【解决方案1】:

    这是依赖于 Linux 的代码,但不难移植到 Windows。

    这不是 Servlet 代码,但在这种情况下也可以工作:

    假设我有一个 ServerSocket 正在等待 accept() 调用。当它接收到客户端请求时,它会在另一个端口上创建一个 Socket 来处理该“远程”请求。

    ServerSocket ss = new ServerSocket(2000);
    System.out.println("Listening on local port : " + ss.getLocalPort());
    
    while(...)
    {
     Socket s = ss.accept();
     System.out.println("accepted client request, opened local port : " + s.getPort());
     ...
    }
    

    因此,您需要将 s.getPort() 的输出从 sn-p 提供给以下程序的 main() 方法。

    public class FindUserByPort
    {
      public static void main(String[] args) throws Exception
      {
        String cmd = "netstat -anp | grep ";
        int port = Integer.valueOf(args[0]);
        cmd = cmd + port ;
    
        Process pr = Runtime.getRuntime().exec(cmd);
        InputStream is = pr.getInputStream();
    
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = null;
        List<Integer> pIDs = new ArrayList<Integer>();
    
        while ((line = br.readLine()) != null)
        {
          if (line.contains("127.0.0.1:" + port))
          {
            String pidPname = line.substring(line.indexOf("ESTABLISHED") + "ESTABLISHED".length());
            pidPname = pidPname.trim();
            String pid = pidPname.split("/")[0];
            pIDs.add(Integer.valueOf(pid));
          }
        }
        if (pIDs.size() > 0)
        {
          for (int pid : pIDs)
          {
            String command = "top -n1 -b -p " + pid ;
            Process p = Runtime.getRuntime().exec(command);
            InputStream _is = p.getInputStream();
    
            BufferedReader _br = new BufferedReader(new InputStreamReader(_is));
            String _line = null;
            while ((_line = _br.readLine()) != null)
            {
              _line = _line.trim();
              if(_line.startsWith(String.valueOf(pid)))
              {
                String[] values = _line.split(" ");
                System.out.println("pid : " + pid + ", user : " + values[1]);
              }
            }
            _is.close();
            _br.close();
          }
        }
        is.close();
        br.close();
      }
    }
    

    【讨论】:

    • 您能否提供更多有关您的上述代码的信息,它看起来很有趣,但超出了我的想象。请解释一下上面的代码
    猜你喜欢
    • 1970-01-01
    • 2012-12-23
    • 2012-05-01
    • 2019-09-16
    • 2011-06-24
    • 2018-12-08
    • 1970-01-01
    • 2015-01-28
    相关资源
    最近更新 更多