Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. 2017-07-28 06:58:46.926 INFO 6821 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/dashboard/create-project],methods=[POST]}" onto public java.lang.String org.myapp.controller.ProjectController.createProjectPost(org.myapp.form.ProjectForm)
  2.  
  3. package org.myapp.config;
  4.  
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.web.servlet.config.annotation.*;
  7.  
  8. @Configuration
  9. @EnableWebMvc
  10. public class MvcConfig extends WebMvcConfigurerAdapter {
  11.  
  12. private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
  13. "classpath:/static/", "classpath:/public/"
  14. };
  15.  
  16. @Override
  17. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  18. registry.addResourceHandler("/**")
  19. .addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
  20. }
  21.  
  22. }
  23.  
  24. package org.myapp.config;
  25.  
  26. import org.myapp.service.impl.UserDetailsServiceImpl;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.context.annotation.Bean;
  29. import org.springframework.context.annotation.Configuration;
  30. import org.springframework.http.HttpMethod;
  31. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  32. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  33. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  34. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  35. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  36. import org.springframework.security.crypto.password.PasswordEncoder;
  37. import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  38.  
  39.  
  40. @Configuration
  41. @EnableWebSecurity
  42. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  43.  
  44. private UserDetailsServiceImpl userDetailsService;
  45.  
  46. public WebSecurityConfig(UserDetailsServiceImpl userDetailsService) {
  47. this.userDetailsService = userDetailsService;
  48. }
  49.  
  50. @Override
  51. public void configure(HttpSecurity http) throws Exception {
  52. http
  53. .authorizeRequests()
  54. .antMatchers("/", "/home", "/about", "/register", "/css/**", "/js/**", "/images/**", "/fonts/**")
  55. .permitAll().anyRequest().authenticated()
  56. .and()
  57. .authorizeRequests()
  58. .antMatchers("/projects/project/{id}/**")
  59. .access("@projectSecurity.isOwner(authentication, #id)")
  60. .and()
  61. .authorizeRequests()
  62. .antMatchers(HttpMethod.POST,"/api/**")
  63. .anonymous()
  64. .and()
  65. .formLogin()
  66. .loginPage("/login")
  67. .defaultSuccessUrl("/dashboard")
  68. .permitAll()
  69. .and()
  70. .logout()
  71. .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
  72. .logoutSuccessUrl("/home")
  73. .permitAll()
  74. .and()
  75. .exceptionHandling()
  76. .accessDeniedPage("/error/403");
  77. }
  78.  
  79. @Bean
  80. public PasswordEncoder passwordEncoder() {
  81. return new BCryptPasswordEncoder();
  82. }
  83.  
  84. @Autowired
  85. public void configureGlobal(AuthenticationManagerBuilder managerBuilder) throws Exception {
  86. managerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
  87. }
  88. }
  89.  
  90. package org.myapp.controller;
  91.  
  92. import org.myapp.entity.Project;
  93. import org.myapp.form.ProjectForm;
  94. import org.myapp.service.ProjectService;
  95. import org.springframework.security.core.context.SecurityContextHolder;
  96. import org.springframework.security.core.userdetails.UserDetails;
  97. import org.springframework.stereotype.Controller;
  98. import org.springframework.ui.Model;
  99. import org.springframework.validation.annotation.Validated;
  100. import org.springframework.web.bind.annotation.*;
  101.  
  102. import javax.validation.Valid;
  103. import java.util.List;
  104.  
  105. @Controller
  106. @Validated
  107. public class ProjectController {
  108.  
  109. private ProjectService projectService;
  110.  
  111. @ModelAttribute("title")
  112. public String addTitle() {
  113. return "Project";
  114. }
  115.  
  116. @ModelAttribute("user")
  117. public UserDetails addUser() {
  118. Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
  119. if(principal instanceof UserDetails) {
  120. return (UserDetails) principal;
  121. }
  122.  
  123. return null;
  124. }
  125.  
  126. public ProjectController(ProjectService projectService) {
  127. this.projectService = projectService;
  128. }
  129.  
  130. @GetMapping("/dashboard/create-project")
  131. public String createProject(Model model) {
  132.  
  133. model.addAttribute("project_form", new ProjectForm());
  134. return "projects/create";
  135. }
  136.  
  137. @PostMapping("/dashboard/create-project")
  138. public String createProjectPost(@ModelAttribute("project_form") @Valid ProjectForm projectForm) {
  139. System.out.println(projectForm);
  140.  
  141. //Project project = projectService.saveProject(projectForm);
  142.  
  143. return "redirect:/dashboard";
  144. }
  145.  
  146. }
  147.  
  148. <#include "../dashboard/header.ftl">
  149.  
  150.  
  151. <div class="row">
  152. <form action="/dashboard/create-project" method="post">
  153. <input name="title" type="text">
  154. <input type="text" name="description">
  155. <input type="submit" value="send">
  156. </form>
  157. </div>
  158.  
  159. <#include "../dashboard/footer.ftl" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement