Jetty JNDI 配置与 Spring 查找

问题描述 投票:0回答:1

我在 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>

之后,我在启用

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

  • org.eclipse.jetty.plus.jndi.Resource
    更改为
    org.eclipse.jetty.plus.jndi.EnvEntry
    ,然后通过 web.xml 中的
    resource-env-ref
    引用它
  • 将资源范围设置为 JVM 而不是 webapp,因此将
    <Arg><Ref refid="webappCtx"/></Arg>
    更改为
    <Arg></Arg>
    ,如此处
  • 所述
  • 添加自动绑定而不使用如上所述的 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个回答
1
投票

好的,我已经成功地让这个工作了。问题出在配置上。我正在使用 Jetty 9,因此根据官方文档(https://eclipse.dev/jetty/documentation/jetty-9/index.html#jndi-quick-setup),这是之前需要完成的唯一事情在 Jetty 中使用 JNDI 就是通过执行

jndi
start.d
模块添加到
--add-to-startd=jndi
。好吧,这并不完全正确,因为这将启用 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 配置,一切都开始像魅力一样工作。

© www.soinside.com 2019 - 2024. All rights reserved.