Search code examples
javahttplocalhostjettyipv6

How to use Jetty IPAccessHandler to whitelist IPv6 addresses?


I want my Jetty server to only handle requests coming from the localhost. I am attempting to do this by:

   ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
   context.setContextPath("/webapi");
   IPAccessHandler ipBlockingHandler = new IPAccessHandler();
   ipBlockingHandler.addWhite("127.0.0.1|/*");
   ipBlockingHandler.setHandler(context);
   final org.eclipse.jetty.server.Server jettyServer = new org.eclipse.jetty.server.Server(8080);
   jettyServer.setHandler(ipBlockingHandler);

Though, this only works for the ipv4 and if I add ipBlockingHandler.addWhite("127.0.0.1|/*"); it does not work.


Solution

  • This is doable by overriding IPAccessHandler and manually parsing the adddresses:

    IPAccessHandler ipBlockingHandler = new IPAccessHandler(){
          @Override
          protected boolean isAddrUriAllowed(String addr,
                                   String path){
              return addr.equals("127.0.0.1") || addr.equals("0:0:0:0:0:0:0:1");
          }
        };