I'm receiving a JmsSecurityException
when publish to a secured WebLogic JMS queue using a Spring's JmsTemplate
. The troubling part is that I'm able to publish successfully using an equivalent non-Spring bit of code.
Below is the exception being thrown (assuming the destination is 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)
Ignoring the fact that I've stripped out all the pooling code for raw debugging, I'm using the following configuration with the standard JmsTemplate for sending messages.
<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>
The equivalent bit of non-Spring code that successfully publishes is below.
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);
Judging by the exception message, it leads me to believe that the credentials are not being provided at a critical location. The JNDI lookups are successful, and changing any properties (including the credentials) to invalid values causes clear errors to be thrown during initialization, long before attempting to publish.
I initially thought the problem would be solved using UserCredentialsConnectionFactoryAdapter
to ensure the credentials were being provided at creation time, but it resulted in the same error. Additionally, I haven't needed to use this for the simple example above.
Just for completeness, here is revelant the jmsTemplate bit of code.
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;
}
});
}
And last but not least, I don't have control or access to the WebLogic instance to where I am publishing. I have a contact who will be reviewing logs to see if there are any important clues, but it could be quite some time.
I've discovered a (non-intuitive) workaround for this issue after some additional research into similar Spring bug reports. I've decided to post this as an answer instead of deleting the question as I hope it will help someone else in the future.
Specifically, I came across SPR-4720 from 2008. John Bakar recommends a fix which appears to do nothing at all, however it does indeed solve the issue above.
Applied to my project, the fix looks like this:
public class UserCredentialsConnectionFactoryAdapter extends org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter {
private JndiTemplate jndiTemplate;
public void setJndiTemplate(JndiTemplate jndiTemplate) {
this.jndiTemplate = jndiTemplate;
}
@Override
protected Connection doCreateConnection(String username, String password) throws JMSException {
wlFix();
return super.doCreateConnection(username, password);
}
/**
* Associate JNDI variables (user and password) with this thread for the
* benefit of the WL drivers.
*/
protected void wlFix() {
try {
@SuppressWarnings("unused")
InitialContext initialContext = new InitialContext(jndiTemplate.getEnvironment());
} catch (NamingException e) {
e.printStackTrace();
}
}
}
Weird, right?
All-in-all, for the reference of others, my Spring configuration now looks like this:
<beans:bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<beans:property name="environment">
<beans:props>
<beans:prop key="java.naming.factory.initial">${jms.jndiFactory}</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="connectionFactoryAdapter" class="com.company.jms.publisher.UserCredentialsConnectionFactoryAdapter">
<beans:property name="jndiTemplate" ref="jndiTemplate" />
<beans:property name="targetConnectionFactory" ref="connectionFactory" />
<beans:property name="username" value="${jms.username}" />
<beans:property name="password" value="${jms.password}" />
</beans:bean>
<beans:bean id="cachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<beans:property name="targetConnectionFactory" ref="connectionFactoryAdapter" />
<beans:property name="sessionCacheSize" value="10" />
</beans:bean>
<beans:bean id="destinationResolver" class="org.springframework.jms.support.destination.JndiDestinationResolver">
<beans:property name="jndiTemplate" ref="jndiTemplate" />
</beans:bean>
<beans:bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<beans:property name="connectionFactory" ref="cachingConnectionFactory" />
<beans:property name="destinationResolver" ref="destinationResolver" />
<beans:property name="defaultDestinationName" value="${jms.publish.destinationName}" />
<beans:property name="sessionAcknowledgeModeName" value="AUTO_ACKNOWLEDGE" />
<beans:property name="sessionTransacted" value="false" />
</beans:bean>
I'd be willing to offer a bounty to anyone who is able to authoritatively (no speculation) explain why creating a dummy InitialContext
and doing nothing with it solves the issue of not properly providing the credentials to WLS.