[英]Jetty JNDI configuration vs Spring lookup
I have got a problem with JNDI configuration on Jetty server. 我在Jetty服务器上的JNDI配置有问题。 I cannot by any means configure it in a way that afterwards Spring (3.0.5) can retrieve JNDI variables. 我不能以任何方式配置它,使得Spring(3.0.5)之后可以检索JNDI变量。
I have some credentials which I do not want to store in properties so that it will not exist in git repo. 我有一些凭据,这些凭据我不想存储在属性中,因此它不会存在于git repo中。 My web application is running on Jetty (most recent version 9.2.3), thus I came up with idea to store this credentials in Jetty web application context. 我的Web应用程序在Jetty(最新版本9.2.3)上运行,因此我想到了将凭据存储在Jetty Web应用程序上下文中的想法。 Jetty provides such solution with jetty-env.xml
. Jetty通过jetty-env.xml
提供了这样的解决方案。 So I have created jetty-env.xml file in my WEB-INF/ like following: 因此,我在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>
After that I have defined binding in the web.xml
as follows: 之后,我在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>
And configured my Spring context in this way: 并以这种方式配置我的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>
After that I am starting Jetty with --add-to-startd=jndi
enabled and I am always getting javax.naming.NameNotFoundException
when creating serverUsername
and serverPassword
. 之后,我在启用--add-to-startd=jndi
启动Jetty,并且在创建serverUsername
和serverPassword
时始终会遇到javax.naming.NameNotFoundException
。 I have tried many modfication, but non have seem to work. 我已经尝试了许多修改,但是似乎没有用。 I have tried: 我努力了:
org.eclipse.jetty.plus.jndi.Resource
to org.eclipse.jetty.plus.jndi.EnvEntry
and then refering to it via resource-env-ref
in web.xml 将org.eclipse.jetty.plus.jndi.Resource
更改为org.eclipse.jetty.plus.jndi.EnvEntry
,然后通过web.xml中的resource-env-ref
<Arg><Ref refid="webappCtx"/></Arg>
to <Arg></Arg>
as mentioned here 资源范围设置为JVM的代替web应用因此更改<Arg><Ref refid="webappCtx"/></Arg>
到<Arg></Arg>
如所提到的在这里 <Call name="bindToENC">
<Arg>server/username</Arg> <!-- binds server/username to java:comp/env/server/username for this webapp -->
</Call>
And many other, but it just does not work. 和许多其他功能一样,但这只是行不通。 Please give me a working example how I can get it to work. 请给我一个工作示例,说明如何使它工作。 Thank you! 谢谢!
Ok so I have managed to get this working. 好的,所以我设法解决了这个问题。 The problem was the configuration. 问题是配置。 I am using Jetty 9 so according to the official documentation ( http://www.eclipse.org/jetty/documentation/current/jndi.html#jndi-quick-setup ) the only thing that needs to be done prior to using JNDI in Jetty is adding jndi
module to start.d
by doing --add-to-startd=jndi
. 我正在使用Jetty 9,因此根据官方文档( http://www.eclipse.org/jetty/documentation/current/jndi.html#jndi-quick-setup ),在使用JNDI之前唯一需要做的事情在Jetty中,通过--add-to-startd=jndi
将jndi
模块添加到start.d
。 And well, that's not exactly true, because this will enable JNDI, BUT WILL NOT INCLUDE jetty-env.xml
contents (Jetty do not even touch it). 嗯,那不是完全正确的,因为这将启用JNDI,但不会包含jetty-env.xml
内容(Jetty甚至不会触摸它)。 I've been reading about the container lifecycle and noticed that in order to use JNDI one need to include following classes in the web application context configuration classes set: 我一直在阅读有关容器生命周期的文章,并注意到,要使用JNDI,需要在Web应用程序上下文配置类集中包含以下类:
This is done by default in $JETTY_HOME/etc/jetty-plus.xml
, which is config file for plus
module. 默认情况下,这是在$JETTY_HOME/etc/jetty-plus.xml
,该文件是plus
模块的配置文件。 So in order to add this classes into the Jetty container lifecycle, and by this include and parse jetty-env.xml, one need to enable plus module in addition to jndi module (jndi does not depend upon plus)! 因此,为了将此类添加到Jetty容器生命周期中,并通过包含和解析jetty-env.xml,除了jndi模块之外,还需要启用plus模块(jndi不依赖于plus)! Thus I have changed my start.d config by invoking --add-to-startd=jndi,plus
(no space between modules) and everything started working like a charm. 因此,我通过调用--add-to-startd=jndi,plus
(模块之间没有空格)更改了start.d配置,并且一切都开始正常运行了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.