From bad1fb55e11c9ad1a0eaf0be67346db4995be2e8 Mon Sep 17 00:00:00 2001 From: Niels Date: Wed, 16 Nov 2022 17:15:41 +0100 Subject: [PATCH] Make Spring Security configurable on servlet basis (#3991) --- core/pom.xml | 5 + .../lifecycle/HttpSecurityConfigurer.java | 119 +----- .../lifecycle/ServletManager.java | 201 ++++++---- .../lifecycle/VerifyServerSecurityBean.java | 5 +- .../ActiveDirectoryAuthenticator.java | 134 +++++++ .../servlets/AuthenticationType.java | 36 ++ .../lifecycle/servlets/IAuthenticator.java | 24 ++ .../servlets/InMemoryAuthenticator.java | 59 +++ .../lifecycle/servlets/JeeAuthenticator.java | 112 ++++++ .../lifecycle/servlets/NoOpAuthenticator.java | 43 +++ .../servlets/ServletAuthenticatorBase.java | 130 +++++++ .../servlets/ServletConfiguration.java | 149 +++++++ .../nn/adapterframework/util/ClassUtils.java | 25 ++ .../webcontrol/DummySSLSocketFactory.java | 2 +- .../webcontrol/LoginFilter.java | 362 ------------------ .../main/resources/AppConstants.properties | 7 + .../resources/SpringEnvironmentContext.xml | 9 +- .../resources/ldap-role-mapping.properties | 4 + core/src/main/resources/log4j4ibis.xml | 4 +- core/src/main/resources/webSecurityConfig.xml | 77 ---- .../lifecycle/ServletManagerTest.java | 32 +- webapp/src/main/webapp/WEB-INF/web.xml | 26 -- 22 files changed, 900 insertions(+), 665 deletions(-) create mode 100644 core/src/main/java/nl/nn/adapterframework/lifecycle/servlets/ActiveDirectoryAuthenticator.java create mode 100644 core/src/main/java/nl/nn/adapterframework/lifecycle/servlets/AuthenticationType.java create mode 100644 core/src/main/java/nl/nn/adapterframework/lifecycle/servlets/IAuthenticator.java create mode 100644 core/src/main/java/nl/nn/adapterframework/lifecycle/servlets/InMemoryAuthenticator.java create mode 100644 core/src/main/java/nl/nn/adapterframework/lifecycle/servlets/JeeAuthenticator.java create mode 100644 core/src/main/java/nl/nn/adapterframework/lifecycle/servlets/NoOpAuthenticator.java create mode 100644 core/src/main/java/nl/nn/adapterframework/lifecycle/servlets/ServletAuthenticatorBase.java create mode 100644 core/src/main/java/nl/nn/adapterframework/lifecycle/servlets/ServletConfiguration.java delete mode 100644 core/src/main/java/nl/nn/adapterframework/webcontrol/LoginFilter.java create mode 100644 core/src/main/resources/ldap-role-mapping.properties delete mode 100644 core/src/main/resources/webSecurityConfig.xml diff --git a/core/pom.xml b/core/pom.xml index 011625988f3..d7cf1c6fbad 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -311,6 +311,11 @@ spring-security-config ${spring-security.version} + + org.springframework.security + spring-security-ldap + ${spring-security.version} + - + - - diff --git a/core/src/main/resources/ldap-role-mapping.properties b/core/src/main/resources/ldap-role-mapping.properties new file mode 100644 index 00000000000..b269a01343d --- /dev/null +++ b/core/src/main/resources/ldap-role-mapping.properties @@ -0,0 +1,4 @@ +#Defaults for legacy LoginFilter +IbisObserver=${ldap.auth.observer.base} +IbisDataAdmin=${ldap.auth.dataadmin.base} +IbisTester=${ldap.auth.tester.base} \ No newline at end of file diff --git a/core/src/main/resources/log4j4ibis.xml b/core/src/main/resources/log4j4ibis.xml index fb2882b7a5d..6958f7206a0 100644 --- a/core/src/main/resources/log4j4ibis.xml +++ b/core/src/main/resources/log4j4ibis.xml @@ -5,7 +5,7 @@ - %d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%t] %X{mid,cid} %c{2} - %m%nxEx{short} + %d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%t] %X{mid,cid} %c{2} - %m%n%xEx{short} @@ -102,6 +102,8 @@ + + diff --git a/core/src/main/resources/webSecurityConfig.xml b/core/src/main/resources/webSecurityConfig.xml deleted file mode 100644 index 3e5db089009..00000000000 --- a/core/src/main/resources/webSecurityConfig.xml +++ /dev/null @@ -1,77 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/core/src/test/java/nl/nn/adapterframework/lifecycle/ServletManagerTest.java b/core/src/test/java/nl/nn/adapterframework/lifecycle/ServletManagerTest.java index 3c152ccb965..f1f232bb761 100644 --- a/core/src/test/java/nl/nn/adapterframework/lifecycle/ServletManagerTest.java +++ b/core/src/test/java/nl/nn/adapterframework/lifecycle/ServletManagerTest.java @@ -3,6 +3,10 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThrows; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isA; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; import java.util.HashMap; import java.util.Map; @@ -23,10 +27,14 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.springframework.beans.factory.config.AutowireCapableBeanFactory; +import org.springframework.context.ApplicationContext; import org.springframework.mock.web.MockServletContext; import lombok.Getter; import lombok.Setter; +import nl.nn.adapterframework.lifecycle.servlets.IAuthenticator; +import nl.nn.adapterframework.lifecycle.servlets.ServletConfiguration; import nl.nn.adapterframework.util.AppConstants; import nl.nn.credentialprovider.util.Misc; @@ -35,7 +43,7 @@ public class ServletManagerTest { private static ServletManager manager; @BeforeClass - public static void prepare() { + public static void prepare() throws Exception { ServletContext context = new MockServletContext() { private Map dynamic = new HashMap<>(); @Override @@ -48,6 +56,27 @@ public ServletRegistration getServletRegistration(String servletName) { } }; manager = new ServletManager(context); + + ApplicationContext applicationContext = mock(ApplicationContext.class); + AutowireCapableBeanFactory beanFactory = mock(AutowireCapableBeanFactory.class); + doReturn(beanFactory).when(applicationContext).getAutowireCapableBeanFactory(); + doReturn(new DummyAuthenticator()).when(beanFactory).createBean(isA(IAuthenticator.class.getClass()), eq(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME), eq(false)); + manager.setApplicationContext(applicationContext); + + manager.afterPropertiesSet(); + } + + private static class DummyAuthenticator implements IAuthenticator { + + @Override + public void registerServlet(ServletConfiguration config) { + // NOOP + } + + @Override + public void build() { + // NOOP + } } @Before @@ -55,6 +84,7 @@ public void setUp() { Properties properties = new Properties(); properties.setProperty("dtap.stage", "ACC"); properties.setProperty(ServletManager.HTTPS_ENABLED_KEY, "confidential"); + properties.setProperty(ServletManager.AUTH_ENABLED_KEY, "false"); ServletManager.setupDefaultSecuritySettings(properties); } diff --git a/webapp/src/main/webapp/WEB-INF/web.xml b/webapp/src/main/webapp/WEB-INF/web.xml index 6a4261b26e6..675d33990f4 100644 --- a/webapp/src/main/webapp/WEB-INF/web.xml +++ b/webapp/src/main/webapp/WEB-INF/web.xml @@ -9,27 +9,6 @@ These actions are configured in an XML file. On the event several actions, called pipes, grouped in a pipeline, are fired. - - LoginFilter - nl.nn.adapterframework.webcontrol.LoginFilter - - allowedExtensions - css js gif jpg png svg - - - allowedObserverPaths - /index.jsp /FileViewerServlet /iaf/testtool - - - allowedDataAdminPaths - /iaf/testtool - - - allowedTesterPaths - /iaf/testtool - - - CacheControlFilter nl.nn.adapterframework.http.CacheControlFilter @@ -61,11 +40,6 @@ /iaf/api/* - - LoginFilter - /* - -