BAEL-4282: Create PasswordEncoderConfiguration to fix the circular dependency issues (#9521)

This commit is contained in:
kwoyke 2020-06-19 15:02:53 +02:00 committed by GitHub
parent 5ad558c800
commit d89866aca7
6 changed files with 40 additions and 12 deletions

View File

@ -0,0 +1,15 @@
package com.baeldung.loginextrafieldscustom;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
public class PasswordEncoderConfiguration {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}

View File

@ -20,6 +20,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomUserDetailsService userDetailsService;
@Autowired
private PasswordEncoder passwordEncoder;
@Override
protected void configure(HttpSecurity http) throws Exception {
@ -49,7 +52,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
public AuthenticationProvider authProvider() {
CustomUserDetailsAuthenticationProvider provider
= new CustomUserDetailsAuthenticationProvider(passwordEncoder(), userDetailsService);
= new CustomUserDetailsAuthenticationProvider(passwordEncoder, userDetailsService);
return provider;
}
@ -57,8 +60,4 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
return new SimpleUrlAuthenticationFailureHandler("/login?error=true");
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.loginextrafieldssimple;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
public class PasswordEncoderConfiguration {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}

View File

@ -22,6 +22,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private PasswordEncoder passwordEncoder;
@Override
protected void configure(HttpSecurity http) throws Exception {
@ -52,7 +55,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
public AuthenticationProvider authProvider() {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setUserDetailsService(userDetailsService);
provider.setPasswordEncoder(passwordEncoder());
provider.setPasswordEncoder(passwordEncoder);
return provider;
}
@ -60,8 +63,4 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
return new SimpleUrlAuthenticationFailureHandler("/login?error=true");
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}

View File

@ -66,7 +66,7 @@ public class LoginFieldsFullIntegrationTest extends AbstractExtraLoginFieldsInte
private User getUser() {
Collection<? extends GrantedAuthority> authorities = new ArrayList<>();
return new User("myusername", "mydomain", "password", true, true, true, true, authorities);
return new User("myusername", "mydomain", "secret", true, true, true, true, authorities);
}
}

View File

@ -66,7 +66,7 @@ public class LoginFieldsSimpleIntegrationTest extends AbstractExtraLoginFieldsIn
private User getUser() {
Collection<? extends GrantedAuthority> authorities = new ArrayList<>();
return new User("myusername", "mydomain", "password", true, true, true, true, authorities);
return new User("myusername", "mydomain", "secret", true, true, true, true, authorities);
}
}