|
33 | 33 | public class DefaultSaslConfig implements SaslConfig { |
34 | 34 | public static final String[] DEFAULT_PREFERRED_MECHANISMS = new String[]{"PLAIN"}; |
35 | 35 |
|
36 | | - private ConnectionFactory factory; |
37 | | - private String authorizationId; |
38 | | - private String[] preferredMechanisms = DEFAULT_PREFERRED_MECHANISMS; |
39 | | - private CallbackHandler callbackHandler; |
| 36 | + private final ConnectionFactory factory; |
| 37 | + private final List<String> mechanisms; |
| 38 | + private final CallbackHandler callbackHandler; |
40 | 39 |
|
| 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 | + */ |
41 | 45 | public DefaultSaslConfig(ConnectionFactory factory) { |
42 | | - this.factory = factory; |
43 | | - callbackHandler = new UsernamePasswordCallbackHandler(factory); |
44 | | - } |
45 | | - |
46 | | - public void setAuthorizationId(String authorizationId) { |
47 | | - this.authorizationId = authorizationId; |
| 46 | + this(factory, DEFAULT_PREFERRED_MECHANISMS); |
48 | 47 | } |
49 | 48 |
|
50 | 49 | /** |
51 | | - * Set a list of SASL mechanisms to use (in descending order of preference) |
52 | | - * @param preferredMechanisms |
| 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) |
53 | 54 | */ |
54 | | - public void setPreferredMechanisms(String[] preferredMechanisms) { |
55 | | - this.preferredMechanisms = preferredMechanisms; |
56 | | - } |
57 | | - |
58 | | - public void setCallbackHandler(CallbackHandler callbackHandler) { |
59 | | - this.callbackHandler = callbackHandler; |
| 55 | + public DefaultSaslConfig(ConnectionFactory factory, String[] mechanisms) { |
| 56 | + this.factory = factory; |
| 57 | + callbackHandler = new UsernamePasswordCallbackHandler(factory); |
| 58 | + this.mechanisms = Arrays.asList(mechanisms); |
60 | 59 | } |
61 | 60 |
|
62 | 61 | public SaslClient getSaslClient(String[] serverMechanisms) throws SaslException { |
63 | | - List<String> server = Arrays.asList(serverMechanisms); |
64 | | - List<String> client = Arrays.asList(preferredMechanisms); |
65 | | - client.retainAll(server); |
| 62 | + Set<String> server = new HashSet<String>(Arrays.asList(serverMechanisms)); |
66 | 63 |
|
67 | | - for (String mechanism: client) { |
68 | | - SaslClient saslClient = Sasl.createSaslClient(new String[]{mechanism}, |
69 | | - authorizationId, "AMQP", factory.getHost(), null, callbackHandler); |
70 | | - if (saslClient != null) return saslClient; |
| 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 | + } |
71 | 70 | } |
72 | 71 | return null; |
73 | 72 | } |
|
0 commit comments