Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- package org.myapp.config;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.config.annotation.*;
- @Configuration
- @EnableWebMvc
- public class MvcConfig extends WebMvcConfigurerAdapter {
- private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
- "classpath:/static/", "classpath:/public/"
- };
- @Override
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry.addResourceHandler("/**")
- .addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
- }
- }
- package org.myapp.config;
- import org.myapp.service.impl.UserDetailsServiceImpl;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.http.HttpMethod;
- import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
- 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.WebSecurityConfigurerAdapter;
- import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
- import org.springframework.security.crypto.password.PasswordEncoder;
- import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
- @Configuration
- @EnableWebSecurity
- public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
- private UserDetailsServiceImpl userDetailsService;
- public WebSecurityConfig(UserDetailsServiceImpl userDetailsService) {
- this.userDetailsService = userDetailsService;
- }
- @Override
- public void configure(HttpSecurity http) throws Exception {
- http
- .authorizeRequests()
- .antMatchers("/", "/home", "/about", "/register", "/css/**", "/js/**", "/images/**", "/fonts/**")
- .permitAll().anyRequest().authenticated()
- .and()
- .authorizeRequests()
- .antMatchers("/projects/project/{id}/**")
- .access("@projectSecurity.isOwner(authentication, #id)")
- .and()
- .authorizeRequests()
- .antMatchers(HttpMethod.POST,"/api/**")
- .anonymous()
- .and()
- .formLogin()
- .loginPage("/login")
- .defaultSuccessUrl("/dashboard")
- .permitAll()
- .and()
- .logout()
- .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
- .logoutSuccessUrl("/home")
- .permitAll()
- .and()
- .exceptionHandling()
- .accessDeniedPage("/error/403");
- }
- @Bean
- public PasswordEncoder passwordEncoder() {
- return new BCryptPasswordEncoder();
- }
- @Autowired
- public void configureGlobal(AuthenticationManagerBuilder managerBuilder) throws Exception {
- managerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
- }
- }
- package org.myapp.controller;
- import org.myapp.entity.Project;
- import org.myapp.form.ProjectForm;
- import org.myapp.service.ProjectService;
- import org.springframework.security.core.context.SecurityContextHolder;
- import org.springframework.security.core.userdetails.UserDetails;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.validation.annotation.Validated;
- import org.springframework.web.bind.annotation.*;
- import javax.validation.Valid;
- import java.util.List;
- @Controller
- @Validated
- public class ProjectController {
- private ProjectService projectService;
- @ModelAttribute("title")
- public String addTitle() {
- return "Project";
- }
- @ModelAttribute("user")
- public UserDetails addUser() {
- Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
- if(principal instanceof UserDetails) {
- return (UserDetails) principal;
- }
- return null;
- }
- public ProjectController(ProjectService projectService) {
- this.projectService = projectService;
- }
- @GetMapping("/dashboard/create-project")
- public String createProject(Model model) {
- model.addAttribute("project_form", new ProjectForm());
- return "projects/create";
- }
- @PostMapping("/dashboard/create-project")
- public String createProjectPost(@ModelAttribute("project_form") @Valid ProjectForm projectForm) {
- System.out.println(projectForm);
- //Project project = projectService.saveProject(projectForm);
- return "redirect:/dashboard";
- }
- }
- <#include "../dashboard/header.ftl">
- <div class="row">
- <form action="/dashboard/create-project" method="post">
- <input name="title" type="text">
- <input type="text" name="description">
- <input type="submit" value="send">
- </form>
- </div>
- <#include "../dashboard/footer.ftl" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement