You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2018/01/23 15:32:27 UTC
[camel] branch master updated: CAMEL-10621: Fixed Rest DSL with
Jetty security via custom define security handler and turned on api-doc as
well would not startup Jetty server due missing NoLoginService error.
Thanks to Remco Schoen for sample code.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push:
new 25b0a29 CAMEL-10621: Fixed Rest DSL with Jetty security via custom define security handler and turned on api-doc as well would not startup Jetty server due missing NoLoginService error. Thanks to Remco Schoen for sample code.
25b0a29 is described below
commit 25b0a2912a535ce488b542f30f809ab0cad9f18f
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Jan 23 16:26:42 2018 +0100
CAMEL-10621: Fixed Rest DSL with Jetty security via custom define security handler and turned on api-doc as well would not startup Jetty server due missing NoLoginService error. Thanks to Remco Schoen for sample code.
---
.../camel/component/jetty/JettyHttpComponent.java | 19 +++--
.../camel/component/jetty/rest/MyLoginService.java | 72 +++++++++++++++++++
.../jetty/rest/RestJettyBasicAuthTest.java | 46 +++++++++++++
.../jetty/rest/RestJettyBasicAuthTest.xml | 80 ++++++++++++++++++++++
4 files changed, 212 insertions(+), 5 deletions(-)
diff --git a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
index ac1ecd4..4ae6252 100644
--- a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
+++ b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
@@ -25,6 +25,7 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
@@ -346,12 +347,20 @@ public abstract class JettyHttpComponent extends HttpCommonComponent implements
CONNECTORS.put(connectorKey, connectorRef);
} else {
-
+ // check if there are any new handlers, and if so then we need to re-start the server
if (endpoint.getHandlers() != null && !endpoint.getHandlers().isEmpty()) {
- // As the server is started, we need to stop the server for a while to add the new handler
- connectorRef.server.stop();
- addJettyHandlers(connectorRef.server, endpoint.getHandlers());
- connectorRef.server.start();
+ List<Handler> existingHandlers = new ArrayList<>();
+ if (connectorRef.server.getHandlers() != null && connectorRef.server.getHandlers().length > 0) {
+ existingHandlers = Arrays.asList(connectorRef.server.getHandlers());
+ }
+ List<Handler> newHandlers = new ArrayList<>(endpoint.getHandlers());
+ boolean changed = !existingHandlers.containsAll(newHandlers) && !newHandlers.containsAll(existingHandlers);
+ if (changed) {
+ LOG.debug("Restarting Jetty server due to adding new Jetty Handlers: {}", newHandlers);
+ connectorRef.server.stop();
+ addJettyHandlers(connectorRef.server, endpoint.getHandlers());
+ connectorRef.server.start();
+ }
}
// ref track the connector
connectorRef.increment();
diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/MyLoginService.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/MyLoginService.java
new file mode 100644
index 0000000..da1af1a
--- /dev/null
+++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/MyLoginService.java
@@ -0,0 +1,72 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jetty.rest;
+
+import java.security.Principal;
+import javax.security.auth.Subject;
+import javax.servlet.ServletRequest;
+
+import org.eclipse.jetty.security.DefaultIdentityService;
+import org.eclipse.jetty.security.IdentityService;
+import org.eclipse.jetty.security.LoginService;
+import org.eclipse.jetty.server.UserIdentity;
+
+public class MyLoginService implements LoginService {
+
+ private IdentityService is = new DefaultIdentityService();
+
+ @Override
+ public String getName() {
+ return "mylogin";
+ }
+
+ @Override
+ public UserIdentity login(String username, Object password, ServletRequest servletRequest) {
+ if ("donald".equals(username)) {
+ Subject subject = new Subject();
+ Principal principal = new Principal() {
+ @Override
+ public String getName() {
+ return "camel";
+ }
+ };
+ return getIdentityService().newUserIdentity(subject, principal, new String[] {"admin"});
+ } else {
+ return null;
+ }
+ }
+
+ @Override
+ public boolean validate(UserIdentity userIdentity) {
+ return true;
+ }
+
+ @Override
+ public IdentityService getIdentityService() {
+ return is;
+ }
+
+ @Override
+ public void setIdentityService(IdentityService identityService) {
+ this.is = identityService;
+ }
+
+ @Override
+ public void logout(UserIdentity userIdentity) {
+ // noop
+ }
+}
diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBasicAuthTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBasicAuthTest.java
new file mode 100644
index 0000000..0bd1e95
--- /dev/null
+++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBasicAuthTest.java
@@ -0,0 +1,46 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jetty.rest;
+
+import org.apache.camel.http.common.HttpOperationFailedException;
+import org.apache.camel.test.spring.CamelSpringTestSupport;
+import org.junit.Test;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+public class RestJettyBasicAuthTest extends CamelSpringTestSupport {
+
+ @Override
+ protected AbstractApplicationContext createApplicationContext() {
+ return new ClassPathXmlApplicationContext("org/apache/camel/component/jetty/rest/RestJettyBasicAuthTest.xml");
+ }
+
+ @Test
+ public void testJestDslBasicAuth() throws Exception {
+ String out = template.requestBody("http://localhost:9444/ping?authMethod=Basic&authUsername=donald&authPassword=duck", null, String.class);
+ assertEquals("\"pong\"", out);
+
+ try {
+ template.requestBody("http://localhost:9444/ping?authMethod=Basic&authUsername=mickey&authPassword=duck", null, String.class);
+ fail("Should not login");
+ } catch (Exception e) {
+ HttpOperationFailedException hofe = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
+ assertEquals(401, hofe.getStatusCode());
+ }
+ }
+
+}
diff --git a/components/camel-jetty9/src/test/resources/org/apache/camel/component/jetty/rest/RestJettyBasicAuthTest.xml b/components/camel-jetty9/src/test/resources/org/apache/camel/component/jetty/rest/RestJettyBasicAuthTest.xml
new file mode 100644
index 0000000..1196a7d
--- /dev/null
+++ b/components/camel-jetty9/src/test/resources/org/apache/camel/component/jetty/rest/RestJettyBasicAuthTest.xml
@@ -0,0 +1,80 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
+ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+ http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
+
+ <bean id="constraint" class="org.eclipse.jetty.util.security.Constraint">
+ <property name="name" value="BASIC"/>
+ <property name="roles" value="admin"/>
+ <property name="authenticate" value="true"/>
+ </bean>
+
+ <bean id="constraintMapping" class="org.eclipse.jetty.security.ConstraintMapping">
+ <property name="constraint" ref="constraint"/>
+ <property name="pathSpec" value="/*"/>
+ </bean>
+
+ <bean id="myLoginService" class="org.apache.camel.component.jetty.rest.MyLoginService"/>
+
+ <bean id="securityHandler" class="org.eclipse.jetty.security.ConstraintSecurityHandler">
+ <property name="constraintMappings">
+ <list>
+ <ref bean="constraintMapping"/>
+ </list>
+ </property>
+ <property name="authenticator">
+ <bean class="org.eclipse.jetty.security.authentication.BasicAuthenticator"/>
+ </property>
+ <property name="loginService" ref="myLoginService"/>
+ </bean>
+
+ <camelContext xmlns="http://camel.apache.org/schema/spring">
+ <restConfiguration component="jetty"
+ bindingMode="json"
+ port="9444"
+ apiContextPath="api-docs"
+ apiContextListing="true"
+ enableCORS="true"
+ >
+
+ <endpointProperty key="handlers" value="securityHandler"/>
+ <dataFormatProperty key="prettyPrint" value="true"/>
+ <apiProperty key="api.version" value="5"/>
+ <apiProperty key="api.title" value="MyTitle"/>
+ <apiProperty key="api.description" value="MyDescription"/>
+ <apiProperty key="cors" value="true"/>
+ </restConfiguration>
+
+
+ <rest>
+ <get uri="/ping">
+ <route>
+ <setBody>
+ <constant>pong</constant>
+ </setBody>
+ </route>
+ </get>
+ </rest>
+
+ </camelContext>
+</beans>
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
davsclaus@apache.org.