将Jetty 6.1迁移到9.3有多个问题 [英] Migrating Jetty 6.1 to 9.3 has multiple issue

查看:255
本文介绍了将Jetty 6.1迁移到9.3有多个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


您好,在迁移具有内置码头服务器6.1的旧版应用程序
时,几乎不需要什么建议。

Hi All need few suggestion while migrating our legacy application which has in-built jetty server 6.1.

我是试图迁移到Jetty 9.3,是的,经过一段漫长的时间,并且在
决定迁移到JAVA 8之后,是的。在更改几个文件时,我遇到了
用jetty 6.1编写的代码。

I am trying to migrate to Jetty 9.3, yes after long gap and after decided to move to JAVA 8. While changing few of the files I encounter piece of code written in jetty 6.1.

代码:



@Override
    public void initialize(final ServiceConfiguration genericConfig, final Controller controller, final int serviceId,
        final ServiceLock lock) throws Exception {
        if (genericConfig instanceof JettyServerConfiguration) {
            configuration = (JettyServerConfiguration) genericConfig;
        } else {
            configuration = XmlConfigurable.createInstance(JettyServerConfiguration.class,
                    genericConfig.getXmlConfigElement());
        }

        server = new Server();
        log.info("jetty version = " + Server.getVersion()); //frozen

        maxWaitForSlave = getConfiguration().getMaxWaitForSlave();

        final boolean debug = getConfiguration().getMortBayDebug();
        log.info("mortbay debug = '" + debug + "'"); //frozen
        org.mortbay.log.Log.getLog().setDebugEnabled(debug);

        // Configure http
        final boolean httpEnabled = getConfiguration().getHttpEnabled();

        if (httpEnabled) {
            // Setup http connector as nio or socket.
            final boolean nio = getConfiguration().getNioEnabled();
            Connector connector;

            if (nio) {
                connector = new SelectChannelConnector();
            } else {
                connector = new SocketConnector();
            }

            final int mainPort = getConfiguration().getHttpPort();

            log.info("adding default connector on port '" + mainPort + "'"); //frozen
            connector.setPort(mainPort);

            server.addConnector(connector);
        }

        // Configure SSL
        final boolean sslEnabled = getConfiguration().getSslEnabled();

        if (sslEnabled) {
            final int sslPort = getConfiguration().getSslPort();
            final String sslKeyStore = getConfiguration().getSslKeyStore();
            final String sslPassword = getConfiguration().getSslPassword();
            final String sslKeyPassword = getConfiguration().getSslKeyPassword();
            final String sslTrustPassword = getConfiguration().getSslTrustPassword();

            //final boolean nio = configuration.getBooleanValue("NioEnabled", false); //frozen
            //if(nio) {
            //sslConnector = new SslSelectChannelConnector();  available in jetty 7
            //} else {
            final SslSocketConnector sslConnector = new SslSocketConnector();
            sslConnector.setKeystore(sslKeyStore);
            sslConnector.setTruststore(sslKeyStore);
            sslConnector.setPassword(sslPassword);
            sslConnector.setKeyPassword(sslKeyPassword);
            sslConnector.setTrustPassword(sslTrustPassword);
            sslConnector.setPort(sslPort);
            log.info("adding ssl connector on port '" + sslPort + "'"); //frozen
            server.addConnector(sslConnector);

            //}
        }

        // Check we had 1 connector else the server is useless
        if (server.getConnectors().length == 0) {
            throw new FileNotFoundException("No connectors registered.  Please see HttpEnable or SslEnable XML tags."); //frozen
        }

        // Configure the handlers
        final HandlerCollection handlers = new HandlerCollection();

        for (final WebAppContext webAppContext : getConfiguration().getWebAppContexts()) {
            log.info("Adding WebAppContext " + webAppContext.getWar() + " at " + webAppContext.getContextPath()); //frozen
            handlers.addHandler(webAppContext);
        }

        // See http://docs.codehaus.org/display/JETTY/Logging+Requests
        final boolean accessLogEnabled = getConfiguration().getLogEnabled();

        if (accessLogEnabled) {
            final RequestLogHandler requestLogHandler = new RequestLogHandler();
            final File logDir = ServiceUtilities.getLogDirectory();

            if (!logDir.exists()) {
                logDir.mkdirs();
            }

            final File logFile = new File(getConfiguration().getLogFormat());

            if (!logFile.getParentFile().exists()) {
                logFile.getParentFile().mkdirs();
            }

            final NCSARequestLog requestLog = new NCSARequestLog(getConfiguration().getLogFormat());
            requestLog.setRetainDays(getConfiguration().getLogRetain());
            requestLog.setAppend(getConfiguration().getLogAppend());
            requestLog.setExtended(getConfiguration().getLogExtended());
            requestLog.setLogTimeZone(getConfiguration().getLogTz());
            requestLog.setLogLatency(getConfiguration().getLogLatency());
            requestLogHandler.setRequestLog(requestLog);
            handlers.addHandler(requestLogHandler);
        }

        handlers.addHandler(new DefaultHandler());

        server.setHandler(handlers);

        server.setUserRealms(new UserRealm[] { new OSMUserRealm() });

        JettyServerInfo.install(server);

        super.initialize(configuration, controller, serviceId, lock);
    }




在给出以下建议之后,我重新像这样写:

After the suggestion given below , I have re-written it like:



@Override
public void initialize(final ServiceConfiguration genericConfig, final Controller controller, final int serviceId,
    final ServiceLock lock) throws Exception {
    if (genericConfig instanceof JettyServerConfiguration) {
        configuration = (JettyServerConfiguration) genericConfig;
    } else {
        configuration = XmlConfigurable.createInstance(JettyServerConfiguration.class,
                genericConfig.getXmlConfigElement());
    }

    server = new Server();
    log.info("jetty version = " + Server.getVersion()); //frozen

    maxWaitForSlave = getConfiguration().getMaxWaitForSlave();

    final boolean debug = getConfiguration().getMortBayDebug();
    log.info("mortbay debug = '" + debug + "'"); //frozen
    //org.eclipse.jetty.util.log.Log.getLog().setDebugEnabled(debug);


        //Re-writing code for Jetty 9.3
        final int mainPort = getConfiguration().getHttpPort();//8580
        final int sslPort = getConfiguration().getSslPort(); //8581
        final String sslKeyStore = getConfiguration().getSslKeyStore();
        final String sslPassword = getConfiguration().getSslPassword();
        final String sslKeyPassword = getConfiguration().getSslKeyPassword();
        final String sslTrustPassword = getConfiguration().getSslTrustPassword();
        //Added for  Jetty 9.3
        final KeyStore trustKeyStore=KeyStore.getInstance(getConfiguration().getSslKeyStore());

        ClassLoader cl = JettyServer.class.getClassLoader();// Get the class loader for my current class which is JettyServer
        String keystoreResource = "ssl/keystore";
         URL f = cl.getResource(keystoreResource);

         if (f == null)
         {
             throw new RuntimeException("Unable to find " + keystoreResource);
         }

         // Setup HTTP Connector
         HttpConfiguration httpConf = new HttpConfiguration();
         httpConf.setSecurePort(mainPort);
         httpConf.setSecureScheme("https");

         // Establish the HTTP ServerConnector
         ServerConnector httpConnector = new ServerConnector(server,
                 new HttpConnectionFactory(httpConf));
         httpConnector.setPort(mainPort);
         server.addConnector(httpConnector);

         // Setup SSL
        SslContextFactory theSSLFactory = new SslContextFactory();

        theSSLFactory.setKeyStorePath(f.toExternalForm());  //replaced for--> theSSLFactory.setKeyStorePath(sslKeyStore);
        theSSLFactory.setKeyManagerPassword(sslPassword);  // or this one ? seems hardcoded --> sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
        theSSLFactory.setKeyStorePassword(sslKeyPassword); // or this one ? seems hardcoded --> sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
        theSSLFactory.setTrustStore(trustKeyStore);
        theSSLFactory.setTrustStorePassword(sslTrustPassword);

        // Setup HTTPS Configuration
        HttpConfiguration httpsConf = new HttpConfiguration(httpConf);
        httpsConf.addCustomizer(new SecureRequestCustomizer()); // adds ssl info to request object

     // Establish the HTTPS ServerConnector
        ServerConnector httpsConnector = new ServerConnector(server,
                new SslConnectionFactory(theSSLFactory,"http/1.1"),
                new HttpConnectionFactory(httpsConf));
        httpsConnector.setPort(sslPort);

        log.info("adding ssl connector on port '" + sslPort + "'"); //frozen
        server.addConnector(httpsConnector);



    // Check we had 1 connector else the server is useless
    if (server.getConnectors().length == 0) {
        throw new FileNotFoundException("No connectors registered.  Please see HttpEnable or SslEnable XML tags."); //frozen
    }

    // Configure the handlers
    final HandlerCollection handlers = new HandlerCollection();

    for (final WebAppContext webAppContext : getConfiguration().getWebAppContexts()) {
        log.info("Adding WebAppContext " + webAppContext.getWar() + " at " + webAppContext.getContextPath()); //frozen
        handlers.addHandler(webAppContext);
    }

    // See http://docs.codehaus.org/display/JETTY/Logging+Requests
    final boolean accessLogEnabled = getConfiguration().getLogEnabled();

    if (accessLogEnabled) {
        final RequestLogHandler requestLogHandler = new RequestLogHandler();
        final File logDir = ServiceUtilities.getLogDirectory();

        if (!logDir.exists()) {
            logDir.mkdirs();
        }

        final File logFile = new File(getConfiguration().getLogFormat());

        if (!logFile.getParentFile().exists()) {
            logFile.getParentFile().mkdirs();
        }

        final NCSARequestLog requestLog = new NCSARequestLog(getConfiguration().getLogFormat());
        requestLog.setRetainDays(getConfiguration().getLogRetain());
        requestLog.setAppend(getConfiguration().getLogAppend());
        requestLog.setExtended(getConfiguration().getLogExtended());
        requestLog.setLogTimeZone(getConfiguration().getLogTz());
        requestLog.setLogLatency(getConfiguration().getLogLatency());
        requestLogHandler.setRequestLog(requestLog);
        handlers.addHandler(requestLogHandler);
    }

    handlers.addHandler(new DefaultHandler());

    server.setHandler(handlers);

    server.setUserRealms(new UserRealm[] { new OSMUserRealm() });

    JettyServerInfo.install(server);

    super.initialize(configuration, controller, serviceId, lock);
}




