簡體   English   中英

Jetty JNDI配置與Spring查找

[英]Jetty JNDI configuration vs Spring lookup

我在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的代替web應用因此更改<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 ),在使用JNDI之前唯一需要做的事情在Jetty中,通過--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配置,並且一切都開始正常運行了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM