сдана 6
This commit is contained in:
parent
5bfc441e59
commit
0a86d54ff6
@ -16,7 +16,7 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur
|
|||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
@EnableGlobalMethodSecurity(securedEnabled = true)
|
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
|
||||||
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
||||||
private final Logger log = LoggerFactory.getLogger(SecurityConfiguration.class);
|
private final Logger log = LoggerFactory.getLogger(SecurityConfiguration.class);
|
||||||
private static final String LOGIN_URL = "/login";
|
private static final String LOGIN_URL = "/login";
|
||||||
|
@ -4,6 +4,8 @@ import com.example.demo.supply.Product.ProductDto;
|
|||||||
import com.example.demo.supply.Product.ProductService;
|
import com.example.demo.supply.Product.ProductService;
|
||||||
import com.example.demo.supply.Supplier.SupplierDto;
|
import com.example.demo.supply.Supplier.SupplierDto;
|
||||||
import com.example.demo.supply.Supplier.SupplierService;
|
import com.example.demo.supply.Supplier.SupplierService;
|
||||||
|
import com.example.demo.supply.User.UserRole;
|
||||||
|
import org.springframework.security.access.annotation.Secured;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
@ -55,6 +57,7 @@ public class OrderMvcController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/add")
|
@GetMapping("/add")
|
||||||
|
@Secured({UserRole.AsString.ADMIN})
|
||||||
public String addOrder(Model model) {
|
public String addOrder(Model model) {
|
||||||
model.addAttribute("orderDto", new OrderDtoForCreate());
|
model.addAttribute("orderDto", new OrderDtoForCreate());
|
||||||
model.addAttribute("selectedSupplier", null);
|
model.addAttribute("selectedSupplier", null);
|
||||||
@ -64,6 +67,7 @@ public class OrderMvcController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/create")
|
@PostMapping("/create")
|
||||||
|
@Secured({UserRole.AsString.ADMIN})
|
||||||
public String saveOrder(Model model,
|
public String saveOrder(Model model,
|
||||||
@ModelAttribute("orderDto") @Valid OrderDtoForCreate order,
|
@ModelAttribute("orderDto") @Valid OrderDtoForCreate order,
|
||||||
BindingResult bindingResult) {
|
BindingResult bindingResult) {
|
||||||
|
@ -1,11 +1,16 @@
|
|||||||
package com.example.demo.supply.Product;
|
package com.example.demo.supply.Product;
|
||||||
|
|
||||||
|
import com.example.demo.supply.User.UserRole;
|
||||||
|
import org.springframework.security.access.annotation.Secured;
|
||||||
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
import java.security.Principal;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("/product")
|
@RequestMapping("/product")
|
||||||
@ -25,7 +30,9 @@ public class ProductMvcController {
|
|||||||
|
|
||||||
@GetMapping(value = {"/edit", "/edit/{id}"})
|
@GetMapping(value = {"/edit", "/edit/{id}"})
|
||||||
public String editProduct(@PathVariable(required = false) Long id,
|
public String editProduct(@PathVariable(required = false) Long id,
|
||||||
Model model) {
|
Model model, Principal principal) {
|
||||||
|
String roleName = ((Authentication)principal).getAuthorities().toArray()[0].toString();
|
||||||
|
if(UserRole.ADMIN.toString().equals(roleName)) {
|
||||||
if (id == null || id <= 0) {
|
if (id == null || id <= 0) {
|
||||||
model.addAttribute("productDto", new ProductDto());
|
model.addAttribute("productDto", new ProductDto());
|
||||||
} else {
|
} else {
|
||||||
@ -34,12 +41,17 @@ public class ProductMvcController {
|
|||||||
}
|
}
|
||||||
return "product-edit";
|
return "product-edit";
|
||||||
}
|
}
|
||||||
|
else return "redirect:/product";
|
||||||
|
}
|
||||||
|
|
||||||
@PostMapping(value = {"", "/{id}"})
|
@PostMapping(value = {"", "/{id}"})
|
||||||
public String saveProduct(@PathVariable(required = false) Long id,
|
public String saveProduct(@PathVariable(required = false) Long id,
|
||||||
@ModelAttribute @Valid ProductDto productDto,
|
@ModelAttribute @Valid ProductDto productDto,
|
||||||
BindingResult bindingResult,
|
BindingResult bindingResult,
|
||||||
Model model) {
|
Model model,
|
||||||
|
Principal principal) {
|
||||||
|
String roleName = ((Authentication)principal).getAuthorities().toArray()[0].toString();
|
||||||
|
if(UserRole.ADMIN.toString().equals(roleName)) {
|
||||||
if (bindingResult.hasErrors()) {
|
if (bindingResult.hasErrors()) {
|
||||||
model.addAttribute("errors", bindingResult.getAllErrors());
|
model.addAttribute("errors", bindingResult.getAllErrors());
|
||||||
return "product-edit";
|
return "product-edit";
|
||||||
@ -51,10 +63,16 @@ public class ProductMvcController {
|
|||||||
}
|
}
|
||||||
return "redirect:/product";
|
return "redirect:/product";
|
||||||
}
|
}
|
||||||
|
else return "redirect:/product";
|
||||||
|
}
|
||||||
|
|
||||||
@PostMapping("/delete/{id}")
|
@PostMapping("/delete/{id}")
|
||||||
public String deleteProduct(@PathVariable Long id) {
|
public String deleteProduct(@PathVariable Long id, Principal principal) {
|
||||||
|
String roleName = ((Authentication)principal).getAuthorities().toArray()[0].toString();
|
||||||
|
if(UserRole.ADMIN.toString().equals(roleName)) {
|
||||||
productService.deleteProduct(id);
|
productService.deleteProduct(id);
|
||||||
return "redirect:/product";
|
return "redirect:/product";
|
||||||
}
|
}
|
||||||
|
else return "redirect:/product";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package com.example.demo.supply.User;
|
|||||||
|
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.security.access.annotation.Secured;
|
import org.springframework.security.access.annotation.Secured;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
@ -9,6 +10,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
|
|
||||||
|
import java.security.Principal;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
@ -22,10 +24,12 @@ public class UserMvcController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
@Secured({UserRole.AsString.ADMIN})
|
|
||||||
public String getUsers(@RequestParam(defaultValue = "1") int page,
|
public String getUsers(@RequestParam(defaultValue = "1") int page,
|
||||||
@RequestParam(defaultValue = "5") int size,
|
@RequestParam(defaultValue = "5") int size,
|
||||||
Model model) {
|
Model model, Principal principal) {
|
||||||
|
|
||||||
|
String roleName = ((Authentication)principal).getAuthorities().toArray()[0].toString();
|
||||||
|
if(UserRole.ADMIN.toString().equals(roleName)) {
|
||||||
final Page<UserDto> users = userService.findAllPages(page, size)
|
final Page<UserDto> users = userService.findAllPages(page, size)
|
||||||
.map(UserDto::new);
|
.map(UserDto::new);
|
||||||
model.addAttribute("users", users);
|
model.addAttribute("users", users);
|
||||||
@ -37,4 +41,9 @@ public class UserMvcController {
|
|||||||
model.addAttribute("totalPages", totalPages);
|
model.addAttribute("totalPages", totalPages);
|
||||||
return "users";
|
return "users";
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
model.addAttribute("error", "Доступ запрещен");
|
||||||
|
return "error";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,7 @@
|
|||||||
<a class="nav-link" href="/logout">
|
<a class="nav-link" href="/logout">
|
||||||
Выход (<span th:text="${#authentication.name}"></span>)
|
Выход (<span th:text="${#authentication.name}"></span>)
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
14
demo/src/main/resources/templates/error.html
Normal file
14
demo/src/main/resources/templates/error.html
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en"
|
||||||
|
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||||
|
xmlns:th="http://www.thymeleaf.org"
|
||||||
|
layout:decorate="~{default}">
|
||||||
|
<body>
|
||||||
|
<div class="container" layout:fragment="content">
|
||||||
|
<div class="alert alert-danger">
|
||||||
|
<span th:text="${error}"></span>
|
||||||
|
</div>
|
||||||
|
<a href="/">На главную</a>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -1,6 +1,8 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en"
|
<html lang="en"
|
||||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.thymeleaf.org"
|
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||||
|
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity6"
|
||||||
|
xmlns:th="http://www.thymeleaf.org"
|
||||||
layout:decorate="~{default}">
|
layout:decorate="~{default}">
|
||||||
<head>
|
<head>
|
||||||
</head>
|
</head>
|
||||||
|
Loading…
Reference in New Issue
Block a user