Connexion page
This commit is contained in:
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|||||||
42
src/main/java/fr/eni/enchere/security/LoginService.java
Normal file
42
src/main/java/fr/eni/enchere/security/LoginService.java
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,8 @@
|
|||||||
<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="checkbox" id="remember-me" name="remember-me"/><label for="remember-me"> Se souvenir de moi</label></div>
|
||||||
|
<div><a href="/mot-de-passe-oublie">Mot de passe oublié</a></div>
|
||||||
<div><input type="submit" value="Connexion"/></div>
|
<div><input type="submit" value="Connexion"/></div>
|
||||||
</form>
|
</form>
|
||||||
<div th:if="${param.error}">
|
<div th:if="${param.error}">
|
||||||
@@ -18,6 +20,7 @@
|
|||||||
<div th:if="${param.logout}">
|
<div th:if="${param.logout}">
|
||||||
Vous avez été déconnecté
|
Vous avez été déconnecté
|
||||||
</div>
|
</div>
|
||||||
|
<div><a href="/inscription">Créer un compte</a></div>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Reference in New Issue
Block a user