simplify the configuration
This commit is contained in:
@@ -2,19 +2,16 @@ package io.reflectoring.passwordencoding.configuration;
|
|||||||
|
|
||||||
import io.reflectoring.passwordencoding.authentication.JdbcUserDetailPasswordService;
|
import io.reflectoring.passwordencoding.authentication.JdbcUserDetailPasswordService;
|
||||||
import io.reflectoring.passwordencoding.authentication.JdbcUserDetailsService;
|
import io.reflectoring.passwordencoding.authentication.JdbcUserDetailsService;
|
||||||
import io.reflectoring.passwordencoding.authentication.UserDetailsMapper;
|
|
||||||
import io.reflectoring.passwordencoding.authentication.UserRepository;
|
|
||||||
import io.reflectoring.passwordencoding.workfactor.BcCryptWorkFactorService;
|
import io.reflectoring.passwordencoding.workfactor.BcCryptWorkFactorService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.authentication.AuthenticationProvider;
|
||||||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
||||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
|
|
||||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
||||||
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;
|
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;
|
||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
|
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
|
||||||
@@ -29,17 +26,17 @@ import java.util.Map;
|
|||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
private final UserRepository userRepository;
|
|
||||||
private final UserDetailsMapper userDetailsMapper;
|
|
||||||
private final BcCryptWorkFactorService bcCryptWorkFactorService;
|
private final BcCryptWorkFactorService bcCryptWorkFactorService;
|
||||||
|
private final JdbcUserDetailsService jdbcUserDetailsService;
|
||||||
|
private final JdbcUserDetailPasswordService jdbcUserDetailPasswordService;
|
||||||
|
|
||||||
public SecurityConfiguration(
|
public SecurityConfiguration(
|
||||||
UserRepository userRepository,
|
BcCryptWorkFactorService bcCryptWorkFactorService,
|
||||||
UserDetailsMapper userDetailsMapper,
|
JdbcUserDetailsService jdbcUserDetailsService,
|
||||||
BcCryptWorkFactorService bcCryptWorkFactorService) {
|
JdbcUserDetailPasswordService jdbcUserDetailPasswordService) {
|
||||||
this.userRepository = userRepository;
|
|
||||||
this.userDetailsMapper = userDetailsMapper;
|
|
||||||
this.bcCryptWorkFactorService = bcCryptWorkFactorService;
|
this.bcCryptWorkFactorService = bcCryptWorkFactorService;
|
||||||
|
this.jdbcUserDetailsService = jdbcUserDetailsService;
|
||||||
|
this.jdbcUserDetailPasswordService = jdbcUserDetailPasswordService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -60,7 +57,8 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
|
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
|
||||||
auth.authenticationProvider(daoAuthenticationProvider()).eraseCredentials(false);
|
auth.authenticationProvider(daoAuthenticationProvider())
|
||||||
|
.eraseCredentials(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@@ -83,21 +81,12 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
|||||||
return new DelegatingPasswordEncoder(encodingId, encoders);
|
return new DelegatingPasswordEncoder(encodingId, encoders);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
|
||||||
public UserDetailsPasswordService userDetailsPasswordService() {
|
|
||||||
return new JdbcUserDetailPasswordService(userRepository, userDetailsMapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
public UserDetailsService userDetailsService() {
|
public AuthenticationProvider daoAuthenticationProvider() {
|
||||||
return new JdbcUserDetailsService(userRepository, userDetailsMapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public DaoAuthenticationProvider daoAuthenticationProvider() {
|
|
||||||
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
|
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
|
||||||
daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
|
daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
|
||||||
daoAuthenticationProvider.setUserDetailsPasswordService(userDetailsPasswordService());
|
daoAuthenticationProvider.setUserDetailsPasswordService(this.jdbcUserDetailPasswordService);
|
||||||
daoAuthenticationProvider.setUserDetailsService(userDetailsService());
|
daoAuthenticationProvider.setUserDetailsService(this.jdbcUserDetailsService);
|
||||||
return daoAuthenticationProvider;
|
return daoAuthenticationProvider;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ public class CarResources {
|
|||||||
@GetMapping("/cars")
|
@GetMapping("/cars")
|
||||||
public Set<Car> cars() {
|
public Set<Car> cars() {
|
||||||
return Set.of(
|
return Set.of(
|
||||||
Car.builder().name("vw").color("black").build(),
|
new Car("vw", "black"),
|
||||||
Car.builder().name("bmw").color("white").build());
|
new Car("bmw", "white"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user