Connexion page

This commit is contained in:
jleroy
2024-04-23 10:34:47 +02:00
parent 0a156ee2f7
commit 39efe3212f
9 changed files with 111 additions and 49 deletions

View File

@@ -1,12 +1,12 @@
package fr.eni.enchere.bll; package fr.eni.enchere.bll;
import fr.eni.enchere.bo.User; import fr.eni.enchere.bo.UserProfil;
import java.util.List; import java.util.List;
public interface UserService { public interface UserService {
List<User> listeUtilisateurs(); List<UserProfil> listeUtilisateurs();
User utilisateur(int id); UserProfil utilisateur(int id);
void setUtilisateur(User utilisateur); void setUtilisateur(UserProfil utilisateur);
void deleteUtilisateur(int id); void deleteUtilisateur(int id);
} }

View File

@@ -1,6 +1,6 @@
package fr.eni.enchere.bll; package fr.eni.enchere.bll;
import fr.eni.enchere.bo.User; import fr.eni.enchere.bo.UserProfil;
import fr.eni.enchere.dal.UserRepository; import fr.eni.enchere.dal.UserRepository;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -16,17 +16,17 @@ public class UserServiceImpl implements UserService {
} }
@Override @Override
public List<User> listeUtilisateurs() { public List<UserProfil> listeUtilisateurs() {
return userRepository.findAll(); return userRepository.findAll();
} }
@Override @Override
public User utilisateur(int id) { public UserProfil utilisateur(int id) {
return userRepository.findById(id); return userRepository.findById(id);
} }
@Override @Override
public void setUtilisateur(User utilisateur) { public void setUtilisateur(UserProfil utilisateur) {
userRepository.save(utilisateur); userRepository.save(utilisateur);
} }

View File

