From 23560c8326116b3c2e7d928eeab423c0635ffe03 Mon Sep 17 00:00:00 2001 From: djmil Date: Sat, 26 Aug 2023 22:29:35 +0200 Subject: [PATCH] SpringBoot: reslove circular dependancie - remove lazy load --- .../djmil/cordacheckers/ApiUserDetailsService.java | 14 ++++++-------- .../CustomAuthenticationProvider.java | 13 ++++++------- .../java/djmil/cordacheckers/SecurityConfig.java | 9 ++------- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/backend/src/main/java/djmil/cordacheckers/ApiUserDetailsService.java b/backend/src/main/java/djmil/cordacheckers/ApiUserDetailsService.java index fcb4455..2d25300 100644 --- a/backend/src/main/java/djmil/cordacheckers/ApiUserDetailsService.java +++ b/backend/src/main/java/djmil/cordacheckers/ApiUserDetailsService.java @@ -1,7 +1,5 @@ package djmil.cordacheckers; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Lazy; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; @@ -12,13 +10,13 @@ import org.springframework.stereotype.Service; @Service public class ApiUserDetailsService implements UserDetailsService { - @Autowired - @Lazy - private PasswordEncoder encoder; + private final PasswordEncoder encoder; + private final ApiUserShortHashService apiUserShortHash; - @Autowired - @Lazy - ApiUserShortHashService apiUserShortHash; + public ApiUserDetailsService(PasswordEncoder encoder, ApiUserShortHashService apiUserShortHash) { + this.encoder = encoder; + this.apiUserShortHash = apiUserShortHash; + } @Override public ApiUserDetails loadUserByUsername(String username) throws UsernameNotFoundException { diff --git a/backend/src/main/java/djmil/cordacheckers/CustomAuthenticationProvider.java b/backend/src/main/java/djmil/cordacheckers/CustomAuthenticationProvider.java index 58d4275..94cb707 100644 --- a/backend/src/main/java/djmil/cordacheckers/CustomAuthenticationProvider.java +++ b/backend/src/main/java/djmil/cordacheckers/CustomAuthenticationProvider.java @@ -1,7 +1,5 @@ package djmil.cordacheckers; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Lazy; import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; @@ -13,12 +11,13 @@ import org.springframework.stereotype.Service; @Service public class CustomAuthenticationProvider implements AuthenticationProvider { - @Autowired - @Lazy - private PasswordEncoder encoder; + private final PasswordEncoder encoder; + private final ApiUserDetailsService userDetailsService; - @Autowired - private ApiUserDetailsService userDetailsService; + public CustomAuthenticationProvider(PasswordEncoder encoder, ApiUserDetailsService userDetailsService) { + this.userDetailsService = userDetailsService; + this.encoder = encoder; + } @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { diff --git a/backend/src/main/java/djmil/cordacheckers/SecurityConfig.java b/backend/src/main/java/djmil/cordacheckers/SecurityConfig.java index 0fcf2c6..8982250 100644 --- a/backend/src/main/java/djmil/cordacheckers/SecurityConfig.java +++ b/backend/src/main/java/djmil/cordacheckers/SecurityConfig.java @@ -1,6 +1,5 @@ package djmil.cordacheckers; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; @@ -22,12 +21,8 @@ public class SecurityConfig { return PasswordEncoderFactories.createDelegatingPasswordEncoder(); } - @Autowired - CustomAuthenticationProvider authenticationProvider; - - @Autowired - public void configure(AuthenticationManagerBuilder auth) throws Exception { - auth.authenticationProvider(authenticationProvider); + public void configure(AuthenticationManagerBuilder auth, CustomAuthenticationProvider authenticationProvider) throws Exception { + auth.authenticationProvider(authenticationProvider); } // @Bean