【问题标题】:Debugging a custom tomcat realm调试自定义 tomcat 领域
【发布时间】:2012-11-01 17:23:26
【问题描述】:

我正在使用 maven 和 eclipse juno 为 Tomcat 7 开发一个自定义领域。

它看起来很像 Implement a Tomcat Realm with LDAP authentication and JDBC authorization 中描述的解决方案,带有:

  • 改编的 hasRole 方法,因为界面略有变化(附加 Wrapper 作为第一个参数);
  • 一些内部特定的东西。

当我尝试使用 eclipse 调试这个领域时,我不断遇到以下异常:

The type org.apache.tomcat.util.buf.ByteChunk cannot be resolved. It is indirectly referenced from required .class files

据我所知,这个类在 $TOMCAT_ROOT/lib/tomcat-coyote.jar 中:

unzip -t /opt/apache-tomcat-7.0.32/lib/tomcat-coyote.jar | grep ByteChunk
    testing: org/apache/tomcat/util/buf/ByteChunk$ByteInputChannel.class   OK
    testing: org/apache/tomcat/util/buf/ByteChunk$ByteOutputChannel.class   OK
    testing: org/apache/tomcat/util/buf/ByteChunk.class   OK

项目很简单,一个maven构建的jar就是这样配置的:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>senrealm</artifactId>
    <groupId>fr.senat</groupId>
    <packaging>jar</packaging>
    <version>0.1.0</version>
    <name>Realm d'authentification</name>
    <url></url>

    <properties>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
        <tomcat.version>7.0.32</tomcat.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-catalina</artifactId>
            <version>${tomcat.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-coyote</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
              <resource>
                      <directory>src/main/resources</directory>
                      <filtering>true</filtering>
                      <excludes>
                              <exclude>ldap.jks</exclude>
                      </excludes>
              </resource>
              <resource>
                      <directory>src/main/resources</directory>
                      <filtering>false</filtering>
                      <includes>
                              <include>ldap.jks</include>
                      </includes>
              </resource>
      </resources>
   </build>
</project>

生成的 jar 文件放在 $TOMCAT_ROOT/lib 中。

有什么想法吗?

补充:问题只发生在 ldaps 连接上。使用非 SSL ldap 连接,一切正常。

即使我在 m 中手动设置参数时也是一个重载的“open”方法,代码如下:

@Override
protected DirContext open() throws NamingException {

    URL ts = getClass().getResource("/ldap.jks");
    System.setProperty("javax.net.ssl.trustStore", ts.getFile());

    Properties ldapProperties;
    ldapProperties = new Properties();
ldapProperties.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
    ldapProperties.put(javax.naming.Context.PROVIDER_URL, "ldaps://ldap.senat.fr:636/dc=senat,dc=fr");
    ldapProperties.put("com.sun.jndi.ldap.connect.pool","true");

    ldapProperties.put(javax.naming.Context.SECURITY_AUTHENTICATION, "simple");
    ldapProperties.put(javax.naming.Context.SECURITY_PROTOCOL, "ssl");
    ldapProperties.put(javax.naming.Context.SECURITY_PRINCIPAL, "uid=batchges, ou=comptes, dc=senat, dc=fr");
    ldapProperties.put(javax.naming.Context.SECURITY_CREDENTIALS, "xxxxx");

    DirContext newDirContext = new InitialDirContext(ldapProperties);
    return newDirContext;

}

... InitialDirContext 构造函数中出现奇怪的异常。

更新:如果我将 javax.net.ssl.trustStore 属性设置为本地文件而不是 jar 中包含的文件,它可以工作。

所以:

@Override
protected DirContext open() throws NamingException {
    System.setProperty("javax.net.ssl.trustStore", "/tmp/ldap.jks");
    return super.open();
}

有效。知道为什么吗?

【问题讨论】:

  • 什么时候留下痕迹?
  • Sun ldap lib 深处的某个地方。

标签: java eclipse tomcat jdbcrealm


【解决方案1】:

好的,所以,错误是 sun ldap 库无法从 jar 加载 jks 文件。 解决方法是将文件从汽车提取到 tomcat 临时目录,然后将此“真实”文件设置为 javax.net.ssl.trustStore 属性。

        String tmpdir = System.getenv("CATALINA_TMPDIR");
        if(tmpdir == null)
            tmpdir = System.getenv("CATALINA_HOME") + "/temp";
        final String tmpJKSfile = tmpdir + "/ldap.jks";
        URL ts = getClass().getResource("/ldap.jks");
        try {
            InputStream in = new BufferedInputStream(ts.openStream());
            OutputStream out = new BufferedOutputStream(new FileOutputStream(tmpJKSfile));
            IOUtils.copy(in, out);
            out.flush();
            out.close();
            in.close();
        } catch (IOException eio) {
            /* your logging */
        }


        System.setProperty(TrustStoreProperty, tmpJKSfile);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多