【问题标题】:Can we Reject/Accept specific endpoint on Jetty connector?我们可以拒绝/接受 Jetty 连接器上的特定端点吗?
【发布时间】:2021-03-14 21:58:13
【问题描述】:

我们在端口 7777 上有一个连接器,我们想拒绝该连接器上针对 /serverStatus 的任何请求?

同时,我只想在端口 80 上的不同连接器上接受 /serverStatus 请求。

【问题讨论】:

    标签: jetty embedded-jetty jetty-9


    【解决方案1】:

    命名您的服务器连接器。

    7777 端口上的 ServerConnector 名为“admin” 80 端口上的 ServerConnector 被命名为“正常”

    然后将/serverStatus 的webapp 设置为具有@admin 的虚拟主机,并将您的根webapp 设置在@normal 的虚拟主机上。

    例如,请参阅Embedded-Jetty Cookbook: ConnectorSpecificContexts.java

    package org.eclipse.jetty.cookbook;
    
    import org.eclipse.jetty.cookbook.handlers.HelloHandler;
    import org.eclipse.jetty.server.Server;
    import org.eclipse.jetty.server.ServerConnector;
    import org.eclipse.jetty.server.handler.ContextHandler;
    import org.eclipse.jetty.server.handler.ContextHandlerCollection;
    
    public class ConnectorSpecificContexts
    {
        public static void main(String[] args) throws Exception
        {
            Server server = new Server();
    
            ServerConnector connectorA = new ServerConnector(server);
            connectorA.setPort(8080);
            connectorA.setName("connA");
            ServerConnector connectorB = new ServerConnector(server);
            connectorB.setPort(9090);
            connectorB.setName("connB");
    
            server.addConnector(connectorA);
            server.addConnector(connectorB);
    
            // Collection of Contexts
            ContextHandlerCollection contexts = new ContextHandlerCollection();
            server.setHandler(contexts);
    
            // Hello Handler (connection A)
            ContextHandler ctxHelloA = new ContextHandler();
            ctxHelloA.setContextPath("/");
            ctxHelloA.setHandler(new HelloHandler("Hello Connection A"));
            ctxHelloA.setVirtualHosts(new String[] { "@connA" });
            contexts.addHandler(ctxHelloA);
    
            // Hello Handler (connection B)
            ContextHandler ctxHelloB = new ContextHandler();
            ctxHelloB.setContextPath("/");
            ctxHelloB.setHandler(new HelloHandler("Greetings from Connection B"));
            ctxHelloB.setVirtualHosts(new String[] { "@connB" });
            contexts.addHandler(ctxHelloB);
    
            server.start();
            server.join();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-08
      • 1970-01-01
      • 2021-10-16
      • 2017-10-07
      • 2019-01-31
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多