mirror of
https://github.com/apache/activemq.git
synced 2025-02-16 15:08:10 +00:00
https://issues.apache.org/jira/browse/AMQ-3924 - connect REST API to secure broker
git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1360626 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f180b1d7db
commit
d4742f597b
@ -83,11 +83,11 @@ public class JettyTestSupport extends TestCase {
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
session.close();
|
||||
connection.close();
|
||||
server.stop();
|
||||
broker.stop();
|
||||
broker.waitUntilStopped();
|
||||
session.close();
|
||||
connection.close();
|
||||
}
|
||||
|
||||
public void waitForJettySocketToAccept(String bindLocation) throws Exception {
|
||||
|
@ -178,4 +178,18 @@ public class RestTest extends JettyTestSupport {
|
||||
assertNotNull("Headers Exist", fields);
|
||||
assertEquals("header value", "value", fields.getStringField("property"));
|
||||
}
|
||||
|
||||
public void testAuth() throws Exception {
|
||||
HttpClient httpClient = new HttpClient();
|
||||
httpClient.start();
|
||||
ContentExchange contentExchange = new ContentExchange();
|
||||
httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
|
||||
contentExchange.setMethod("POST");
|
||||
contentExchange.setURL("http://localhost:8080/message/testPost?type=queue");
|
||||
contentExchange.setRequestHeader("Authorization", "Basic YWRtaW46YWRtaW4=");
|
||||
httpClient.send(contentExchange);
|
||||
|
||||
contentExchange.waitForDone();
|
||||
assertTrue("success status", HttpStatus.isSuccess(contentExchange.getResponseStatus()));
|
||||
}
|
||||
}
|
||||
|
@ -89,6 +89,9 @@ public class WebClient implements HttpSessionActivationListener, HttpSessionBind
|
||||
private CamelContext camelContext;
|
||||
private ProducerTemplate producerTemplate;
|
||||
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public WebClient() {
|
||||
if (factory == null) {
|
||||
throw new IllegalStateException("initContext(ServletContext) not called");
|
||||
@ -140,6 +143,22 @@ public class WebClient implements HttpSessionActivationListener, HttpSessionBind
|
||||
this.deliveryMode = deliveryMode;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public synchronized void closeConsumers() {
|
||||
for (Iterator<MessageConsumer> it = consumers.values().iterator(); it.hasNext();) {
|
||||
MessageConsumer consumer = it.next();
|
||||
@ -244,7 +263,7 @@ public class WebClient implements HttpSessionActivationListener, HttpSessionBind
|
||||
|
||||
public Connection getConnection() throws JMSException {
|
||||
if (connection == null) {
|
||||
connection = factory.createConnection();
|
||||
connection = factory.createConnection(username, password);
|
||||
connection.start();
|
||||
}
|
||||
return connection;
|
||||
@ -368,7 +387,21 @@ public class WebClient implements HttpSessionActivationListener, HttpSessionBind
|
||||
}
|
||||
|
||||
protected static WebClient createWebClient(HttpServletRequest request) {
|
||||
return new WebClient();
|
||||
WebClient client = new WebClient();
|
||||
String auth = request.getHeader("Authorization");
|
||||
if (auth != null) {
|
||||
String[] tokens = auth.split(" ");
|
||||
if (tokens.length == 2) {
|
||||
String encoded = tokens[1].trim();
|
||||
String credentials = new String(javax.xml.bind.DatatypeConverter.parseBase64Binary(encoded));
|
||||
String[] creds = credentials.split(":");
|
||||
if (creds.length == 2) {
|
||||
client.setUsername(creds[0]);
|
||||
client.setPassword(creds[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return client;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user