|
20 | 20 | import javax.security.sasl.Sasl; |
21 | 21 | import javax.security.sasl.SaslClient; |
22 | 22 | import javax.security.sasl.SaslException; |
23 | | -import java.util.Map; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.HashSet; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Set; |
24 | 27 |
|
25 | 28 | /** |
26 | 29 | * Default implementation of SaslConfig that uses the standard Java |
27 | 30 | * algorithm for selecting a sasl client. |
28 | 31 | * @see com.rabbitmq.client.ConnectionFactory |
29 | 32 | */ |
30 | 33 | public class DefaultSaslConfig implements SaslConfig { |
31 | | - private ConnectionFactory factory; |
32 | | - private String authorizationId; |
33 | | - private Map<String,?> mechanismProperties; |
34 | | - private CallbackHandler callbackHandler; |
| 34 | + public static final String[] DEFAULT_PREFERRED_MECHANISMS = new String[]{"PLAIN"}; |
35 | 35 |
|
36 | | - public DefaultSaslConfig(ConnectionFactory factory) { |
37 | | - this.factory = factory; |
38 | | - callbackHandler = new UsernamePasswordCallbackHandler(factory); |
39 | | - } |
| 36 | + private final ConnectionFactory factory; |
| 37 | + private final List<String> mechanisms; |
| 38 | + private final CallbackHandler callbackHandler; |
40 | 39 |
|
41 | | - public void setAuthorizationId(String authorizationId) { |
42 | | - this.authorizationId = authorizationId; |
| 40 | + /** |
| 41 | + * Create a DefaultSaslConfig which only wants to use PLAIN. |
| 42 | + * |
| 43 | + * @param factory - the ConnectionFactory to use to obtain username, password and host |
| 44 | + */ |
| 45 | + public DefaultSaslConfig(ConnectionFactory factory) { |
| 46 | + this(factory, DEFAULT_PREFERRED_MECHANISMS); |
43 | 47 | } |
44 | 48 |
|
45 | | - public void setMechanismProperties(Map<String, ?> mechanismProperties) { |
46 | | - this.mechanismProperties = mechanismProperties; |
| 49 | + /** |
| 50 | + * Create a DefaultSaslConfig with a list of mechanisms to use. |
| 51 | + * |
| 52 | + * @param factory - the ConnectionFactory to use to obtain username, password and host |
| 53 | + * @param mechanisms - a list of SASL mechanisms to use (in descending order of preference) |
| 54 | + */ |
| 55 | + public DefaultSaslConfig(ConnectionFactory factory, String[] mechanisms) { |
| 56 | + this.factory = factory; |
| 57 | + callbackHandler = new UsernamePasswordCallbackHandler(factory); |
| 58 | + this.mechanisms = Arrays.asList(mechanisms); |
47 | 59 | } |
48 | 60 |
|
49 | | - public void setCallbackHandler(CallbackHandler callbackHandler) { |
50 | | - this.callbackHandler = callbackHandler; |
51 | | - } |
| 61 | + public SaslClient getSaslClient(String[] serverMechanisms) throws SaslException { |
| 62 | + Set<String> server = new HashSet<String>(Arrays.asList(serverMechanisms)); |
52 | 63 |
|
53 | | - public SaslClient getSaslClient(String[] mechanisms) throws SaslException { |
54 | | - return Sasl.createSaslClient(mechanisms, authorizationId, "AMQP", |
55 | | - factory.getHost(), mechanismProperties, callbackHandler); |
| 64 | + for (String mechanism: mechanisms) { |
| 65 | + if (server.contains(mechanism)) { |
| 66 | + SaslClient saslClient = Sasl.createSaslClient(new String[]{mechanism}, |
| 67 | + null, "AMQP", factory.getHost(), null, callbackHandler); |
| 68 | + if (saslClient != null) return saslClient; |
| 69 | + } |
| 70 | + } |
| 71 | + return null; |
56 | 72 | } |
57 | 73 | } |
0 commit comments