【问题标题】:Jetty JNDI configuration vs Spring lookupJetty JNDI 配置与 Spring 查找
【发布时间】:2014-10-24 17:30:51
【问题描述】:

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

我有一些我不想存储在属性中的凭据,这样它就不会存在于 git repo 中。我的 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>

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

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

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

【问题讨论】:

    标签: spring jetty jndi


    【解决方案1】:

    好的,所以我已经设法让它工作了。问题是配置。我正在使用 Jetty 9,因此根据官方文档 (http://www.eclipse.org/jetty/documentation/current/jndi.html#jndi-quick-setup),在 Jetty 中使用 JNDI 之前唯一需要做的就是通过执行 --add-to-startd=jndijndi 模块添加到 start.d。好吧,这并不完全正确,因为这将启用 JNDI,但不会包括 jetty-env.xml 内容(Jetty 甚至不会触摸它)。我一直在阅读有关容器生命周期的内容,并注意到为了使用 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 配置,一切都开始像魅力一样工作。

    【讨论】:

      猜你喜欢
      • 2011-05-17
      • 1970-01-01
      • 2013-07-29
      • 1970-01-01
      • 2011-12-16
      • 2015-06-21
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多