1
resposta

Não consigo realizar login (There is no PasswordEncoder mapped for the id "null")

toda vez que tenho realizar o login , eu tomo o seguinte erro

There is no PasswordEncoder mapped for the id "null"

segue as classes @Data public class User implements UserDetails{

@Id
@NonNull
private Long codigopessoa; 
@NonNull
private String login;
@NonNull
private String senha;
@NonNull
private String admin;

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
    // TODO Auto-generated method stub
    return null;
}
@Override
public String getPassword() {
    // TODO Auto-generated method stub
    return this.senha;
}

@Service

public class UserService implements UserDetailsService{

@Autowired
private UserRepository  userRepository;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

    User user =  userRepository.findByLogin(username)
            .orElseThrow(()-> new UsernameNotFoundException("Login não contrado"));



    return user;/*

    return org.springframework.security.core.userdetails.User
            .builder()
            .username(user.getLogin())
            .password("{noop}"+user.getSenha())
            .roles("USER").build();*/
}

@EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private UserService userService;

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(this.userService).passwordEncoder(new BCryptPasswordEncoder());

}

@Bean
public AuthenticationManager authenticationManager() throws Exception {
    return super.authenticationManager();
}

@Override
public void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().cors().and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
1 resposta

Oi Pedro,

Na sua classe SecurityConfig altere esse metodo:

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(this.userService).passwordEncoder(new BCryptPasswordEncoder());
}

Para:

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(this.userService).passwordEncoder(passwordEncoder());
}

E crie esse novo metodo nessa mesma classe:

@Bean
public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();    
}