Commit d0a03db788fecf45e878f1a3b792dafddeb4654a

Authored by sfeutrie
1 parent e743b1b9

restructuration du site web avec une page d'accueil, de login et d'ajout d'utili…

…sateur et ajout de Spring Security pour l'instant pas opérationnel
PFE06/pom.xml
... ... @@ -3,8 +3,8 @@
3 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 4 <modelVersion>4.0.0</modelVersion>
5 5  
6   - <groupId>com.example</groupId>
7   - <artifactId>demo</artifactId>
  6 + <groupId>com.PFE</groupId>
  7 + <artifactId>ServerManager</artifactId>
8 8 <version>0.0.1-SNAPSHOT</version>
9 9 <packaging>jar</packaging>
10 10  
... ... @@ -33,7 +33,10 @@
33 33 <groupId>org.springframework.boot</groupId>
34 34 <artifactId>spring-boot-starter-web</artifactId>
35 35 </dependency>
36   -
  36 + <dependency>
  37 + <groupId>org.springframework.boot</groupId>
  38 + <artifactId>spring-boot-starter-security</artifactId>
  39 + </dependency>
37 40 <dependency>
38 41 <groupId>org.postgresql</groupId>
39 42 <artifactId>postgresql</artifactId>
... ...
PFE06/src/main/java/com/PFE/ServerManager/Customer.java
1 1 package com.PFE.ServerManager;
2 2  
3   -import javax.persistence.Column;
4   -import javax.persistence.Entity;
5   -import javax.persistence.GeneratedValue;
6   -import javax.persistence.GenerationType;
7   -import javax.persistence.Id;
8   -import javax.persistence.Table;
9   -
10   -@Entity // This tells Hibernate to make a table out of this class
11   -@Table(name = "Customer") // DON'T USE "User" because it is a reserved name in PostgreSQL
  3 +import javax.persistence.*;
  4 +
  5 +@Entity
  6 +@Table(name = "customer") // NE PAS utiliser "User" car c'est un mot clef réservé pour PostgreSQL