我仍然不确定该怎么做

Still I am not sure what to do with this line.

server.setUserRealms(new UserRealm [] {new OSMUserRealm()));

server.setUserRealms(new UserRealm[] { new OSMUserRealm() });


我在Jetty 9.3中找不到UserRealm的任何替代方法,有任何
建议如何重新编写此行代码?

I didn't find any alternative method in jetty 9.3 for UserRealm, any suggestion how can I re-write this line of code?


推荐答案


提醒:Jetty版本(自1995年起)是 < servlet_support>。< major_version>。< minor_version>

恭喜,您基本上已经在Jetty中仅跳过了14个主要版本。 :-)

Congrats, you've essentially just skipped 14 major versions ahead in Jetty. :-)

难怪它看起来如此艰巨。

No wonder it seems so daunting.


决定搬家到JAVA 8

decided to move to JAVA 8

知道 2019年1月是Java 8公共更新的最后一天
那时,对于非付费的oracle客户来说,它实际上已经寿终正寝了。

Know that January 2019 is the last day of Java 8 public updates. It's practically end of life at that point for non-paying oracle customers.

注意:Java 9已经被弃用/不受支持。

Note: Java 9 is already deprecated / unsupported.

由于您提到使用SSL / TLS,因此必须保持JVM处于最新状态,并且不使用过期的JVM (浏览器要求和TLS要求)。

Since you mention using SSL/TLS, its is mandatory to keep your JVM up to date and not use an expired JVM (Browser requirements and TLS requirements).

考虑在今年(2018年)之前升级(再次)到Java 11。

Consider upgrading (again) to Java 11 before this year (2018) is over.

Java的更新周期正在加速,不要指望Java版本会坚持多年了。主要版本预计不会持续超过6个月(当前)。

The update cycle for java is accelerating, don't expect Java versions to stick around for years anymore. Major versions are not expected to last more then 6 months (currently).


log.info( mortbay debug =' + debug + '); //冻结
org.eclipse.jetty.util.log.Log.getLog()。setDebugEnabled(debug);

log.info("mortbay debug = '" + debug + "'"); //frozen org.eclipse.jetty.util.log.Log.getLog().setDebugEnabled(debug);

日志无法像这样工作。

这取决于所选择的实现(现在有SLf4j,java.util.logging和System.err)。使用 Log.getLog()可以获取根日志的外观,而在外观上使用 setDebugEnabled()只是设置

