【问题标题】:Tomcat 9 Valve Causing Server Start FailureTomcat 9 Valve 导致服务器启动失败
【发布时间】:2019-01-10 15:46:28
【问题描述】:

我编写了一个自定义 tomcat 阀门来解析 HTTP 标头并使用它们进行身份验证。 Valve 通过扩展 AuthenticatorBase 来工作。我编译它并将它放在 $CATALINA_HOME/lib 中。这是代码:


    public class TomcatLogin extends AuthenticatorBase {

        private String[] roleNames = null;
        private String ivcreds = null;
        private String ivuser = null;
        private String ivgroups = null;
        private GenericPrincipal principal = null;

        // Constructor defers to super class (Authenticator base)
        public TomcatLogin(){
            super();
        }

        protected boolean doAuthenticate(Request request, HttpServletResponse response)
        {
            List<String> groupsList = null;

            System.out.println("Obtaining Headers from Request");
            try {
                ivuser = request.getHeader("iv-user");
                ivcreds = request.getHeader("iv-creds");
                ivgroups = request.getHeader("iv-groups");
            } catch(Exception e) {
                e.printStackTrace();
            }

            // Require all header credentials for proper authentication
            if(ivuser == null || ivcreds == null || ivgroups == null)
                return false;

            // Split ivgroups by comma seporated value
            // Then remove head and tail quotation marks
            roleNames = ivgroups.split(",");
            for(int i=0; i<roleNames.length; i++){
                roleNames[i] = roleNames[i].substring(1, roleNames[i].length()-1 );
                groupsList.add(roleNames[i]);
            }

            principal = new GenericPrincipal(ivuser, ivcreds, groupsList);
            request.setUserPrincipal(principal);

            return true;
        }

        public String getAuthMethod() {
            return "HTTPAuthenticator";
        }

    }

然后我告诉 Tomcat 使用 server.xml 中的阀门。用于扩展 AuthenticatorBase 的 documentation当使用此类时,它所附加的上下文(或层次结构中的父容器)必须具有关联的领域,可用于验证用户和枚举角色以他们被分配了。我以为我已经正确配置了它,但是它抛出错误并且 Tomcat 无法启动。这是server.xml 配置:


    <?xml version="1.0" encoding="UTF-8"?>
    ...
      <Server>
        <Service name="Catalina">
          <Engine name="Catalina" defaultHost="localhost">
            <Realm className="org.apache.catalina.realm.LockOutRealm">
              <Realm className="org.apache.catalina.realm.UserDatabaseRealm" 
                     resourceName="UserDatabase"/>
            </Realm>

            <!-- Here is my valve -->
            <Valve className="package.of.my.custom.valve.TomcatLogin" />

            <Host ... >
               ...
            </Host>
          </Engine>
        </Service>
      </Server>

这是我收到的错误消息:

10-Jan-2019 10:11:03.576 SEVERE [main] org.apache.tomcat.util.digester.Digester.endElement End event threw exception
 java.lang.reflect.InvocationTargetException
... A bunch of useless stacktrace
Caused by: java.lang.IllegalArgumentException: Configuration error:  Must be attached to a Context at org.apache.catalina.authenticator.AuthenticatorBase.setContainer(AuthenticatorBase.java:278)
at org.apache.catalina.core.StandardPipeline.addValve(StandardPipeline.java:335)
at org.apache.catalina.core.ContainerBase.addValve(ContainerBase.java:1133)
    ... 27 more

10-Jan-2019 10:11:03.579 WARNING [main] org.apache.catalina.startup.Catalina.load Catalina.start using conf/server.xml: Error at (107, 82) : Configuration error:  Must be attached to a Context
10-Jan-2019 10:11:03.579 SEVERE [main] org.apache.catalina.startup.Catalina.start Cannot start server. Server instance is not configured.

我认为我的阀门编写正确,所以我的猜测是问题出在配置中。不知道为什么它没有获得附加的上下文。有什么想法吗?

编辑: 我尝试将阀门放在我的应用程序的META-INF/context.xml 中(我必须制作一个,因为一开始没有一个)。这里是:

<?xml version="1.0"?>
<Context>
    <Valve className="package.of.my.custom.valve.TomcatLogin" />
</Context>

服务器随后启动,但未能部署任何应用程序。我遇到了与我最初尝试在此自定义实现中使用的 IBM 阀门类似的错误,它无法从 catalina.jar 找到 AuthenticatorBase 类。这是我得到的SEVERE 错误:

    10-Jan-2019 15:34:06.673 SEVERE [main] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
    org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/sample-DB]]
...
Stacktrace info
...
        at org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:441)
    Caused by: java.lang.ExceptionInInitializerError
        at org.apache.tomcat.util.digester.Digester.startDocument(Digester.java:1102)
    Caused by: java.lang.NullPointerException
        at java.nio.charset.Charset.put(Charset.java:538)
...
Stacktrace
...
    10-Jan-2019 15:34:06.688 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/fmac/deploy/sample.war] has finished in [11] ms
    10-Jan-2019 15:34:06.689 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [/fmac/deploy/sample-auth.war]
    10-Jan-2019 15:34:06.692 SEVERE [main] org.apache.tomcat.util.digester.Digester.startElement Begin event threw error
    java.lang.NoClassDefFoundError: org/apache/catalina/authenticator/AuthenticatorBase
...
Stacktrace
The Error Below is the most confusing one. How can it not find this class?
...
    Caused by: java.lang.ClassNotFoundException: org.apache.catalina.authenticator.AuthenticatorBase
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
...
Stacktrace
...
    10-Jan-2019 15:34:13.823 SEVERE [https-jsse-nio2-8443-exec-3] org.apache.coyote.http11.Http11Processor.service Error processing request
    java.lang.NoClassDefFoundError: Could not initialize class
org.apache.tomcat.util.buf.B2CConverter
        at org.apache.catalina.connector.CoyoteAdapter.convertURI(CoyoteAdapter.java:1072)

【问题讨论】:

    标签: java xml tomcat tomcat-valve


    【解决方案1】:

    将您的 &lt;Valve&gt; 放入您的 &lt;Context&gt; 元素中。这通常应该在您的 Web 应用程序的 META-INF/context.xml 文件中,而不是在 conf/server.xml 中。

    【讨论】:

    • 当我这样做时,服务器会启动,但我得到了一些其他错误,导致我的所有应用程序(包括那些不使用 context.xml 或 web.xml 中的安全约束)无法工作。我在上面发布了更深入的解释
    【解决方案2】:

    所以我终于能够找出问题所在。我们有一个自定义的 tomcat 实现,其中一部分涉及在java 命令中附加类路径。出于某种原因,正在使用的某些数据库驱动程序导致 Tomcat 无法在 CATALINA_HOME/lib 中找到任何库。我在 Docker 容器中运行,这是来自 VM 版本的一些旧的残留物。我们最终只需要扔掉那些 开车出去。

    不太清楚为什么他们会完全覆盖基本的lib/ 目录,但至少这些错误在离开时我实际上能够使用我拥有的预构建身份验证器,而不是微调这个自定义的。

    【讨论】:

      猜你喜欢
      • 2017-03-21
      • 1970-01-01
      • 2015-12-26
      • 2019-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 2018-04-15
      相关资源
      最近更新 更多