码头 JNDI 配置与弹簧查找



我在Jetty服务器上的JNDI配置有问题。我无论如何都不能以 Spring (3.0.5( 之后可以检索 JNDI 变量的方式配置它。

有一些凭据,我不想将其存储在属性中,因此它不会存在于 git 存储库中。我的 Web 应用程序在 Jetty 上运行(最新版本 9.2.3(,因此我想出了将此凭据存储在 Jetty Web 应用程序上下文中的想法。Jetty提供了具有jetty-env.xml的解决方案。所以我在我的 WEB-INF/中创建了 jetty-env.xml 文件,如下所示:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure id="webappCtx" class="org.eclipse.jetty.webapp.WebAppContext">
  <New id="username"  class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg><Ref refid="webappCtx"/></Arg> <!-- scope -->
    <Arg>server/username</Arg> <!-- name -->
    <Arg type="java.lang.String">myUsername</Arg> <!-- value -->
  </New>
  <New id="password"  class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg><Ref refid="webappCtx"/></Arg> <!-- scope -->
    <Arg>server/password</Arg> <!-- name -->
    <Arg type="java.lang.String">qwerty</Arg> <!-- value -->
  </New>
</Configure>

之后,我在web.xml中定义了绑定,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="MyWebApp" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>My Web App</display-name>
<!-- INITIALIZE SPRING -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/spring-context.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- JETTY-ENV JNDI -->
<resource-ref>
    <res-ref-name>server/username</res-ref-name>
    <res-type>java.lang.String</res-type>
</resource-ref>
<resource-ref>
    <res-ref-name>server/password</res-ref-name>
    <res-type>java.lang.String</res-type>
</resource-ref>
</web-app>

并以这种方式配置了我的 Spring 上下文:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:amq="http://activemq.apache.org/schema/core"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
   xmlns:jee="http://www.springframework.org/schema/jee"
   xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.2.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
    <jee:jndi-lookup id="serverUsername" jndi-name="server/username" expected-type="java.lang.String" resource-ref="true" />
    <jee:jndi-lookup id="serverPassword" jndi-name="server/password" expected-type="java.lang.String" resource-ref="true"/>
    <bean name="abstractServerConnectionFactory" class="my.package.ServerConnectionFactory" abstract="true">
        <constructor-arg value="${server.host}"/>
        <constructor-arg value="${server.port}"/>
        <constructor-arg ref="serverUsername"/>
        <constructor-arg ref="serverPassword"/>
    </bean>
</beans>

之后,我在启用 Jetty 的情况下启动 --add-to-startd=jndi,在创建serverUsernameserverPassword时我总是javax.naming.NameNotFoundException。我尝试了很多修改,但似乎都没有工作。我试过:

  • org.eclipse.jetty.plus.jndi.Resource更改为org.eclipse.jetty.plus.jndi.EnvEntry,然后通过 Web 中的resource-env-ref引用它.xml
  • 将资源范围设置为 JVM 而不是 webapp,因此将<Arg><Ref refid="webappCtx"/></Arg>更改为 <Arg></Arg>,如此处所述
  • 在不使用 Web 的情况下添加自动绑定.xml如此处所述
<Call name="bindToENC">
    <Arg>server/username</Arg>  <!-- binds server/username to java:comp/env/server/username for this webapp -->
</Call>

还有许多其他,但它就是不起作用。请给我一个工作示例,如何让它工作。谢谢!

好的,所以我设法让它工作。问题是配置。我正在使用 Jetty 9,因此根据官方文档 (http://www.eclipse.org/jetty/documentation/current/jndi.html#jndi-quick-setup(,在 Jetty 中使用 JNDI 之前唯一需要做的是jndi模块通过执行--add-to-startd=jndi来添加到start.d。好吧,这并不完全正确,因为这将启用 JNDI,但不包括jetty-env.xml内容(码头甚至不要碰它(。我一直在阅读有关容器生命周期的信息,并注意到为了使用 JNDI,需要在 Web 应用程序上下文配置类集中包含以下类:

  • org.eclipse.jetty.plus.webapp.EnvConfiguration
  • org.eclipse.jetty.plus.webapp.PlusConfiguration

这是默认在 $JETTY_HOME/etc/jetty-plus.xml 中完成的,这是plus模块的配置文件。因此,为了将此类添加到 Jetty 容器生命周期中,并通过包含和解析 jetty-env.xml,除了 jndi 模块之外,还需要启用 plus 模块(jndi 不依赖于 plus(!因此,我通过调用 --add-to-startd=jndi,plus(模块之间没有空格(来更改我的 start.d 配置,一切都开始像魅力一样工作。

最新更新