From 3a9aad02cf59a8b826d8099e87e0c4cce95118a2 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Mon, 15 Jan 2024 14:08:23 -0800 Subject: [PATCH] Partial migration of mail --- .../com/wisemapping/config/Application.java | 14 +- .../config/MethodSecurityConfig.java | 38 ----- .../config/common/CommonConfig.java | 11 ++ .../config/{ => common}/HibernateConfig.java | 4 +- .../config/common/InterceptorsConfig.java | 17 +++ .../config/common/SecurityConfig.java | 78 +++++++++++ .../config/rest/RestAppConfig.java | 3 +- .../GoogleAuthenticationProvider.java | 75 +++++----- .../wisemapping/service/MailerService.java | 2 +- .../src/main/resources/application.properties | 2 +- .../resources/spring/wisemapping-mail.xml | 15 ++ ...g-service.xml => wisemapping-messages.xml} | 2 - .../resources/spring/wisemapping-rest.xml | 9 ++ .../spring/wisemapping-security-db.xml | 26 ---- .../spring/wisemapping-security-ldap.xml | 55 -------- .../resources/spring/wisemapping-servlet.xml | 20 --- wise-webapp/src/main/webapp/WEB-INF/web.xml | 131 ------------------ 17 files changed, 179 insertions(+), 323 deletions(-) delete mode 100644 wise-webapp/src/main/java/com/wisemapping/config/MethodSecurityConfig.java create mode 100644 wise-webapp/src/main/java/com/wisemapping/config/common/CommonConfig.java rename wise-webapp/src/main/java/com/wisemapping/config/{ => common}/HibernateConfig.java (93%) create mode 100644 wise-webapp/src/main/java/com/wisemapping/config/common/InterceptorsConfig.java create mode 100644 wise-webapp/src/main/java/com/wisemapping/config/common/SecurityConfig.java create mode 100755 wise-webapp/src/main/resources/spring/wisemapping-mail.xml rename wise-webapp/src/main/resources/spring/{wisemapping-service.xml => wisemapping-messages.xml} (95%) delete mode 100644 wise-webapp/src/main/resources/spring/wisemapping-security-db.xml delete mode 100644 wise-webapp/src/main/resources/spring/wisemapping-security-ldap.xml delete mode 100644 wise-webapp/src/main/resources/spring/wisemapping-servlet.xml delete mode 100644 wise-webapp/src/main/webapp/WEB-INF/web.xml diff --git a/wise-webapp/src/main/java/com/wisemapping/config/Application.java b/wise-webapp/src/main/java/com/wisemapping/config/Application.java index 44c48de5..a76f9a1c 100644 --- a/wise-webapp/src/main/java/com/wisemapping/config/Application.java +++ b/wise-webapp/src/main/java/com/wisemapping/config/Application.java @@ -1,15 +1,15 @@ package com.wisemapping.config; -import com.wisemapping.config.mvc.MvcAppConfig; -import com.wisemapping.config.mvc.MvcSecurityConfig; +import com.wisemapping.config.common.CommonConfig; +import com.wisemapping.config.common.HibernateConfig; +import com.wisemapping.config.common.InterceptorsConfig; +import com.wisemapping.config.common.SecurityConfig; import com.wisemapping.config.rest.ServletConfig; import com.wisemapping.config.rest.RestAppConfig; import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.ImportResource; import org.springframework.security.web.firewall.StrictHttpFirewall; @SpringBootApplication @@ -18,9 +18,9 @@ public class Application { public static void main(String[] args) { new SpringApplicationBuilder() - .parent(MethodSecurityConfig.class, HibernateConfig.class).web(WebApplicationType.NONE) -// .child(MvcAppConfig.class, MvcSecurityConfig.class).web(WebApplicationType.SERVLET) - .child(RestAppConfig.class, ServletConfig.class).web(WebApplicationType.SERVLET) + .parent(HibernateConfig.class, ServletConfig.class, CommonConfig.class, SecurityConfig.class).web(WebApplicationType.NONE) +// .child(MvcAppConfig.class, MvcSecurityConfig.class, SecurityConfig.class).web(WebApplicationType.SERVLET) + .child(RestAppConfig.class, ServletConfig.class, InterceptorsConfig.class).web(WebApplicationType.SERVLET) .run(args); } diff --git a/wise-webapp/src/main/java/com/wisemapping/config/MethodSecurityConfig.java b/wise-webapp/src/main/java/com/wisemapping/config/MethodSecurityConfig.java deleted file mode 100644 index e4423b76..00000000 --- a/wise-webapp/src/main/java/com/wisemapping/config/MethodSecurityConfig.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.wisemapping.config; - -import com.wisemapping.security.MapAccessPermissionEvaluation; -import com.wisemapping.security.ReadSecurityAdvise; -import com.wisemapping.security.UpdateSecurityAdvise; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.ImportResource; -import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler; -import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler; -import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; - -@Configuration -@EnableMethodSecurity( - securedEnabled = true, - jsr250Enabled = true) -@ImportResource(value = {"classpath:spring/wisemapping-service.xml"}) -@ComponentScan({"com.wisemapping.security", "com.wisemapping.service", "com.wisemapping.dao", "com.wisemapping.util", "com.wisemapping.model"}) -public class MethodSecurityConfig { - - @Autowired - private ReadSecurityAdvise readAdvice; - - @Autowired - private UpdateSecurityAdvise updateAdvice; - - @Bean - protected MethodSecurityExpressionHandler createExpressionHandler() { - DefaultMethodSecurityExpressionHandler expressionHandler = - new DefaultMethodSecurityExpressionHandler(); - - final MapAccessPermissionEvaluation permissionEvaluator = new MapAccessPermissionEvaluation(readAdvice, updateAdvice); - expressionHandler.setPermissionEvaluator(permissionEvaluator); - return expressionHandler; - } -} diff --git a/wise-webapp/src/main/java/com/wisemapping/config/common/CommonConfig.java b/wise-webapp/src/main/java/com/wisemapping/config/common/CommonConfig.java new file mode 100644 index 00000000..c3bfc4b7 --- /dev/null +++ b/wise-webapp/src/main/java/com/wisemapping/config/common/CommonConfig.java @@ -0,0 +1,11 @@ +package com.wisemapping.config.common; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; + +@Configuration +@ImportResource(value = {"spring/wisemapping-mail.xml"}) +@ComponentScan({"com.wisemapping.security", "com.wisemapping.service", "com.wisemapping.dao", "com.wisemapping.util", "com.wisemapping.model"}) +public class CommonConfig { +} diff --git a/wise-webapp/src/main/java/com/wisemapping/config/HibernateConfig.java b/wise-webapp/src/main/java/com/wisemapping/config/common/HibernateConfig.java similarity index 93% rename from wise-webapp/src/main/java/com/wisemapping/config/HibernateConfig.java rename to wise-webapp/src/main/java/com/wisemapping/config/common/HibernateConfig.java index b5d266b7..56a4a281 100644 --- a/wise-webapp/src/main/java/com/wisemapping/config/HibernateConfig.java +++ b/wise-webapp/src/main/java/com/wisemapping/config/common/HibernateConfig.java @@ -1,4 +1,4 @@ -package com.wisemapping.config; +package com.wisemapping.config.common; import com.wisemapping.model.User; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -9,7 +9,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @Configuration @EnableAutoConfiguration -@EnableJpaRepositories(basePackages={"com.wisemapping.dao"}) +@EnableJpaRepositories(basePackages={"com.wisemapping.dao","com.wisemapping.service"}) @EntityScan(basePackageClasses= User.class) public class HibernateConfig { diff --git a/wise-webapp/src/main/java/com/wisemapping/config/common/InterceptorsConfig.java b/wise-webapp/src/main/java/com/wisemapping/config/common/InterceptorsConfig.java new file mode 100644 index 00000000..bde3ae28 --- /dev/null +++ b/wise-webapp/src/main/java/com/wisemapping/config/common/InterceptorsConfig.java @@ -0,0 +1,17 @@ +package com.wisemapping.config.common; + +import com.wisemapping.filter.RequestPropertiesInterceptor; +import com.wisemapping.filter.UserLocaleInterceptor; +import org.jetbrains.annotations.NotNull; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Component +public class InterceptorsConfig implements WebMvcConfigurer { + @Override + public void addInterceptors(@NotNull final InterceptorRegistry registry) { + registry.addInterceptor(new UserLocaleInterceptor()); + registry.addInterceptor(new RequestPropertiesInterceptor()); + } +} \ No newline at end of file diff --git a/wise-webapp/src/main/java/com/wisemapping/config/common/SecurityConfig.java b/wise-webapp/src/main/java/com/wisemapping/config/common/SecurityConfig.java new file mode 100644 index 00000000..f0dc3628 --- /dev/null +++ b/wise-webapp/src/main/java/com/wisemapping/config/common/SecurityConfig.java @@ -0,0 +1,78 @@ +package com.wisemapping.config.common; + +import com.wisemapping.security.*; +import org.jetbrains.annotations.NotNull; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler; +import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.crypto.password.PasswordEncoder; + +import static org.springframework.security.crypto.factory.PasswordEncoderFactories.createDelegatingPasswordEncoder; + +@Configuration +@EnableWebSecurity +@EnableMethodSecurity( + securedEnabled = true, + jsr250Enabled = true) +public class SecurityConfig { + + @Autowired + private ReadSecurityAdvise readAdvice; + + @Autowired + private UpdateSecurityAdvise updateAdvice; + + @Autowired + private UserDetailsService userDetailsService; + + @Bean + protected MethodSecurityExpressionHandler createExpressionHandler() { + DefaultMethodSecurityExpressionHandler expressionHandler = + new DefaultMethodSecurityExpressionHandler(); + + final MapAccessPermissionEvaluation permissionEvaluator = new MapAccessPermissionEvaluation(readAdvice, updateAdvice); + expressionHandler.setPermissionEvaluator(permissionEvaluator); + return expressionHandler; + } + + @Bean + public PasswordEncoder passwordEncoder() { + return createDelegatingPasswordEncoder(); + } + + @Bean + public AuthenticationProvider googleAuthenticationProvider() { + return new GoogleAuthenticationProvider(userDetailsService); + + } + + @Bean + public AuthenticationProvider dbAuthenticationProvider() { + com.wisemapping.security.AuthenticationProvider provider = + new com.wisemapping.security.AuthenticationProvider(); + provider.setEncoder(passwordEncoder()); + provider.setUserDetailsService(userDetailsService); + return provider; + } + + @Bean + public AuthenticationManager authenticationManager(@NotNull HttpSecurity http) + throws Exception { + final AuthenticationManagerBuilder builder = http.getSharedObject(AuthenticationManagerBuilder.class); + builder.userDetailsService(userDetailsService) + .passwordEncoder(passwordEncoder()); + + builder.authenticationProvider(dbAuthenticationProvider()); + builder.authenticationProvider(googleAuthenticationProvider()); + + return builder.build(); + } +} diff --git a/wise-webapp/src/main/java/com/wisemapping/config/rest/RestAppConfig.java b/wise-webapp/src/main/java/com/wisemapping/config/rest/RestAppConfig.java index 1c5fa5f8..092fd7d6 100644 --- a/wise-webapp/src/main/java/com/wisemapping/config/rest/RestAppConfig.java +++ b/wise-webapp/src/main/java/com/wisemapping/config/rest/RestAppConfig.java @@ -21,7 +21,7 @@ import static org.springframework.security.config.Customizer.withDefaults; @SpringBootApplication @EnableWebSecurity -@ImportResource(value = {"classpath:spring/wisemapping-service.xml"}) +//@ImportResource(value = {"classpath:spring/wisemapping-rest.xml"}) @ComponentScan({"com.wisemapping.rest"}) public class RestAppConfig { @Bean @@ -55,4 +55,5 @@ public class RestAppConfig { return http.build(); } + } diff --git a/wise-webapp/src/main/java/com/wisemapping/security/GoogleAuthenticationProvider.java b/wise-webapp/src/main/java/com/wisemapping/security/GoogleAuthenticationProvider.java index 89e94077..71560c4c 100644 --- a/wise-webapp/src/main/java/com/wisemapping/security/GoogleAuthenticationProvider.java +++ b/wise-webapp/src/main/java/com/wisemapping/security/GoogleAuthenticationProvider.java @@ -1,5 +1,6 @@ package com.wisemapping.security; +import org.jetbrains.annotations.NotNull; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; @@ -11,52 +12,48 @@ public class GoogleAuthenticationProvider implements org.springframework.securit private UserDetailsService userDetailsService; - public UserDetailsService getUserDetailsService() { - return userDetailsService; - } - - public void setUserDetailsService(UserDetailsService userDetailsService) { + public GoogleAuthenticationProvider(@NotNull UserDetailsService userDetailsService) { this.userDetailsService = userDetailsService; } - /** - * Authenticate the given PreAuthenticatedAuthenticationToken. - * - * If the principal contained in the authentication object is null, the request will - * be ignored to allow other providers to authenticate it. - */ - @Override - public Authentication authenticate(Authentication inputToken) throws AuthenticationException { - if (!supports(inputToken.getClass())) { - return null; - } - if (inputToken.getPrincipal() == null) { - throw new BadCredentialsException("No pre-authenticated principal found in request."); - } - UserDetails userDetails = userDetailsService.loadUserByUsername(inputToken.getName()); + /** + * Authenticate the given PreAuthenticatedAuthenticationToken. + *

+ * If the principal contained in the authentication object is null, the request will + * be ignored to allow other providers to authenticate it. + */ + @Override + public Authentication authenticate(Authentication inputToken) throws AuthenticationException { + if (!supports(inputToken.getClass())) { + return null; + } + if (inputToken.getPrincipal() == null) { + throw new BadCredentialsException("No pre-authenticated principal found in request."); + } + UserDetails userDetails = userDetailsService.loadUserByUsername(inputToken.getName()); final User user = userDetails.getUser(); - if (!user.isActive()) { - throw new BadCredentialsException("User has been disabled for login " + inputToken.getName()); - } - - PreAuthenticatedAuthenticationToken resultToken = new PreAuthenticatedAuthenticationToken(userDetails, - inputToken.getCredentials(), userDetails.getAuthorities()); - resultToken.setDetails(userDetails); - - userDetailsService.getUserService().auditLogin(user); + if (!user.isActive()) { + throw new BadCredentialsException("User has been disabled for login " + inputToken.getName()); + } - return resultToken; - } + PreAuthenticatedAuthenticationToken resultToken = new PreAuthenticatedAuthenticationToken(userDetails, + inputToken.getCredentials(), userDetails.getAuthorities()); + resultToken.setDetails(userDetails); - /** - * Indicate that this provider only supports PreAuthenticatedAuthenticationToken - * (sub)classes. - */ - @Override - public final boolean supports(Class authentication) { - return PreAuthenticatedAuthenticationToken.class.isAssignableFrom(authentication); - } + userDetailsService.getUserService().auditLogin(user); + + return resultToken; + } + + /** + * Indicate that this provider only supports PreAuthenticatedAuthenticationToken + * (sub)classes. + */ + @Override + public final boolean supports(Class authentication) { + return PreAuthenticatedAuthenticationToken.class.isAssignableFrom(authentication); + } } diff --git a/wise-webapp/src/main/java/com/wisemapping/service/MailerService.java b/wise-webapp/src/main/java/com/wisemapping/service/MailerService.java index 93c512d2..d38d42ca 100644 --- a/wise-webapp/src/main/java/com/wisemapping/service/MailerService.java +++ b/wise-webapp/src/main/java/com/wisemapping/service/MailerService.java @@ -38,7 +38,7 @@ public final class MailerService { //~ Instance fields ...................................................................................... - @Autowired +// @Autowired private JavaMailSender mailSender; @Autowired diff --git a/wise-webapp/src/main/resources/application.properties b/wise-webapp/src/main/resources/application.properties index 743c64ee..1c7d734a 100755 --- a/wise-webapp/src/main/resources/application.properties +++ b/wise-webapp/src/main/resources/application.properties @@ -13,7 +13,7 @@ spring.sql.init.mode=always # LOG -logging.level.root=TRACE +logging.level.root=DEBUG logging.level.org.apache.tomcat=INFO ################################################################################## diff --git a/wise-webapp/src/main/resources/spring/wisemapping-mail.xml b/wise-webapp/src/main/resources/spring/wisemapping-mail.xml new file mode 100755 index 00000000..7cebddbe --- /dev/null +++ b/wise-webapp/src/main/resources/spring/wisemapping-mail.xml @@ -0,0 +1,15 @@ + + + + + + + + messages + + + + diff --git a/wise-webapp/src/main/resources/spring/wisemapping-service.xml b/wise-webapp/src/main/resources/spring/wisemapping-messages.xml similarity index 95% rename from wise-webapp/src/main/resources/spring/wisemapping-service.xml rename to wise-webapp/src/main/resources/spring/wisemapping-messages.xml index a37fb92e..7a097fe9 100755 --- a/wise-webapp/src/main/resources/spring/wisemapping-service.xml +++ b/wise-webapp/src/main/resources/spring/wisemapping-messages.xml @@ -28,6 +28,4 @@ - - diff --git a/wise-webapp/src/main/resources/spring/wisemapping-rest.xml b/wise-webapp/src/main/resources/spring/wisemapping-rest.xml index 8ee1830c..5c2da2db 100644 --- a/wise-webapp/src/main/resources/spring/wisemapping-rest.xml +++ b/wise-webapp/src/main/resources/spring/wisemapping-rest.xml @@ -49,4 +49,13 @@ + + + + + + messages + + + \ No newline at end of file diff --git a/wise-webapp/src/main/resources/spring/wisemapping-security-db.xml b/wise-webapp/src/main/resources/spring/wisemapping-security-db.xml deleted file mode 100644 index 695f7b62..00000000 --- a/wise-webapp/src/main/resources/spring/wisemapping-security-db.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/wise-webapp/src/main/resources/spring/wisemapping-security-ldap.xml b/wise-webapp/src/main/resources/spring/wisemapping-security-ldap.xml deleted file mode 100644 index 12691441..00000000 --- a/wise-webapp/src/main/resources/spring/wisemapping-security-ldap.xml +++ /dev/null @@ -1,55 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/wise-webapp/src/main/resources/spring/wisemapping-servlet.xml b/wise-webapp/src/main/resources/spring/wisemapping-servlet.xml deleted file mode 100644 index 136df01d..00000000 --- a/wise-webapp/src/main/resources/spring/wisemapping-servlet.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - - diff --git a/wise-webapp/src/main/webapp/WEB-INF/web.xml b/wise-webapp/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index 42ab6c1b..00000000 --- a/wise-webapp/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,131 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file