@@ -1,6 +1,6 @@
package fr.eni.enchere.bo; package fr.eni.enchere.bo;
public class User { public class UserProfil {
//Déclaration de variable //Déclaration de variable
private int id; private int id;
@@ -13,13 +13,13 @@ public class User {
private String code_postal; private String code_postal;
private String ville; private String ville;
private String password; //Voir la sécurité du mot de passe private String password; //Voir la sécurité du mot de passe
private int credit; private float credit;
private boolean isAdmin; private boolean isAdmin;
//Constructeur //Constructeur
public User(){} public UserProfil(){}
public User(int id, String pseudo, String nom, String prenom, String email, String telephone, String rue, String code_postal, String ville, String password, int credit, boolean isAdmin) { public UserProfil(int id, String pseudo, String nom, String prenom, String email, String telephone, String rue, String code_postal, String ville, String password, int credit, boolean isAdmin) {
setId(id); setId(id);
setPrenom(prenom); setPrenom(prenom);
setNom(nom); setNom(nom);
@@ -115,11 +115,11 @@ public class User {
this.password = password; this.password = password;
} }
public int getCredit() { public float getCredit() {
return credit; return credit;
} }
public void setCredit(int credit) { public void setCredit(float credit) {
this.credit = credit; this.credit = credit;
} }

View File

@@ -1,7 +1,7 @@
package fr.eni.enchere.controllers; package fr.eni.enchere.controllers;
import fr.eni.enchere.bll.UserService; import fr.eni.enchere.bll.UserService;
import fr.eni.enchere.bo.User; import fr.eni.enchere.bo.UserProfil;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@@ -21,12 +21,12 @@ public class InscriptionController {
@GetMapping @GetMapping
public String viewInscription(Model model) { public String viewInscription(Model model) {
model.addAttribute("user", new User()); model.addAttribute("user", new UserProfil());
return "inscription"; return "inscription";
} }
@PostMapping("/newUser") @PostMapping("/newUser")
public String setUser(@ModelAttribute User user) { public String setUser(@ModelAttribute UserProfil user) {
userService.setUtilisateur(user); userService.setUtilisateur(user);
return "redirect:/accueil"; return "redirect:/accueil";
} }

View File

@@ -1,12 +1,13 @@
package fr.eni.enchere.dal; package fr.eni.enchere.dal;
import fr.eni.enchere.bo.User; import fr.eni.enchere.bo.UserProfil;
import java.util.List; import java.util.List;
public interface UserRepository { public interface UserRepository {
List<User> findAll(); List<UserProfil> findAll();
User findById(int id); UserProfil findById(int id);
void save(User utilisateur); UserProfil findByUsername(String username);
void save(UserProfil utilisateur);
void delete(int id); void delete(int id);
} }

View File

@@ -1,6 +1,6 @@
package fr.eni.enchere.dal; package fr.eni.enchere.dal;
import fr.eni.enchere.bo.User; import fr.eni.enchere.bo.UserProfil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
@@ -10,7 +10,9 @@ import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder; import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
@Repository @Repository
@Primary @Primary
@@ -26,17 +28,41 @@ public class UserRepositoryImpl implements UserRepository {
} }
@Override @Override
public List<User> findAll() { public List<UserProfil> findAll() {
return List.of(); return List.of();
} }
@Override @Override
public User findById(int id) { public UserProfil findById(int id) {
return null; return null;
} }
@Override @Override
public void save(User utilisateur) { public UserProfil findByUsername(String username) {
String sql = "SELECT * FROM UTILISATEURS WHERE pseudo = :username OR email = :username";
Map<String, Object> params = new HashMap<>();
params.put("username", username);
UserProfil user = namedParameterJdbcTemplate.queryForObject(sql, params, (rs, rowNum) -> {
UserProfil userProfile = new UserProfil();
userProfile.setId(rs.getInt("no_utilisateur"));
userProfile.setPrenom(rs.getString("prenom"));
userProfile.setNom(rs.getString("nom"));
userProfile.setPseudo(rs.getString("pseudo"));
userProfile.setEmail(rs.getString("email"));
userProfile.setTelephone(rs.getString("telephone"));
userProfile.setRue(rs.getString("rue"));
userProfile.setCode_postal(rs.getString("code_postal"));
userProfile.setVille(rs.getString("ville"));
userProfile.setPassword(rs.getString("mot_de_passe"));
userProfile.setCredit(rs.getFloat("credit"));
userProfile.setAdmin(rs.getBoolean("administrateur"));
return userProfile;
});
return user;
}
@Override
public void save(UserProfil utilisateur) {
if (utilisateur.getId() == 0) { if (utilisateur.getId() == 0) {
String sql = "INSERT INTO UTILISATEURS (pseudo, nom, prenom, email, telephone, rue, code_postal, ville, mot_de_passe, credit, administrateur) VALUES (:pseudo, :nom, :prenom, :email, :telephone, :rue, :code_postal, :ville, :mot_de_passe, 0, false)"; String sql = "INSERT INTO UTILISATEURS (pseudo, nom, prenom, email, telephone, rue, code_postal, ville, mot_de_passe, credit, administrateur) VALUES (:pseudo, :nom, :prenom, :email, :telephone, :rue, :code_postal, :ville, :mot_de_passe, 0, false)";
MapSqlParameterSource parameters = new MapSqlParameterSource(); MapSqlParameterSource parameters = new MapSqlParameterSource();

View File

@@ -0,0 +1,42 @@
package fr.eni.enchere.security;
import fr.eni.enchere.bo.UserProfil;
import fr.eni.enchere.dal.UserRepository;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.User.UserBuilder;
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.Component;
@Component
public class LoginService implements UserDetailsService {
private UserRepository userRep;
public LoginService(UserRepository userRep) {
this.userRep = userRep;
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// Charger l'utilisateur depuis la base de données
UserProfil utilisateur = userRep.findByUsername(username);
// Créer un nouvel objet UserBuilder
UserBuilder userBuilder = User.builder()
.username(utilisateur.getPseudo())
.password(utilisateur.getPassword());
// Ajouter les rôles en fonction de isAdmin()
if(utilisateur.isAdmin()) {
userBuilder.roles("ADMIN", "MEMBRE");
} else {
userBuilder.roles("MEMBRE");
}
// Retourner l'utilisateur UserDetails construit
return userBuilder.build();
}
}

View File

@@ -4,8 +4,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher; import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
@@ -28,12 +26,4 @@ public class WebSecurityConfig {
return http.build(); return http.build();
} }
@Bean
public PasswordEncoder encoder() {
// Production :
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
// Dev/test
//return NoOpPasswordEncoder.getInstance();
}
} }

View File

@@ -4,20 +4,23 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
</head> </head>
<body> <body>
<div id="container-main"> <div id="container-main">
<h2>Pour se Connecter :</h2> <h2>Pour se Connecter :</h2>
<br> <br>
<form th:action="@{/login}" method="post"> <form th:action="@{/login}" method="post">
<div><label>Identifiant: <input type="text" name="username"/> </label></div> <div><label>Identifiant: <input type="text" name="username"/> </label></div>
<div><label>Mot de passe: <input type="password" name="password"/> </label></div> <div><label>Mot de passe: <input type="password" name="password"/> </label></div>
<div><input type="submit" value="Connexion"/></div> <div><input type="checkbox" id="remember-me" name="remember-me"/><label for="remember-me"> Se souvenir de moi</label></div>
</form> <div><a href="/mot-de-passe-oublie">Mot de passe oublié</a></div>
<div th:if="${param.error}"> <div><input type="submit" value="Connexion"/></div>
Identifiant ou mot de passe incorrect </form>
<div th:if="${param.error}">
Identifiant ou mot de passe incorrect
</div>
<div th:if="${param.logout}">
Vous avez été déconnecté
</div>
<div><a href="/inscription">Créer un compte</a></div>
</div> </div>
<div th:if="${param.logout}">
Vous avez été déconnecté
</div>
</div>
</body> </body>
</html> </html>