e1305e8c
sfeutrie
projet Spring boo...
|
1
2
3
|
package com.PFE.ServerManager;
import org.springframework.beans.factory.annotation.Autowired;
|
66a8b43c
Antoine Duquenoy
Intégration du fr...
|
4
|
import org.springframework.http.HttpStatus;
|
e1305e8c
sfeutrie
projet Spring boo...
|
5
|
import org.springframework.stereotype.Controller;
|
e743b1b9
Antoine Duquenoy
Ajout d'un utilis...
|
6
|
import org.springframework.web.bind.annotation.*;
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
7
|
import org.springframework.web.multipart.MultipartFile;
|
d0a03db7
sfeutrie
restructuration d...
|
8
|
import org.springframework.web.servlet.ModelAndView;
|
57e157a4
sfeutrie
optimisation de l...
|
9
|
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
10
11
|
import java.io.*;
import java.sql.Timestamp;
|
115e3f68
sfeutrie
amélioration du f...
|
12
13
|
import java.util.*;
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
14
15
16
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.Authentication;
|
e1305e8c
sfeutrie
projet Spring boo...
|
17
|
|
66a8b43c
Antoine Duquenoy
Intégration du fr...
|
18
|
import javax.servlet.annotation.MultipartConfig;
|
57e157a4
sfeutrie
optimisation de l...
|
19
|
|
d0a03db7
sfeutrie
restructuration d...
|
20
|
@Controller
|
66a8b43c
Antoine Duquenoy
Intégration du fr...
|
21
|
@MultipartConfig(fileSizeThreshold = 20971520)
|
e1305e8c
sfeutrie
projet Spring boo...
|
22
|
public class MainController {
|
d0a03db7
sfeutrie
restructuration d...
|
23
24
|
@Autowired
|
e1305e8c
sfeutrie
projet Spring boo...
|
25
26
|
CustomerRepository customerRepository;
|
d0a03db7
sfeutrie
restructuration d...
|
27
28
29
|
@Autowired
RoleRepository roleRepository;
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
30
31
32
33
34
35
36
37
38
39
40
41
|
@Autowired
BCryptPasswordEncoder bCryptPasswordEncoder;
@GetMapping(value="/")
public String homeRedirection(){
return "redirect:home";
}
@GetMapping(value="/home")
public ModelAndView home() {
ModelAndView modelAndView = new ModelAndView();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
072e7fc2
Unknown
Passage de pseudo...
|
42
43
|
Customer customer = customerRepository.findByEmail(auth.getName());
modelAndView.addObject("customerName", customer.getEmail().split("@")[0]);
|
16791f79
sfeutrie
routage du site
|
44
|
modelAndView.addObject("customerRole", customer.getRole());
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
45
46
|
modelAndView.setViewName("home");
return modelAndView;
|
e1305e8c
sfeutrie
projet Spring boo...
|
47
48
|
}
|
deabfae9
sfeutrie
ajout d'une class...
|
49
50
51
52
53
54
55
56
57
58
59
|
@GetMapping(value="/session")
public ModelAndView session() {
ModelAndView modelAndView = new ModelAndView();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Customer customer = customerRepository.findByEmail(auth.getName());
modelAndView.addObject("customerName", customer.getEmail().split("@")[0]);
modelAndView.addObject("customerRole", customer.getRole());
modelAndView.setViewName("session");
return modelAndView;
}
|
d0a03db7
sfeutrie
restructuration d...
|
60
|
@GetMapping(path="/registration")
|
66a8b43c
Antoine Duquenoy
Intégration du fr...
|
61
62
63
|
public ModelAndView registration() {
ModelAndView modelAndView = new ModelAndView();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
072e7fc2
Unknown
Passage de pseudo...
|
64
65
|
Customer customer = customerRepository.findByEmail(auth.getName());
modelAndView.addObject("customerName", customer.getEmail().split("@")[0]);
|
115e3f68
sfeutrie
amélioration du f...
|
66
|
modelAndView.addObject("customerRole", customer.getRole());
|
66a8b43c
Antoine Duquenoy
Intégration du fr...
|
67
68
|
modelAndView.setViewName("registration");
return modelAndView;
|
e743b1b9
Antoine Duquenoy
Ajout d'un utilis...
|
69
70
|
}
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
71
72
73
74
75
|
@GetMapping(path="/denied")
public String denied() {
return "denied";
}
|
d0a03db7
sfeutrie
restructuration d...
|
76
|
@PostMapping(path="/registration")
|
072e7fc2
Unknown
Passage de pseudo...
|
77
|
public ModelAndView addNewUser(@RequestParam String email, @RequestParam String password, @RequestParam String role) {
|
d0a03db7
sfeutrie
restructuration d...
|
78
79
|
//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
ModelAndView modelAndView = new ModelAndView(); // il n'est peut être pas utile d'utiliser ModelAndView
|
e1305e8c
sfeutrie
projet Spring boo...
|
80
|
Customer n = new Customer();
|
072e7fc2
Unknown
Passage de pseudo...
|
81
|
n.setEmail(email);
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
82
|
n.setPassword(bCryptPasswordEncoder.encode(password));
|
072e7fc2
Unknown
Passage de pseudo...
|
83
|
n.setId((int)(customerRepository.count() + 1));
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
84
|
n.setActive(1);
|
072e7fc2
Unknown
Passage de pseudo...
|
85
|
Customer temp = customerRepository.findByEmail(email);
|
16791f79
sfeutrie
routage du site
|
86
|
Role userRole = roleRepository.findByRole(role);
|
57e157a4
sfeutrie
optimisation de l...
|
87
|
n.setRoles(new HashSet<Role>(Arrays.asList(userRole)));
|
115e3f68
sfeutrie
amélioration du f...
|
88
89
|
//utilisé uniquement pour continuer à afficher l'utilisateur connecté//
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
072e7fc2
Unknown
Passage de pseudo...
|
90
91
|
Customer customer = customerRepository.findByEmail(auth.getName());
modelAndView.addObject("customerName", customer.getEmail().split("@")[0]);
|
115e3f68
sfeutrie
amélioration du f...
|
92
93
|
modelAndView.addObject("customerRole", customer.getRole());
modelAndView.setViewName("registration");
|
072e7fc2
Unknown
Passage de pseudo...
|
94
|
|
e743b1b9
Antoine Duquenoy
Ajout d'un utilis...
|
95
|
if(temp != null) {
|
e23a6c00
Antoine Duquenoy
UI : modification...
|
96
97
|
modelAndView.addObject("message", "L'utilisateur existe déjà !");
modelAndView.addObject("fail", true);
|
e743b1b9
Antoine Duquenoy
Ajout d'un utilis...
|
98
|
}
|
d0a03db7
sfeutrie
restructuration d...
|
99
|
else {
|
e23a6c00
Antoine Duquenoy
UI : modification...
|
100
101
|
modelAndView.addObject("message", "L'utilisateur a bien été ajouté !");
modelAndView.addObject("ok", true);
|
d0a03db7
sfeutrie
restructuration d...
|
102
|
customerRepository.save(n);
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
103
|
}
|
d0a03db7
sfeutrie
restructuration d...
|
104
105
106
|
modelAndView.setViewName("registration");
return modelAndView;
}
|
e743b1b9
Antoine Duquenoy
Ajout d'un utilis...
|
107
|
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
108
109
110
111
112
113
114
115
|
@RequestMapping(value = "/file", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public void submit(
@RequestParam MultipartFile file, @RequestParam String nodes,
@RequestParam String filename) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
072e7fc2
Unknown
Passage de pseudo...
|
116
|
Customer customer = customerRepository.findByEmail(auth.getName());
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
117
|
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
|
072e7fc2
Unknown
Passage de pseudo...
|
118
|
File dirs = new File("files/" + customer.getEmail().split("@")[0] + "_" + timestamp.getTime());
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
dirs.mkdirs();
OutputStream outputStream = null;
InputStream inputStream = null;
try {
inputStream = file.getInputStream();
File newFile = new File(dirs.getPath() + "/" + file.getOriginalFilename());
if (!newFile.exists()) {
newFile.createNewFile();
}
outputStream = new FileOutputStream(newFile);
int read = 0;
byte[] bytes = new byte[1024];
while((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
outputStream.close();
}
catch(IOException e) {
}
}
|
66a8b43c
Antoine Duquenoy
Intégration du fr...
|
150
151
|
}
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
152
|
|
d0a03db7
sfeutrie
restructuration d...
|
153
|
@GetMapping(path="/login")
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
154
|
public ModelAndView login() {
|
d0a03db7
sfeutrie
restructuration d...
|
155
156
157
|
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login");
return modelAndView;
|
e1305e8c
sfeutrie
projet Spring boo...
|
158
|
}
|
e1305e8c
sfeutrie
projet Spring boo...
|
159
|
|
115e3f68
sfeutrie
amélioration du f...
|
160
|
/*@GetMapping(path="/all")
|
e1305e8c
sfeutrie
projet Spring boo...
|
161
|
public @ResponseBody Iterable<Customer> getAllUsers() {
|
e1305e8c
sfeutrie
projet Spring boo...
|
162
|
return customerRepository.findAll();
|
115e3f68
sfeutrie
amélioration du f...
|
163
164
165
166
167
168
169
|
}*/
@GetMapping(path="/all")
public ModelAndView getAllUsers() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("all");
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
072e7fc2
Unknown
Passage de pseudo...
|
170
171
|
Customer customer = customerRepository.findByEmail(auth.getName());
modelAndView.addObject("customerName", customer.getEmail().split("@")[0]);
|
115e3f68
sfeutrie
amélioration du f...
|
172
173
174
175
176
177
178
179
|
modelAndView.addObject("customerRole", customer.getRole());
/*List<Customer> list = new ArrayList<Customer>();
Iterator<Customer> listIterator = customerRepository.findAll().iterator();
while (listIterator.hasNext()) {
list.add(listIterator.next());
}*/
List<Customer> list = customerRepository.findAll(); // attention : la méthode findAll() de JpaRepository retourne une liste alors que celle de CrudRepository retourne un itérable
|
072e7fc2
Unknown
Passage de pseudo...
|
180
|
modelAndView.addObject("list", list);
|
115e3f68
sfeutrie
amélioration du f...
|
181
|
return modelAndView;
|
e1305e8c
sfeutrie
projet Spring boo...
|
182
|
}
|
d0a03db7
sfeutrie
restructuration d...
|
183
|
|
115e3f68
sfeutrie
amélioration du f...
|
184
|
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
185
|
@GetMapping(value="/success")
|
d0a03db7
sfeutrie
restructuration d...
|
186
187
188
|
public String success(){
return "success";
}
|
deabfae9
sfeutrie
ajout d'une class...
|
189
190
191
192
193
194
195
196
197
198
199
200
|
@GetMapping(path="/test")
public String testGet() {
return "test";
}
@PostMapping(path="/test")
public String test() {
//ModelAndView modelAndView = new ModelAndView();
//modelAndView.setViewName("test");
return "test";
}
|
e1305e8c
sfeutrie
projet Spring boo...
|
201
|
}
|