EJB应用程序部署到服务器A上的Glassfish 3.1.2.2,servlet部署到服务器B上的Tomcat 7.从独立应用程序查找工作正常,从servlet查找抛出NameNotFoundException。 两种情况下使用的一组库(来自Glassfish模块目录)完全相同。因此,我们假设需要一些额外的Tomcat配置。
部署到Tomcat的servlet的相关代码:
InitialContext ctx;
try {
Properties props = new Properties();
props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
System.setProperty("java.security.auth.login.config", "jaas.config");
ProgrammaticLogin pl = new ProgrammaticLogin();
pl.login("user", "s3cret".toCharArray(), "file", true);
ctx = new InitialContext(props);
HelloBean helloBean = (HelloBean) ctx.lookup("java:global/HelloApp/HelloBeanImpl!app.hello.HelloBean");
} catch (Exception e) {
e.printStackTrace(System.err);
}
异常始终相同,并且getCause中没有有用的信息:
javax.naming.NameNotFoundException: Name [global/HelloApp/HelloBeanImpl!app.hello.HelloBean] is not bound in this Context. Unable to find [global].
at org.apache.naming.NamingContext.lookup(NamingContext.java:819)
at org.apache.naming.NamingContext.lookup(NamingContext.java:167)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:156)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at app.servlet.HelloServlet.doGet(HelloServlet.java:28)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:1)
好的,我们现在能够从Tomcat查找安全的bean。您需要将很多Glassfish特定的jar放到classpath中才能使用。我们在catalina.properties中使用了共享加载器来定义来自Glassfish模块目录的jar的路径(您可能需要排除所有servlet和jsp jar以避免与Tomcat自己的lib冲突)。还要确保不小心将两个security.jar放到classpath中。工作配置如下所示:
InitialContext ctx;
try {
Properties props = new Properties();
props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty(Context.STATE_FACTORIES, "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
props.setProperty(Context.URL_PKG_PREFIXES, "com.sun.enterprise.naming");
System.setProperty("java.security.auth.login.config", "./jaas.config");
ProgrammaticLogin pl = new ProgrammaticLogin();
boolean loginSuccessfull = pl.login("username", "password".toCharArray());
ctx = new InitialContext(props);
HelloBean helloBean = (HelloBean) ctx.lookup("java:global/HelloApp/HelloBeanImpl!app.hello.HelloBean");
resp.getOutputStream().print(helloBean.sayHelloTo("user"));
} catch (Exception e) {
e.printStackTrace(System.err);
}