0
Error in this loop of repetition. It is only taking the first value of the database. You’re just doing the consultation in the first field and you’re ignoring the others.
@Controller
// @RequestMapping("/login")
public class LoginController extends HttpServlet {
private static final long serialVersionUID = 1L;
private String username;
private String password;
@Autowired
Usuarios usuarios;
HttpServletRequest request;
HttpServletResponse response;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@RequestMapping("/login")
public ModelAndView logins() {
ModelAndView mv = new ModelAndView("/login");
mv.addObject(new Documento());
return mv;
}
@RequestMapping("/efetuaLogin")
public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
boolean validacao = false;
HttpSession sessao;
List<Usuario> usuariosCadastrados = usuarios.lista();
String username = request.getParameter("username");
String password = request.getParameter("password");
for (Usuario usuario : usuariosCadastrados) {
String loginbd = usuario.getUsername();
String senhabd = usuario.getPassword();
System.out.println("username do Formulario...:" + loginbd);
System.out.println("Senha do banco........:" + senhabd);
System.out.println("Senha do Formulario...:" + password);
System.out.println("username do Formulario...:" + username);
if (username.equals(loginbd) && password.equals(senhabd)) {
validacao = true;
}
if (validacao == true) {
return new ModelAndView("/documentos");
} else {
return new ModelAndView("hello");
}
}
return null;
}
}
@Service
public class LoginService {
@PersistenceContext
private EntityManager em;
@Autowired
private Usuarios usuarios;
public List<Usuario> lista() {
return em.createQuery("select u from Usuario u", Usuario.class).getResultList();
}
}
The method
lista
of your classLoginService
seems to be right. However, what you bring is theusuarios.lista()
, classUsuarios
and not of classLoginService
(and you didn’t put that method on your list in the question, so you can’t tell what or why it’s wrong). Also, you’re taking every user in the database and looking one by one to look up the correct login and password, which is inefficient. It would be much better to search the database only the user you want.– Victor Stafusa