Consistently using credentials for creating the JMSContext

This commit updates UserCredentialsConnectionFactoryAdapter to apply
the same handling of credentials for creating the JMSContext.

Closes gh-33270
This commit is contained in:
Stéphane Nicoll
2024-07-25 09:41:08 +02:00
parent 6e55e78b22
commit 65faca8236
2 changed files with 189 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,14 +33,16 @@ import org.springframework.util.StringUtils;
/**
* An adapter for a target JMS {@link jakarta.jms.ConnectionFactory}, applying the
* given user credentials to every standard {@code createConnection()} call,
* that is, implicitly invoking {@code createConnection(username, password)}
* on the target. All other methods simply delegate to the corresponding methods
* of the target ConnectionFactory.
* given user credentials to every standard methods that can also be used with
* authentication, this {@code createConnection()} and {@code createContext()}. In
* other words, it is implicitly invoking {@code createConnection(username, password)} or
* {@code createContext(username, password)}} on the target. All other methods simply
* delegate to the corresponding methods of the target ConnectionFactory.
*
* <p>Can be used to proxy a target JNDI ConnectionFactory that does not have user
* credentials configured. Client code can work with the ConnectionFactory without
* passing in username and password on every {@code createConnection()} call.
* passing in username and password on every {@code createConnection()} and
* {@code createContext()} call.
*
* <p>In the following example, client code can simply transparently work
* with the preconfigured "myConnectionFactory", implicitly accessing
@@ -58,9 +60,9 @@ import org.springframework.util.StringUtils;
* &lt;/bean&gt;</pre>
*
* <p>If the "username" is empty, this proxy will simply delegate to the standard
* {@code createConnection()} method of the target ConnectionFactory.
* This can be used to keep a UserCredentialsConnectionFactoryAdapter bean
* definition just for the <i>option</i> of implicitly passing in user credentials
* {@code createConnection()} or {@code createContext()} method of the target
* ConnectionFactory. This can be used to keep a UserCredentialsConnectionFactoryAdapter
* bean definition just for the <i>option</i> of implicitly passing in user credentials
* if the particular target ConnectionFactory requires it.
*
* <p>As of Spring Framework 5, this class delegates JMS 2.0 {@code JMSContext}
@@ -69,8 +71,10 @@ import org.springframework.util.StringUtils;
* as long as no actual JMS 2.0 calls are triggered by the application's setup.
*
* @author Juergen Hoeller
* @author Stephane Nicoll
* @since 1.2
* @see #createConnection
* @see #createContext
* @see #createQueueConnection
* @see #createTopicConnection
*/
@@ -296,7 +300,22 @@ public class UserCredentialsConnectionFactoryAdapter
@Override
public JMSContext createContext() {
return obtainTargetConnectionFactory().createContext();
JmsUserCredentials threadCredentials = this.threadBoundCredentials.get();
if (threadCredentials != null) {
return doCreateContext(threadCredentials.username, threadCredentials.password);
}
else {
return doCreateContext(this.username, this.password);
}
}
protected JMSContext doCreateContext(@Nullable String username, @Nullable String password) {
if (StringUtils.hasLength(username)) {
return obtainTargetConnectionFactory().createContext(username, password);
}
else {
return obtainTargetConnectionFactory().createContext();
}
}
@Override
@@ -311,7 +330,22 @@ public class UserCredentialsConnectionFactoryAdapter
@Override
public JMSContext createContext(int sessionMode) {
return obtainTargetConnectionFactory().createContext(sessionMode);
JmsUserCredentials threadCredentials = this.threadBoundCredentials.get();
if (threadCredentials != null) {
return doCreateContext(threadCredentials.username, threadCredentials.password, sessionMode);
}
else {
return doCreateContext(this.username, this.password, sessionMode);
}
}
protected JMSContext doCreateContext(@Nullable String username, @Nullable String password, int sessionMode) {
if (StringUtils.hasLength(username)) {
return obtainTargetConnectionFactory().createContext(username, password, sessionMode);
}
else {
return obtainTargetConnectionFactory().createContext(sessionMode);
}
}
private ConnectionFactory obtainTargetConnectionFactory() {