SpringBoot: reslove circular dependancie

- remove lazy load
This commit is contained in:
djmil 2023-08-26 22:29:35 +02:00
parent 25b0b34b8d
commit 23560c8326
3 changed files with 14 additions and 22 deletions

View File

@ -1,7 +1,5 @@
package djmil.cordacheckers; 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.User;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
@ -12,13 +10,13 @@ import org.springframework.stereotype.Service;
@Service @Service
public class ApiUserDetailsService implements UserDetailsService { public class ApiUserDetailsService implements UserDetailsService {
@Autowired private final PasswordEncoder encoder;
@Lazy private final ApiUserShortHashService apiUserShortHash;
private PasswordEncoder encoder;
@Autowired public ApiUserDetailsService(PasswordEncoder encoder, ApiUserShortHashService apiUserShortHash) {
@Lazy this.encoder = encoder;
ApiUserShortHashService apiUserShortHash; this.apiUserShortHash = apiUserShortHash;
}
@Override @Override
public ApiUserDetails loadUserByUsername(String username) throws UsernameNotFoundException { public ApiUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

View File

@ -1,7 +1,5 @@
package djmil.cordacheckers; 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.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@ -13,12 +11,13 @@ import org.springframework.stereotype.Service;
@Service @Service
public class CustomAuthenticationProvider implements AuthenticationProvider { public class CustomAuthenticationProvider implements AuthenticationProvider {
@Autowired private final PasswordEncoder encoder;
@Lazy private final ApiUserDetailsService userDetailsService;
private PasswordEncoder encoder;
@Autowired public CustomAuthenticationProvider(PasswordEncoder encoder, ApiUserDetailsService userDetailsService) {
private ApiUserDetailsService userDetailsService; this.userDetailsService = userDetailsService;
this.encoder = encoder;
}
@Override @Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException { public Authentication authenticate(Authentication authentication) throws AuthenticationException {

View File

@ -1,6 +1,5 @@
package djmil.cordacheckers; package djmil.cordacheckers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
@ -22,11 +21,7 @@ public class SecurityConfig {
return PasswordEncoderFactories.createDelegatingPasswordEncoder(); return PasswordEncoderFactories.createDelegatingPasswordEncoder();
} }
@Autowired public void configure(AuthenticationManagerBuilder auth, CustomAuthenticationProvider authenticationProvider) throws Exception {
CustomAuthenticationProvider authenticationProvider;
@Autowired
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider); auth.authenticationProvider(authenticationProvider);
} }