package fr.eni.enchere.controllers; import fr.eni.enchere.bll.UserService; import fr.eni.enchere.bo.UserProfil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class LoginController { @Autowired private UserService userService; public LoginController(UserService userService) { super(); this.userService = userService; } @GetMapping("/login") public String login(Model modele) { // Vérifier si l'utilisateur est déjà authentifié Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (!authentication.getName().equals("anonymousUser")){ return "redirect:/accueil"; } return "security/login"; } @PostMapping("/login") public String login(@RequestParam("username") String username, @RequestParam("password") String password) { UserProfil user = userService.utilisateurByName(username); if (user != null && user.getPassword().equals(password)) { return "redirect:/accueil"; } else { return "redirect:/security/login?error"; } } }