It depends on the implementation chosen (there's SLf4j, java.util.logging, and System.err now). Using Log.getLog() gets you the root logging facade, and using setDebugEnabled() on the facade is just setting debug on the facade, not the actual loggers.

使用适当的日志记录实现并命名记录器。

Use a proper logging implementation and named loggers.


if(nio){
连接器=新的SelectChannelConnector(服务器);
} else {

connector = new SocketConnector(server);
}

if (nio) { connector = new SelectChannelConnector(server); } else {
connector = new SocketConnector(server); }

不再有阻塞连接器了,它的100%基于NIO的(过去5个主要版本)

There's no blocking connectors anymore, its 100% NIO based (for the past 5 major versions)

欢迎使用现代Web协议。

Welcome to the world of modern web protocols.

连接器仅服务于一种协议的时代已经过去,现在它只支持一种协议

Gone are the days of a connector serving just 1 protocol, now its all negotiated during the actual connection.

例如:Connect,TLS,ALPN,HTTP / 2,带有TLS的HTTP / 1.1,不带有TLS的HTTP / 1.1等(以及单个连接的TLS协商中包含的所有信息还用于确定您与哪个Web应用程序通信,使用哪个主机以及使用哪些证书等)

Eg: Connect, TLS, ALPN, HTTP/2, HTTP/1.1 with TLS, HTTP/1.1 without TLS, etc. (And all of the information carried in the TLS negotiation of a single connection is also used to determine which webapp you talk to, using what host, and what certificates, etc)

A ServerConnector的作用是仅侦听端口,您分配给ServerConnector的ConnectionFactories决定如何处理该连接。

A ServerConnector's role is to only listen to a port, the ConnectionFactories you assign to the ServerConnector determine what to do with that connection.

为此阅读各种示例代码。

Read the various example code for this.

请参阅:

  • eclipse/jetty.project/examples/embedded
  • jetty-project/embedded-jetty-cookbook
  • eclipse/jetty.project/examples/embedded - LikeJettyXml.java
  • eclipse/jetty.project/examples/embedded - Http2Server.java

一个简化的示例:

    Server server = new Server();
    int httpsPort = 8443;

    // Find Keystore
    ClassLoader cl = ServerConnectorHttps.class.getClassLoader();
    String keystoreResource = "ssl/keystore";
    URL f = cl.getResource(keystoreResource);
    if (f == null)
    {
        throw new RuntimeException("Unable to find " + keystoreResource);
    }

    // Setup SSL
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(f.toExternalForm());
    sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
    sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");

    // Setup HTTPS Configuration
    HttpConfiguration httpsConf = new HttpConfiguration();
    httpsConf.setSecurePort(httpsPort);
    httpsConf.setSecureScheme("https");
    httpsConf.addCustomizer(new SecureRequestCustomizer()); // adds ssl info to request object

    // Establish the ServerConnector
    ServerConnector httpsConnector = new ServerConnector(server,
            new SslConnectionFactory(sslContextFactory,"http/1.1"),
            new HttpConnectionFactory(httpsConf));
    httpsConnector.setPort(httpsPort);

    server.addConnector(httpsConnector);

    // Add a Handler for requests
    server.setHandler(new HelloHandler("Hello Secure World"));

    server.start();
    server.join();
}




server.setUserRealms(new UserRealm [] {新的OSMUserRealm()});
我没有在Jetty 9.3中找到UserRealm的任何替代方法,有任何建议我该如何重新编写此行代码?

server.setUserRealms(new UserRealm[] { new OSMUserRealm() }); I didn't find any alternative method in jetty 9.3 for UserRealm, any suggestion how can I re-write this line of code?

安全性由LoginService处理,LoginService可以容纳一个领域(如果LoginService支持这样的概念)。

Security is handled by the LoginService, a LoginService could hold a realm (if that LoginService supports such a concept).

LoginService可以作为Bean属于服务器,但这仅用于控制LoginService的生命周期。

A LoginService can belong as a Bean on the server, but that's only for controlling the LifeCycle of the LoginService.

具有安全上下文设置以使用特定领域的WebAppContext将在服务器上找到所有LoginService Bean,并且使用与指定的领域匹配的那个。

A WebAppContext with a security context setup to use a specific realm will find all LoginService beans on the server and use the one that matches the Realm specified.

这篇关于将Jetty 6.1迁移到9.3有多个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