Advertisement
Guest User

Untitled

a guest
Aug 13th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.92 KB | None | 0 0
  1. @Controller
  2. @RequestMapping("/admin")
  3. public class AdminController {
  4.  
  5. @Autowired
  6. private StudentService studentService;
  7. private UserService userService;
  8.  
  9. @GetMapping("/allStudentsAdmin")
  10. public ModelAndView allStudentsForUser() {
  11. ModelAndView mv = new ModelAndView();
  12. List<Student> studentList = studentService.getAllStudents();
  13. mv.addObject("studentList", studentList);
  14. mv.setViewName("allStudentsAdmin");
  15. return mv;
  16. }
  17.  
  18. @GetMapping(value = "/deleteStudent/{id}")
  19. public ModelAndView deleteUserById(@PathVariable Long id) {
  20. studentService.deleteStudentById(id);
  21. ModelAndView mv = new ModelAndView("redirect:/admin/allStudentsAdmin");
  22. return mv;
  23. }
  24.  
  25. @GetMapping(value = "/editStudent/{id}")
  26. public ModelAndView displayEditUserForm(@PathVariable Long id) {
  27. ModelAndView mv = new ModelAndView("adminEditStudent");
  28. Student student = studentService.getStudentById(id);
  29. mv.addObject("headerMessage", "Редактирование студента");
  30. mv.addObject("student", student);
  31. return mv;
  32. }
  33.  
  34. @PostMapping(value = "/editStudent")
  35. public String saveEditedUser(
  36. @RequestParam("id") Long id,
  37. @RequestParam("name") String name,
  38. @RequestParam("surname") String surname,
  39. @RequestParam("avatar") MultipartFile file) {
  40. try {
  41. studentService.updateStudent(name, surname, file, studentService.getStudentById(id));
  42. } catch (FileSystemException ex) {
  43. ex.printStackTrace();
  44. } catch (IOException e) {
  45. return "redirect:/errors";
  46. }
  47.  
  48. return "redirect:/admin/allStudentsAdmin";
  49. }
  50.  
  51. @GetMapping(value = "/addStudentAdmin")
  52. public ModelAndView displayNewUserForm() {
  53. ModelAndView mv = new ModelAndView("addStudentAdmin");
  54. mv.addObject("headerMessage", "Add Student Details");
  55. mv.addObject("student", new Student());
  56. return mv;
  57. }
  58.  
  59. @PostMapping(value = "/addStudentAdmin")
  60. public String saveNewStudent(@RequestParam("name") @NonNull String name,
  61. @RequestParam("surname") @NonNull String surname,
  62. @RequestParam("avatar") MultipartFile file)
  63. throws IOException {
  64.  
  65. Student student = new Student();
  66. student.setSurname(surname);
  67. student.setName(name);
  68.  
  69. if (file != null && !file.isEmpty()) {
  70. student.setAvatar(studentService.saveAvatarImage(file).getName());
  71. }
  72. studentService.saveStudent(student);
  73. return "redirect:/admin/allStudentsAdmin";
  74. }
  75.  
  76. @GetMapping(value = "/addUser")
  77. public ModelAndView displayAddUserForm() {
  78. ModelAndView mv = new ModelAndView("addUser");
  79.  
  80. mv.addObject("user", new User());
  81. return mv;
  82. }
  83.  
  84. @PostMapping(value = "/addUser")
  85. public String saveNewUser(@RequestParam("login") @NonNull String login,
  86. @RequestParam("password") @NonNull String password,
  87. @RequestParam("role") @NonNull String role)
  88.  
  89. throws IOException {
  90.  
  91. User user = new User();
  92. user.setPassword(password);
  93. user.setLogin(login);
  94.  
  95.  
  96. userService.saveUser(user);
  97. return "redirect:/admin/allStudentsAdmin";
  98. }
  99.  
  100. }
  101.  
  102. <body>
  103.  
  104. <div class="add">
  105. <br>
  106. <br>
  107. <br>
  108.  
  109. <br>
  110. <center>
  111.  
  112.  
  113. <form:form method="POST" action="${pageContext.request.contextPath}/admin/addUser" enctype="multipart/form-data">
  114. <table>
  115.  
  116.  
  117. <tr>
  118. <td><label path="Login">Login</label></td>
  119. <td><input type="text" name="login"/></td>
  120. </tr>
  121.  
  122. <tr>
  123. <td><label path="Password">Password</label></td>
  124. <td><input type="text" name="password"/></td>
  125. </tr>
  126. <tr>
  127. <td><select path="role" name="nubexSelect" size="3" multiple form="nubexForm">
  128. <option>Admin</option>
  129. <option>User</option>
  130. </select></td>
  131.  
  132.  
  133.  
  134. <td><input class="btn btn-primary" type="submit" value="Submit"></td>
  135. </tr>
  136.  
  137. </table>
  138. </form:form>
  139. </center>
  140. </div>
  141. </body>
  142.  
  143. @Entity
  144. @Table(name = "user")
  145. public class User implements Serializable {
  146.  
  147. @Id
  148. @GeneratedValue(strategy = GenerationType.AUTO)
  149. private long id;
  150. private String login;
  151. private String password;
  152.  
  153. private String role;
  154.  
  155.  
  156.  
  157. public long getId() {
  158. return id;
  159. }
  160.  
  161. public void setId(long id) {
  162. this.id = id;
  163. }
  164.  
  165. public String getLogin() {
  166. return login;
  167. }
  168.  
  169. public void setLogin(String login) {
  170. this.login = login;
  171. }
  172.  
  173. public String getPassword() {
  174. return password;
  175. }
  176.  
  177. public void setPassword(String password) {
  178. this.password = password;
  179. }
  180.  
  181. public String getRole() {
  182. return role;
  183. }
  184.  
  185. public void setRole(String role) {
  186. this.role = role;
  187. }
  188.  
  189.  
  190.  
  191.  
  192. @Override
  193. public String toString() {
  194. return "Student{" +
  195. "id=" + id +
  196. ", login='" + login + ''' +
  197. ", password='" + password + ''' +
  198. ", role='" + role + ''' +
  199. '}';
  200. }
  201. }
  202.  
  203. @Repository
  204. public interface UserRepository extends CrudRepository<User, Long>{
  205.  
  206.  
  207.  
  208. }
  209.  
  210. public interface UserService {
  211.  
  212.  
  213.  
  214.  
  215.  
  216. boolean saveUser(User user);
  217.  
  218.  
  219.  
  220. User updateUser(String login, String password, String role, User targetUser) throws IOException;
  221.  
  222. }
  223.  
  224. @Service
  225. @Transactional
  226. public class UserServiceImpl implements UserService {
  227.  
  228. @Value("${storage.location}")
  229.  
  230. private String storageLocation;
  231.  
  232. private UserRepository repository;
  233.  
  234. public UserServiceImpl() {
  235.  
  236. }
  237.  
  238. @Autowired
  239. public UserServiceImpl(UserRepository repository) {
  240. super();
  241. this.repository = repository;
  242. }
  243.  
  244.  
  245.  
  246.  
  247.  
  248. @Override
  249. public boolean saveUser(User user) {
  250. try {
  251. repository.save(user);
  252. return true;
  253. } catch (Exception ex) {
  254. return false;
  255. }
  256. }
  257.  
  258. @Override
  259. public User updateUser(String login, String password, String role, User targetUser)
  260. throws IOException {
  261.  
  262. if (login != null && !login.equals(targetUser.getLogin())) {
  263.  
  264. targetUser.setLogin(login);
  265.  
  266. }
  267.  
  268. if (password != null && !password.equals(targetUser.getPassword())) {
  269.  
  270. targetUser.setPassword(password);
  271.  
  272. }
  273.  
  274. if (role != null && !role.equals(targetUser.getRole())) {
  275.  
  276. targetUser.setRole(role);
  277.  
  278. }
  279.  
  280.  
  281.  
  282. return targetUser;
  283.  
  284. }
  285.  
  286. @Configuration
  287. @EnableWebSecurity
  288. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  289.  
  290. @Override
  291. protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
  292. auth.inMemoryAuthentication()
  293. .withUser("admin").password(passwordEncoder().encode("1234")).roles("ADMIN")
  294. .and()
  295. .withUser("user").password(passwordEncoder().encode("user1234")).roles("USER")
  296. .and();
  297. }
  298.  
  299. @Override
  300. protected void configure(HttpSecurity http) throws Exception {
  301. http.authorizeRequests()
  302. .antMatchers("/admin/**").hasRole("ADMIN")
  303. .antMatchers("/user/**").hasRole("USER")
  304. .antMatchers("/**").permitAll()
  305. .and()
  306. .formLogin()
  307. .loginPage("/login")
  308. .defaultSuccessUrl("/allStudents")
  309. .and()
  310. .logout()
  311. .and()
  312. .csrf().disable();
  313. }
  314.  
  315. @Bean
  316. public PasswordEncoder passwordEncoder() {
  317. return new BCryptPasswordEncoder();
  318. }
  319.  
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement