我收到一个JmsSecurityException
使用 Spring 发布到安全 WebLogic JMS 队列的消息JmsTemplate
。令人不安的部分是我能够使用等效的非 Spring 代码成功发布。
下面是抛出的异常(假设目标是QUEUE.IN
)。
org.springframework.jms.JmsSecurityException: Access denied to resource: type=<jms>, application=DistQueue-Module, destinationType=queue, resource=QUEUE.IN, action=send; nested exception is weblogic.jms.common.JMSSecurityException: Access denied to resource: type=<jms>, application=DistQueue-Module, destinationType=queue, resource=QUEUE.IN, action=send
at org.springframework.jms.support.JmsUtils.convertJmsAccessException(JmsUtils.java:291)
at org.springframework.jms.support.JmsAccessor.convertJmsAccessException(JmsAccessor.java:168)
at org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:469)
at org.springframework.jms.core.JmsTemplate.send(JmsTemplate.java:534)
at org.springframework.jms.core.JmsTemplate.send(JmsTemplate.java:526)
at com.company.jms.publisher.service.impl.JmsServiceImpl.sendMessage(JmsServiceImpl.java:34)
at com.company.jms.publisher.service.impl.JmsServiceImpl.publish(JmsServiceImpl.java:29)
at com.company.jms.publisher.controller.MainController.postIt(MainController.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:401)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:945)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
2013-11-07 19:26:56.506:WARN::Nested in org.springframework.jms.JmsSecurityException: Access denied to resource: type=<jms>, application=DistQueue-Module, destinationType=queue, resource=QUEUE.IN, action=send; nested exception is weblogic.jms.common.JMSSecurityException: Access denied to resource: type=<jms>, application=DistQueue-Module, destinationType=queue, resource=QUEUE.IN, action=send:
weblogic.jms.common.JMSSecurityException: Access denied to resource: type=<jms>, application=DistQueue-Module, destinationType=queue, resource=QUEUE.IN, action=send
at weblogic.jms.common.JMSSecurityHelper.checkPermission(JMSSecurityHelper.java:162)
at weblogic.jms.backend.BEDestinationSecurityImpl.checkSendPermission(BEDestinationSecurityImpl.java:74)
at weblogic.jms.backend.BEDestinationImpl.checkPermission(BEDestinationImpl.java:2513)
at weblogic.jms.backend.BEDestinationImpl.sendInitialize(BEDestinationImpl.java:1565)
at weblogic.jms.backend.BEDestinationImpl.send(BEDestinationImpl.java:2079)
at weblogic.jms.backend.BEDestinationImpl.wrappedSend(BEDestinationImpl.java:2051)
at weblogic.jms.backend.BEDestinationImpl.invoke(BEDestinationImpl.java:1539)
at weblogic.messaging.dispatcher.Request.wrappedFiniteStateMachine(Request.java:961)
at weblogic.messaging.dispatcher.DispatcherImpl.dispatchAsyncInternal(DispatcherImpl.java:140)
at weblogic.messaging.dispatcher.DispatcherImpl.dispatchAsync(DispatcherImpl.java:116)
at weblogic.messaging.dispatcher.Request.dispatchAsync(Request.java:1304)
at weblogic.jms.dispatcher.Request.dispatchAsync(Request.java:97)
at weblogic.jms.frontend.FEProducer.doDispatch(FEProducer.java:900)
at weblogic.jms.frontend.FEProducer.sendRetryDestination(FEProducer.java:1033)
at weblogic.jms.frontend.FEProducer.send(FEProducer.java:1435)
at weblogic.jms.frontend.FEProducer.invoke(FEProducer.java:1496)
at weblogic.messaging.dispatcher.Request.wrappedFiniteStateMachine(Request.java:961)
at weblogic.messaging.dispatcher.DispatcherServerRef.invoke(DispatcherServerRef.java:276)
at weblogic.messaging.dispatcher.DispatcherServerRef.handleRequest(DispatcherServerRef.java:141)
at weblogic.messaging.dispatcher.DispatcherServerRef.access$000(DispatcherServerRef.java:34)
at weblogic.messaging.dispatcher.DispatcherServerRef$2.run(DispatcherServerRef.java:112)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:178)
忽略我已经剥离了所有用于原始调试的池代码这一事实,我将以下配置与标准 JmsTemplate 一起用于发送消息。
<beans:bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<beans:property name="environment">
<beans:props>
<beans:prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</beans:prop>
<beans:prop key="java.naming.provider.url">${jms.connectionUrl}</beans:prop>
<beans:prop key="java.naming.security.principal">${jms.username}</beans:prop>
<beans:prop key="java.naming.security.credentials">${jms.password}</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<beans:bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<beans:property name="jndiTemplate" ref="jndiTemplate" />
<beans:property name="jndiName" value="${jms.connectionFactoryName}" />
</beans:bean>
<beans:bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
<beans:property name="jndiTemplate" ref="jndiTemplate" />
<beans:property name="jndiName" value="${jms.destinationName}" />
</beans:bean>
<beans:bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<beans:property name="connectionFactory" ref="connectionFactory" />
<beans:property name="defaultDestination" ref="destination" />
<beans:property name="sessionAcknowledgeModeName" value="AUTO_ACKNOWLEDGE" />
<beans:property name="sessionTransacted" value="false" />
</beans:bean>
成功发布的非 Spring 代码的等效位如下。
Hashtable<String, String> environment = new Hashtable<String, String>();
environment.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
environment.put(Context.PROVIDER_URL, prop("jms.connectionUrl"));
environment.put(Context.SECURITY_PRINCIPAL, prop("jms.username"));
environment.put(Context.SECURITY_CREDENTIALS, prop("jms.password"));
Context context = new InitialContext(environment);
ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup(prop("jms.connectionFactoryName"));
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = (Destination) context.lookup(prop("jms.destinationName"));
MessageProducer producer = session.createProducer(destination);
connection.start();
TextMessage tm = session.createTextMessage(UUID.randomUUID().toString());
producer.send(tm);
从异常消息来看,它让我相信凭据没有在关键位置提供。JNDI 查找成功,将任何属性(包括凭证)更改为无效值会导致在初始化期间抛出明显的错误,早在尝试发布之前。
我最初认为可以通过UserCredentialsConnectionFactoryAdapter
确保在创建时提供凭据来解决问题,但它导致了同样的错误。此外,我不需要在上面的简单示例中使用它。
只是为了完整起见,这里有一些 jmsTemplate 代码。
public void sendMessage(final String message) throws JmsException {
jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage(message);
return textMessage;
}
});
}
最后但并非最不重要的一点是,我无法控制或访问要发布的 WebLogic 实例。我有一个联系人将查看日志以查看是否有任何重要线索,但这可能需要相当长的时间。