spring-attic / spring-native

Spring Native is now superseded by Spring Boot 3 official native support

Home Page:https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot instantiate class: com.sun.jndi.ldap.LdapCtxFactory error with spring-native

Priyap1997 opened this issue · comments

Built a native image using the version 0.12.1 and seeing the below error when application tries to connect to a LDAP server.

2022-12-29 13:00:11.600  WARN 58668 --- [nio-8080-exec-2] o.s.b.actuate.ldap.LdapHealthIndicator   : LDAP health check failed

org.springframework.ldap.NoInitialContextException: Cannot instantiate class: com.sun.jndi.ldap.LdapCtxFactory; nested exception is javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.jndi.ldap.LdapCtxFactory [Root exception is java.lang.ClassNotFoundException: com.sun.jndi.ldap.LdapCtxFactory]
        at org.springframework.ldap.support.LdapUtils.convertLdapException(LdapUtils.java:202) ~[na:na]

Spring-boot version: 2.7.1

Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
        <relativePath />
    </parent>
    <groupId>com.example.demo</groupId>
    <artifactId>ldap-native-service</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging>
    <name>ldap-native-service</name>

    <properties>
        <java.version>11</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-native.version>0.12.1</spring-native.version>
        <native-buildtools.version>0.9.19</native-buildtools.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.experimental</groupId>
            <artifactId>spring-native</artifactId>
            <version>${spring-native.version}</version>
        </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-ldap</artifactId>
    </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- dependency to enable the actuator endpoints-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <version>2.7.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.experimental</groupId>
                <artifactId>spring-aot-maven-plugin</artifactId>
                <version>${spring-native.version}</version>
                <executions>
                    <execution>
                        <id>test-generate</id>
                        <goals>
                            <goal>test-generate</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>generate</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </build>

    <profiles>
        <profile>
            <id>native</id>
            <properties>
                <repackage.classifier>exec</repackage.classifier>
            </properties>
            <dependencies>
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.graalvm.buildtools</groupId>
                        <artifactId>native-maven-plugin</artifactId>
                        <version>${native-buildtools.version}</version>
                        <executions>
                            <execution>
                                <id>build-native</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>build</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/release</url>
        </pluginRepository>
    </pluginRepositories>
</project>

Ldapconfig.java

@Configuration
@EnableConfigurationProperties
public class Ldapconfig {

    @Primary
    @Bean("ldapContextSource")
    public LdapContextSource producerLdapContextSource() {
        LdapContextSource contextSource = new LdapContextSource();
            System.out.println("Setting up contextSource for producer openLDAP");
            contextSource.setUrl("ldap://localhost:18889");
            contextSource.setBase("dc=example,dc=com");
            contextSource.setUserDn("uid=admin,ou=system");
            contextSource.setPassword("password");
            contextSource.setPooled(false);
        return contextSource;
    }


    @Bean("ldapTemplate")
    public LdapTemplate consumerLdapTemplate(@Qualifier("ldapContextSource") ContextSource contextSource) {
       System.out.println("Setting up consumer ldap template");
        return new LdapTemplate(contextSource);
    }
}

Hi team,
Could you please provide some update on this issue. We are facing challenge to implement the same in our project.

Spring Native is now superseded by Spring Boot 3 official native support, see the related reference documentation for more details.

As a consequence, I am closing this issue, and recommend trying your use case with latest Spring Boot 3 version. If you still experience the issue reported here, please open an issue directly on the related Spring project (Spring Framework, Data, Security, Boot, Cloud, etc.) with a reproducer.

Thanks for your contribution on the experimental Spring Native project, we hope you will enjoy the official native support introduced by Spring Boot 3.