Na parte de criar os usuarios:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig{
@Autowired
private DataSource dataSource;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception{
http
.authorizeHttpRequests()
.anyRequest().authenticated()
.and()
.formLogin(form -> form
.loginPage("/login")
.defaultSuccessUrl("/home",true)
.permitAll())
.logout(logout -> logout.logoutUrl("/logout"));
return http.build();
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Bean
public JdbcUserDetailsManager users(PasswordEncoder encoder){
UserDetails user =
User.builder()
.username("joao")
.password(encoder.encode("joao"))
.roles("ADM")
.build();
JdbcUserDetailsManager jdbcUserDetailsManager = new JdbcUserDetailsManager(dataSource);
jdbcUserDetailsManager.createUser(user);
return jdbcUserDetailsManager;
}
}
Após criar usuarios
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests()
.anyRequest().authenticated()
.and()
.formLogin(form ->
form
.loginPage("/login")
.defaultSuccessUrl("/home", true)
.permitAll()
)
.logout(logout -> logout.logoutUrl("/logout"));
return http.build();
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Bean
public UserDetailsManager users(DataSource dataSource) {
JdbcUserDetailsManager users = new JdbcUserDetailsManager(dataSource);
return users;
}
}