Estou usando o banco de dados MySQL e ao tentar logar no sistema sempre recebo 'Bad credentials' como retorno.
package br.com.makode.makode.config.security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
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.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@EnableWebSecurity
@Configuration
public class SecurityConfigurations extends WebSecurityConfigurerAdapter {
@Autowired
private AutenticacaoService autenticacaoService;
//Cofiguração de autentificação
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(autenticacaoService).passwordEncoder(new BCryptPasswordEncoder());
}
//Configuração de autorização
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers(HttpMethod.GET,"/pessoas").permitAll()
.antMatchers(HttpMethod.GET,"/pessoas/*").permitAll()
.anyRequest().authenticated()
.and().formLogin();
}
//Configurações de recursos estaticos(js, css, imagens, etc)
@Override
public void configure(WebSecurity web) throws Exception {
}
public static void main(String[] args) {
System.out.println(new BCryptPasswordEncoder().encode("123456"));
}
}
package br.com.makode.makode.config.security; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; import br.com.makode.makode.model.Usuario; import br.com.makode.makode.repository.UsuarioRepository;
@Service public class AutenticacaoService implements UserDetailsService{
@Autowired
UsuarioRepository repository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
System.out.println("Nome do usuario: " + username);
Optional<Usuario> usuario = repository.findByDescricao(username);
System.out.println("Nome do usuario: " + usuario);
if(usuario.isPresent()) {
usuario.get();
System.out.println("usuario get: " + usuario.get().getDescricao());
System.out.println("usuario get: " + usuario.get().getSenha());
System.out.println("usuario get: " + usuario.get().getPassword());
}
throw new UsernameNotFoundException("Usuario invalido");
}
}
package br.com.makode.makode.model;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
@Entity
public class Usuario implements UserDetails {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String descricao;
private String usuario;
private String senha;
@ManyToMany(fetch = FetchType.EAGER)
private List<Perfil> perfis = new ArrayList<>();
package br.com.makode.makode.repository;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import br.com.makode.makode.model.Usuario;
public interface UsuarioRepository extends JpaRepository<Usuario,Long> {
Optional<Usuario> findByDescricao(String usuario);
}