12 7 public class Customer{
13 8  
14 9 @Id
15 10 @GeneratedValue(strategy=GenerationType.AUTO)
16   - private Integer id;
  11 + private Integer customer_id;
17 12  
18 13 @Column(name = "pseudo")
19 14 private String pseudo;
... ... @@ -21,20 +16,17 @@ public class Customer{
21 16 @Column(name = "password")
22 17 private String password;
23 18  
24   - public Integer getId() {
25   - return id;
26   - }
  19 + @ManyToOne(cascade = CascadeType.ALL)
  20 + @JoinTable(name = "customer_role", joinColumns = @JoinColumn(name = "customer_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
  21 + //private Set<Role> roles;
  22 + private Role role;
27 23  
28   - public String getPseudo() {
29   - return pseudo;
30   - }
31   -
32   - public String getPassword() {
33   - return password;
  24 + public void setRole(Role role) {
  25 + this.role = role;
34 26 }
35 27  
36 28 public void setId(Integer id) {
37   - this.id = id;
  29 + this.customer_id = id;
38 30 }
39 31  
40 32 public void setPseudo(String pseudo) {
... ... @@ -44,5 +36,4 @@ public class Customer{
44 36 public void setPassword(String password) {
45 37 this.password = password;
46 38 }
47   -
48 39 }
49 40 \ No newline at end of file
... ...
PFE06/src/main/java/com/PFE/ServerManager/MainController.java
... ... @@ -3,44 +3,89 @@ package com.PFE.ServerManager;
3 3 import org.springframework.beans.factory.annotation.Autowired;
4 4 import org.springframework.stereotype.Controller;
5 5 import org.springframework.web.bind.annotation.*;
  6 +import org.springframework.web.servlet.ModelAndView;
6 7  
7   -import java.util.Map;
8   -
9   -@Controller // This means that this class is a Controller
  8 +@Controller
10 9 public class MainController {
11   - @Autowired // This means to get the bean called userRepository which is auto-generated by Spring, we will use it to handle the Customers
  10 +
  11 + @Autowired
12 12 CustomerRepository customerRepository;
13 13  
  14 + @Autowired
  15 + RoleRepository roleRepository;
  16 +
14 17 @RequestMapping(value="/")
15 18 public String home(){
16   - return "redirect:login";
  19 + return "home";
17 20 }
18 21  
19   - @GetMapping(path="/login") // Map ONLY GET Requests
20   - public String login() {
21   - return "login"; //return "redirect:/...."; //to send a request to redirect the current page
  22 + @GetMapping(path="/registration")
  23 + public String registration() {
  24 + return "registration";//fait le lien automatiquement avec le page html du même nom //return "redirect:/....";
22 25 }
23 26  
24   - @PostMapping(path="/login")
25   - public String addNewUser(@RequestParam String pseudo, @RequestParam String password) {
26   - // @RequestParam means it is a parameter from the GET or POST request
27   - //the model Map is used by thymeleaf as a storage for values display on the html page
  27 + @PostMapping(path="/registration")
  28 + public ModelAndView addNewUser(@RequestParam String pseudo, @RequestParam String password) {
  29 + //Model map, ModelAndView ou l'utilisation direct comme dans la méthode précédente sont 3 méthodes qui permettent d'envoyer des informations et donc de changer l'apparence d'une page
  30 + ModelAndView modelAndView = new ModelAndView(); // il n'est peut être pas utile d'utiliser ModelAndView
28 31 Customer n = new Customer();
29 32 n.setPseudo(pseudo);
30 33 n.setPassword(password);
31 34 Customer temp = customerRepository.findByPseudo(pseudo);
32 35  
  36 + /*Role nRole = roleRepository.findByRole("ADMIN");
  37 + n.setRoles(new HashSet<Role>(Arrays.asList(nRole)));*/
  38 + Role role = new Role(); // l'utilisation d'un role au lieu d'un tableau semble valide, ormis la première ligne de la table qui n'est pas utilisé
  39 + role.setRole("ADMIN");
  40 + n.setRole(role);
  41 +
33 42 if(temp != null) {
34   - return "redirect:login?error";
  43 + modelAndView.addObject("ok", "l'utilisateur existe déjà");
  44 + //return "login?fail";
35 45 }
  46 + else {
  47 + modelAndView.addObject("ok", "l'utilisateur a bien été ajouté");
  48 + customerRepository.save(n);
  49 + }
  50 + modelAndView.setViewName("registration");
  51 + return modelAndView;
  52 + }
36 53  
37   - customerRepository.save(n);
38   - return "redirect:login?ok";
  54 + @GetMapping(path="/login")
  55 + public ModelAndView login(){
  56 + ModelAndView modelAndView = new ModelAndView();
  57 + modelAndView.setViewName("login");
  58 + return modelAndView;
39 59 }
  60 + //////// Ne fonctionne pas /////////
  61 + /*public String login() {
  62 + return "login"; //return "redirect:/...."; //to send a request to redirect the current page
  63 + }*/
  64 + /*
  65 + @PostMapping(path="/login")
  66 + public ModelAndView connexion(@RequestParam String pseudo, @RequestParam String password) {
  67 + // @RequestParam means it is a parameter from the GET or POST request
  68 + //the model Map is used by thymeleaf as a storage for values display on the html page, this is the same way for ModelAndView
  69 + ModelAndView modelAndView = new ModelAndView();
  70 + Customer temp = customerRepository.findByPseudo(pseudo);
  71 + if(temp != null) {
  72 + modelAndView.addObject("error", "vous etes autorisé à être sur cette page");
  73 + }
  74 + else{
  75 + modelAndView.addObject("error", "vous n'etes pas autorisé à être sur cette page");
  76 + }
  77 + modelAndView.setViewName("login");
  78 + return modelAndView;
  79 + }*/
  80 + //////////////////////////
40 81  
41 82 @GetMapping(path="/all")
42 83 public @ResponseBody Iterable<Customer> getAllUsers() {
43   - // This returns a JSON or XML with the users
44 84 return customerRepository.findAll();
45 85 }
  86 +
  87 + @RequestMapping(value="/success")
  88 + public String success(){
  89 + return "success";
  90 + }
46 91 }
47 92 \ No newline at end of file
... ...
PFE06/src/main/java/com/PFE/ServerManager/Role.java 0 → 100644
... ... @@ -0,0 +1,24 @@
  1 +package com.PFE.ServerManager;
  2 +
  3 +import javax.persistence.Column;
  4 +import javax.persistence.Entity;
  5 +import javax.persistence.GeneratedValue;
  6 +import javax.persistence.GenerationType;
  7 +import javax.persistence.Id;
  8 +import javax.persistence.Table;
  9 +
  10 +@Entity
  11 +@Table(name = "role")
  12 +public class Role {
  13 + @Id
  14 + @GeneratedValue(strategy = GenerationType.AUTO)
  15 + @Column(name = "role_id")
  16 + private Integer role_id;
  17 +
  18 + @Column(name = "role")
  19 + private String role;
  20 +
  21 + public void setRole(String role) {
  22 + this.role = role;
  23 + }
  24 +}
0 25 \ No newline at end of file
... ...
PFE06/src/main/java/com/PFE/ServerManager/RoleRepository.java 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +package com.PFE.ServerManager;
  2 +
  3 +import org.springframework.data.jpa.repository.JpaRepository;
  4 +import org.springframework.data.repository.CrudRepository;
  5 +import org.springframework.stereotype.Repository;
  6 +
  7 +@Repository
  8 +public interface RoleRepository extends CrudRepository<Role, Integer> {
  9 + Role findByRole(String role);
  10 +}
  11 +
... ...
PFE06/src/main/java/com/PFE/ServerManager/SecurityConfig.java 0 → 100644
... ... @@ -0,0 +1,43 @@
  1 +package com.PFE.ServerManager;
  2 +
  3 +import org.springframework.beans.factory.annotation.Autowired;
  4 +import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  5 +import org.springframework.context.annotation.Configuration;
  6 +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  7 +import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  8 +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  9 +
  10 +import javax.sql.DataSource;
  11 +
  12 +@Configuration
  13 +@EnableAutoConfiguration
  14 +public class SecurityConfig extends WebSecurityConfigurerAdapter {
  15 +
  16 + @Autowired
  17 + DataSource dataSource;
  18 +
  19 + @Autowired
  20 + public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
  21 + auth.jdbcAuthentication()
  22 + .dataSource(dataSource)
  23 + .usersByUsernameQuery("select pseudo, password from customer where pseudo=?")
  24 + .authoritiesByUsernameQuery("select u.pseudo, r.role from customer u inner join customer_role ur on(u.customer_id=ur.customer_id) inner join role r on(ur.role_id=r.role_id) where u.pseudo=?");
  25 + }
  26 +
  27 + @Override
  28 + protected void configure(HttpSecurity http) throws Exception {
  29 + http
  30 + .authorizeRequests()
  31 + .antMatchers("/").permitAll()
  32 + .antMatchers("/registration").permitAll()
  33 + .antMatchers("/login").permitAll()
  34 + //.antMatchers("/registration").hasRole("ADMIN").anyRequest().authenticated()
  35 + .and()
  36 + .formLogin()
  37 + .loginPage("/login").failureUrl("/login?error=true").defaultSuccessUrl("/success")
  38 + .and()
  39 + .logout()
  40 + .permitAll();
  41 + //http.exceptionHandling().accessDeniedPage("/403");
  42 + }
  43 +}
0 44 \ No newline at end of file
... ...
PFE06/src/main/resources/application.properties
... ... @@ -2,9 +2,23 @@
2 2 #-----------------------------------------------#
3 3 #---------- Spring Database management ---------#
4 4 #-----------------------------------------------#
5   -spring.jpa.hibernate.ddl-auto=update
6   -#"create" if the database doesn't exist : it will reinitialize the DB every time the process is restarted
7   -#"update" if the database already exists
8   -spring.datasource.url=jdbc:postgresql://localhost:3306/sql_only
  5 +spring.jpa.hibernate.ddl-auto=create
  6 +#update
  7 +#"create" recrée la base de données à chaque lancement
  8 +#"update" met à jour la base données
  9 +
  10 +#Simon Postgres config :
  11 +spring.datasource.url=jdbc:postgresql://localhost:5432/sql_only
9 12 spring.datasource.username=postgres
10   -spring.datasource.password=admin
11 13 \ No newline at end of file
  14 +spring.datasource.password=idalurf123
  15 +
  16 +#Antoine Postgres config :
  17 +#spring.datasource.url=jdbc:postgresql://localhost:3302/sql_only
  18 +#spring.datasource.username=postgres
  19 +#spring.datasource.password=admin
  20 +
  21 +# montre les communications JPA avec la BDD
  22 +spring.jpa.show-sql = true
  23 +
  24 +# exécute le fichier data.sql pour préciser le role ADMIN
  25 +spring.datasource.initialization-mode=always
12 26 \ No newline at end of file
... ...
PFE06/src/main/resources/data.sql 0 → 100644
... ... @@ -0,0 +1,6 @@
  1 +/* ce fichier doit être placé dans les ressources afin d'être utilisé */
  2 +INSERT INTO "role" VALUES (1,'ADMIN');
  3 +/*INSERT INTO "customer" VALUES (10,1,'Feutrier','Simon');
  4 +INSERT INTO "customer" VALUES (11,1,'Duquenoy','Antoine');
  5 +INSERT INTO "customer_role" VALUES (1,1);
  6 +INSERT INTO "customer_role" VALUES (2,1);*/
0 7 \ No newline at end of file
... ...
PFE06/src/main/resources/templates/home.html 0 → 100644
... ... @@ -0,0 +1,12 @@
  1 +<!DOCTYPE html>
  2 +<html xmlns:th="http://www.thymeleaf.org">
  3 +<html lang="en">
  4 +<head>
  5 + <meta charset="UTF-8">
  6 + <title>Page d'accueil</title>
  7 +</head>
  8 +<body>
  9 +<a th:href="@{/login}">connexion</a>
  10 +<a th:href="@{/registration}">enregistrer des utilisateurs</a>
  11 +</body>
  12 +</html>
0 13 \ No newline at end of file
... ...
PFE06/src/main/resources/templates/login.html
1 1 <!DOCTYPE html>
2 2 <html xmlns:th="http://www.thymeleaf.org">
3 3 <head>
4   - <title>Add users page</title>
  4 + <title>Page de connexion</title>
5 5 <meta charset="utf-8"/>
6 6 </head>
7 7 <body>
8 8 <div>
9   - <h5>Add new users</h5>
  9 + <h5>Ajouter de nouveaux utilisateurs :</h5>
10 10 <form th:action="@{/login}" method="POST">
11 11 <div class="form1">
12 12 <label for="username">User Name: </label>
... ... @@ -16,14 +16,15 @@
16 16 <label for="password">Password: </label>
17 17 <input type="password" id="password" placeholder="Enter Password" name="password"/>
18 18 </div>
19   - <button type="submit">Ajouter</button>
  19 + <button type="submit">se connecter</button>
20 20 </form>
21   - <div th:if="${param.ok}">
  21 + <span th:utext="${error}"></span>
  22 + <!--<div th:if="${param.ok}">
22 23 <span>L'utilisateur a été ajouté !</span>
23 24 </div>
24   - <div th:if="${param.error}">
  25 + <div th:if="${param.fail}">
25 26 <span>Le pseudo existe déjà !</span>
26   - </div>
  27 + </div>-->
27 28 </div>
28 29 </body>
29 30 </html>
30 31 \ No newline at end of file
... ...
PFE06/src/main/resources/templates/registration.html 0 → 100644
... ... @@ -0,0 +1,31 @@
  1 +<!DOCTYPE html>
  2 +<html xmlns:th="http://www.thymeleaf.org">
  3 +<head>
  4 + <title>Ajout d'utilisateurs</title>
  5 + <meta charset="utf-8"/>
  6 +</head>
  7 +<body>
  8 +<div>
  9 + <h5>Ajout d'utilisateurs :</h5>
  10 + <form th:action="@{/registration}" method="POST">
  11 + <div class="form1">
  12 + <label for="username">User Name: </label>
  13 + <input type="text" id="username" placeholder="Enter UserName" name="pseudo"/>
  14 + </div>
  15 + <div class="form2">
  16 + <label for="password">Password: </label>
  17 + <input type="password" id="password" placeholder="Enter Password" name="password"/>
  18 + </div>
  19 + <button type="submit">Ajouter</button>
  20 + </form>
  21 + <span th:utext="${ok}"></span>
  22 + <a href="/login">connexion</a>
  23 + <!--<div th:if="${param.ok}">
  24 + <span>L'utilisateur a été ajouté !</span>
  25 + </div>
  26 + <div th:if="${param.fail}">
  27 + <span>Le pseudo existe déjà !</span>
  28 + </div>-->
  29 +</div>
  30 +</body>
  31 +</html>
0 32 \ No newline at end of file
... ...
PFE06/src/main/resources/templates/success.html 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +<!DOCTYPE html>
  2 +<html lang="en">
  3 +<head>
  4 + <meta charset="UTF-8">
  5 + <title>SUCCESS</title>
  6 +</head>
  7 +<body>
  8 +<a href="/login">connexion</a>
  9 +</body>
  10 +</html>
0 11 \ No newline at end of file
... ...