classnotfoundexception

mpgws1up  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(367)

我是applet/servlet开发方面的新手,面临一个尚未找到解决方法的问题。
环境如下:
操作系统:solaris 5.10
同于:iplanet6 sp8
jre:j2re1.4.2\ 04
jsse.jar包含在servlet类路径中。
我的servlet必须通过ldap连接到ldap服务器;我的ldap类如下所示

public class MyLdapClass
{
    private static final String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";

    public MyLdapClass(String _strHost, int _iPort, String _strLogin, String _strPassword, String _strBaseDN)
    {
        ...

        environ.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
        environ.put(Context.SECURITY_PRINCIPAL, _strLogin);
        environ.put(Context.SECURITY_CREDENTIALS, _strPassword);

        String strUrl = "ldap://" + _strHost + ":" + _iPort;

        XTrustProvider.install();
        environ.put(Context.SECURITY_PROTOCOL, "ssl");

        environ.put(Context.PROVIDER_URL, strUrl);
        environ.put(Context.SECURITY_AUTHENTICATION, "simple");

        environ.put("com.sun.jndi.ldap.read.timeout", "5000");
    }

    private void connect() throws NamingException
    {
        try
        {
            this.context = new InitialLdapContext(this.environ, null);
        }
        catch (NamingException ne)
        {
            throw ne;
        }
    }

    private void _disconnect() throws NamingException
    {
        try
        {
            this.context.close();
        }
        catch (NamingException ne)
        {
            throw ne;
        }
    }
}

我的servlet(与我的问题相关的代码部分)如下所示:

public class MyServlet extends HttpServlet
{
  MyLdapClass ldap;

  public void init(ServletConfig paramServletConfig) throws ServletException
  {
    ldap = new MyLdapClass("ipaddress", port, "login", "pwd", "baseDN");
  }

  public void doPost(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws ServletException
  {
    try
    {
        ldap.connect();
    }
    catch (NamingException ne)
    {
      throw ne;
    }

    ...
  }
}

当我将小程序的请求发布到servlet时,会引发如下异常:

javax.naming.CommunicationException: _ip:port_ [**Root exception is java.lang.ClassNotFoundException: javax/net/ssl/SSLSocketFactory**]
   at com.sun.jndi.ldap.Connection.<init>(Unknown Source)
   at com.sun.jndi.ldap.LdapClient.<init>(Unknown Source)
   at com.sun.jndi.ldap.LdapClient.getInstance(Unknown Source)
   at com.sun.jndi.ldap.LdapCtx.connect(Unknown Source)
   at com.sun.jndi.ldap.LdapCtx.<init>(Unknown Source)
   at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(Unknown Source)
   at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(Unknown Source)
   at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(Unknown Source)
   at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(Unknown Source)
   at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
   at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
   at javax.naming.InitialContext.init(Unknown Source)
   at javax.naming.ldap.InitialLdapContext.<init>(Unknown Source)
   at MyLdapClass.connect(MyLdapClass.java:_row number_)
   at MyServlet.doPost(MyServlet.java:_row number_)
   at javax.servlet.http.HttpServlet.doPost(HttpServlet.java:_row number_)
   at com.iplanet.server.http.servlet.NSServletRunner.invokeServletService(NSServletRunner.java:937)
   at com.iplanet.server.http.servlet.NSServletRunner.Service(NSServletRunner.java:494)
Caused by: java.lang.ClassNotFoundException: javax/net/ssl/SSLSocketFactory
   at java.lang.Class.forName0(Native Method)
   at java.lang.Class.forName(Unknown Source)
   at com.sun.jndi.ldap.VersionHelper12.loadClass(Unknown Source)
   at com.sun.jndi.ldap.Connection.createSocket(Unknown Source)
   ... 20 more
Internal error: servlet service function had thrown ServletException (uri=/servlet/MyServlet): javax.servlet.ServletException: NamingException:__ip:port__, stack: javax.servlet.ServletException: NamingException:_ip:port_

如果我从myservlet服务方法调用ldap.connect(),也会发生同样的情况;相反,如果我从myservlet类的init方法调用ldap.connect(),一切正常,但我有一个要求,即必须按需打开连接,并在处理请求时关闭连接。
我错过什么了吗?我该怎么修?提前谢谢大家
作为补充,xtrustprovider代码如下:

package jas.frontend;

import java.security.AccessController;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.PrivilegedAction;
import java.security.Provider;
import java.security.Security;
import java.security.cert.X509Certificate;

import javax.net.ssl.ManagerFactoryParameters;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactorySpi;
import javax.net.ssl.X509TrustManager;

public final class XTrustProvider extends Provider {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private final static String NAME = "XTrustJSSE";
    private final static String INFO = "XTrust JSSE Provider (implements trust factory with truststore validation disabled)";
    private final static double VERSION = 1.0D;

    public XTrustProvider() {
        super(NAME, VERSION, INFO);

        AccessController.doPrivileged(new PrivilegedAction() {

            public Object run() {
                put("TrustManagerFactory." + TrustManagerFactoryImpl.getAlgorithm(),
                        TrustManagerFactoryImpl.class.getName());
                return null;
            }
        });
    }

    public static void install() {
        if (Security.getProvider(NAME) == null) {
            Security.insertProviderAt(new XTrustProvider(), 2);
            Security.setProperty("ssl.TrustManagerFactory.algorithm", TrustManagerFactoryImpl.getAlgorithm());
        }
    }

    public final static class TrustManagerFactoryImpl extends TrustManagerFactorySpi {

        public TrustManagerFactoryImpl() {}

        public static String getAlgorithm() {
            return "XTrust509";
        }

        protected void engineInit(KeyStore keystore) throws KeyStoreException {}

        protected void engineInit(ManagerFactoryParameters mgrparams) throws InvalidAlgorithmParameterException {
            throw new InvalidAlgorithmParameterException(XTrustProvider.NAME + " does not use ManagerFactoryParameters");
        }

        protected TrustManager[] engineGetTrustManagers() {
            return new TrustManager[] { new X509TrustManager() {

                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkClientTrusted(X509Certificate[] certs, String authType) {}

                public void checkServerTrusted(X509Certificate[] certs, String authType) {}
            } };
        }
    }
}
chhkpiq4

chhkpiq41#

最后我把它修好了(多亏了埃曼纽尔·加洛,虽然没有签约,但无论如何他是个很棒的家伙)!!!
问题是我加了
/opt/wbp/j2re1.4.2_04/lib/rt.jar:/opt/wbp/j2re1.4.2_04/lib/jce.jar:/opt/wbp/j2re1.4.2_04/lib/jsse.jar
在类路径字段中(见下图)

为了工作,必须使用bootclasspath属性将它们添加到选项字段中,并使用-x选项。
在其他rwords中,在<iplanet\u install\u path>/servers/https-<virtual\u server\u name>/config/jvm12.conf中,这是错误的配置

option=-xbootclasspath:/lib/tools.jar:/jre/lib/rt.jar

选项=-xrs
option=-xnoagent
jvm.classpath=/opt/iplanet6/servers/plugins/servlets/examples/legacy/beans.10/sdkbeans10.jar:/opt/wbp/j2re1.4.2_04/lib/rt.jar:/opt/wbp/j2re1.4.2_04/lib/jce.jar:/opt/wbp/j2re1.4.2_04/lib/jsse.jar
当替换为下面的一个时,效果非常好
选项=-xrs
option=-xnoagent
classpath=/opt/iplanet6/servers/plugins/servlets/examples/legacy/beans.10/sdkbeans10.jar
option=-xbootclasspath:/opt/wbp/j2re1.4.2_04/lib/rt.jar:/opt/wbp/j2re1.4.2_04/lib/jce.jar:/opt/wbp/j2re1.4.2_04/lib/jsse.jar
注1:每个key=value对必须写在一行上;这里是“-”和xbootclasspath之间的换行符。。。。取决于页面格式

相关问题