Initial commit
This commit is contained in:
commit
ded235bde0
46 files changed
+3489
No files matched your search
@@ -0,0 +1,13 @@
|
||||
package com.example.test_security2db;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class TestSecurity2dbApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestSecurity2dbApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.example.test_security2db.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class MvcConfig implements WebMvcConfigurer {
|
||||
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.example.test_security2db.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class WebSecurityConfig {
|
||||
|
||||
@Bean
|
||||
public static PasswordEncoder passwordEncoder() {
|
||||
return new BCryptPasswordEncoder();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.csrf(csrf -> csrf.disable())
|
||||
.authorizeHttpRequests(auth -> auth
|
||||
.requestMatchers("/register/**", "/", "/index", "/about").permitAll()
|
||||
.requestMatchers("/users", "/roles/**").hasRole("ADMIN")
|
||||
.anyRequest().authenticated()
|
||||
)
|
||||
.formLogin(
|
||||
form -> form
|
||||
.loginPage("/login")
|
||||
.loginProcessingUrl("/login")
|
||||
.defaultSuccessUrl("/index")
|
||||
.permitAll()
|
||||
).logout(
|
||||
logout -> logout
|
||||
.logoutRequestMatcher(PathPatternRequestMatcher.pathPattern("/logout"))
|
||||
.permitAll()
|
||||
);
|
||||
return http.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.example.test_security2db.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import com.example.test_security2db.entity.Book;
|
||||
import com.example.test_security2db.repository.BookRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Controller
|
||||
public class BookController {
|
||||
|
||||
@Autowired
|
||||
private BookRepository bookRepository;
|
||||
|
||||
@GetMapping("/books")
|
||||
public ModelAndView getAllBooks() {
|
||||
ModelAndView mav = new ModelAndView("list-books");
|
||||
mav.addObject("books", bookRepository.findAll());
|
||||
return mav;
|
||||
}
|
||||
|
||||
@GetMapping("/addBookForm")
|
||||
public ModelAndView addBookForm() {
|
||||
ModelAndView mav = new ModelAndView("add-book-form");
|
||||
Book book = new Book();
|
||||
mav.addObject("book", book);
|
||||
return mav;
|
||||
}
|
||||
|
||||
@PostMapping("/saveBook")
|
||||
public String saveBook(@ModelAttribute Book book) {
|
||||
bookRepository.save(book);
|
||||
return "redirect:/books";
|
||||
}
|
||||
|
||||
@GetMapping("/showBookUpdateForm")
|
||||
public ModelAndView showUpdateForm(@RequestParam Long bookId) {
|
||||
ModelAndView mav = new ModelAndView("add-book-form");
|
||||
Optional<Book> optionalBook = bookRepository.findById(bookId);
|
||||
Book book = new Book();
|
||||
if (optionalBook.isPresent()) {
|
||||
book = optionalBook.get();
|
||||
}
|
||||
mav.addObject("book", book);
|
||||
return mav;
|
||||
}
|
||||
|
||||
@GetMapping("/deleteBook")
|
||||
public String deleteBook(@RequestParam Long bookId) {
|
||||
bookRepository.deleteById(bookId);
|
||||
return "redirect:/books";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.example.test_security2db.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import com.example.test_security2db.entity.Book;
|
||||
import com.example.test_security2db.entity.BookDetail;
|
||||
import com.example.test_security2db.repository.BookDetailRepository;
|
||||
import com.example.test_security2db.repository.BookRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Controller
|
||||
public class BookDetailController {
|
||||
|
||||
@Autowired
|
||||
private BookDetailRepository bookDetailRepository;
|
||||
|
||||
@Autowired
|
||||
private BookRepository bookRepository;
|
||||
|
||||
@GetMapping("/details")
|
||||
public ModelAndView getAllDetails() {
|
||||
ModelAndView mav = new ModelAndView("list-details");
|
||||
mav.addObject("details", bookDetailRepository.findAll());
|
||||
return mav;
|
||||
}
|
||||
|
||||
@GetMapping("/addDetailForm")
|
||||
public ModelAndView addDetailForm() {
|
||||
ModelAndView mav = new ModelAndView("add-detail-form");
|
||||
mav.addObject("detail", new BookDetail());
|
||||
mav.addObject("books", bookRepository.findAll());
|
||||
return mav;
|
||||
}
|
||||
|
||||
@PostMapping("/saveDetail")
|
||||
public String saveDetail(@ModelAttribute BookDetail detail,
|
||||
@RequestParam Long bookId) {
|
||||
Optional<Book> optionalBook = bookRepository.findById(bookId);
|
||||
if (optionalBook.isPresent()) {
|
||||
detail.setBook(optionalBook.get());
|
||||
}
|
||||
bookDetailRepository.save(detail);
|
||||
return "redirect:/details";
|
||||
}
|
||||
|
||||
@GetMapping("/showDetailUpdateForm")
|
||||
public ModelAndView showUpdateForm(@RequestParam Long detailId) {
|
||||
ModelAndView mav = new ModelAndView("add-detail-form");
|
||||
Optional<BookDetail> optionalDetail = bookDetailRepository.findById(detailId);
|
||||
BookDetail detail = new BookDetail();
|
||||
if (optionalDetail.isPresent()) {
|
||||
detail = optionalDetail.get();
|
||||
}
|
||||
mav.addObject("detail", detail);
|
||||
mav.addObject("books", bookRepository.findAll());
|
||||
return mav;
|
||||
}
|
||||
|
||||
@GetMapping("/deleteDetail")
|
||||
public String deleteDetail(@RequestParam Long detailId) {
|
||||
bookDetailRepository.deleteById(detailId);
|
||||
return "redirect:/details";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.example.test_security2db.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@Controller
|
||||
public class MainController {
|
||||
|
||||
@GetMapping({"/", "/index"})
|
||||
public String home() {
|
||||
return "index";
|
||||
}
|
||||
|
||||
@GetMapping("/about")
|
||||
public String about() {
|
||||
return "about";
|
||||
}
|
||||
|
||||
@GetMapping("/calc")
|
||||
public String calc() {
|
||||
return "calc";
|
||||
}
|
||||
|
||||
@PostMapping("/calc")
|
||||
public String calcResult(@RequestParam double price,
|
||||
@RequestParam int count,
|
||||
@RequestParam double discount,
|
||||
Model model) {
|
||||
double result = price * count * (1 - discount / 100);
|
||||
model.addAttribute("result", result);
|
||||
return "calc";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.example.test_security2db.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import com.example.test_security2db.entity.Role;
|
||||
import com.example.test_security2db.entity.User;
|
||||
import com.example.test_security2db.repository.RoleRepository;
|
||||
import com.example.test_security2db.repository.UserRepository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Controller
|
||||
public class RoleController {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private RoleRepository roleRepository;
|
||||
|
||||
@GetMapping("/roles")
|
||||
public ModelAndView roles() {
|
||||
ModelAndView mav = new ModelAndView("roles");
|
||||
mav.addObject("users", userRepository.findAll());
|
||||
return mav;
|
||||
}
|
||||
|
||||
@PostMapping("/saveRole")
|
||||
public String saveRole(@RequestParam Long userId,
|
||||
@RequestParam(required = false) List<String> roles) {
|
||||
Optional<User> optionalUser = userRepository.findById(userId);
|
||||
if (optionalUser.isPresent()) {
|
||||
User user = optionalUser.get();
|
||||
List<Role> newRoles = new ArrayList<>();
|
||||
if (roles != null) {
|
||||
for (String name : roles) {
|
||||
Role role = roleRepository.findByName(name);
|
||||
if (role == null) {
|
||||
role = new Role();
|
||||
role.setName(name);
|
||||
role = roleRepository.save(role);
|
||||
}
|
||||
newRoles.add(role);
|
||||
}
|
||||
}
|
||||
user.setRoles(newRoles);
|
||||
userRepository.save(user);
|
||||
}
|
||||
return "redirect:/users";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.example.test_security2db.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import com.example.test_security2db.dto.UserDto;
|
||||
import com.example.test_security2db.entity.User;
|
||||
import com.example.test_security2db.service.UserService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import java.util.List;
|
||||
|
||||
@Controller
|
||||
public class SecurityController {
|
||||
|
||||
private UserService userService;
|
||||
|
||||
public SecurityController(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@GetMapping("/login")
|
||||
public String login() {
|
||||
return "login";
|
||||
}
|
||||
|
||||
@GetMapping("/register")
|
||||
public String showRegistrationForm(Model model) {
|
||||
UserDto user = new UserDto();
|
||||
model.addAttribute("user", user);
|
||||
return "register";
|
||||
}
|
||||
|
||||
@PostMapping("/register/save")
|
||||
public String registration(@Valid @ModelAttribute("user") UserDto userDto,
|
||||
BindingResult result,
|
||||
Model model) {
|
||||
User existingUser = userService.findUserByEmail(userDto.getEmail());
|
||||
|
||||
if (existingUser != null && existingUser.getEmail() != null && !existingUser.getEmail().isEmpty()) {
|
||||
result.rejectValue("email", null,
|
||||
"На этот адрес электронной почты уже зарегистрирована учетная запись.");
|
||||
}
|
||||
|
||||
if (result.hasErrors()) {
|
||||
model.addAttribute("user", userDto);
|
||||
return "/register";
|
||||
}
|
||||
|
||||
userService.saveUser(userDto);
|
||||
return "redirect:/register?success";
|
||||
}
|
||||
|
||||
@GetMapping("/users")
|
||||
public String users(Model model) {
|
||||
List<UserDto> users = userService.findAllUsers();
|
||||
model.addAttribute("users", users);
|
||||
return "users";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.example.test_security2db.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import com.example.test_security2db.entity.Shop;
|
||||
import com.example.test_security2db.repository.ShopRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Controller
|
||||
public class ShopController {
|
||||
|
||||
@Autowired
|
||||
private ShopRepository shopRepository;
|
||||
|
||||
@GetMapping("/shops")
|
||||
public ModelAndView getAllShops() {
|
||||
ModelAndView mav = new ModelAndView("list-shops");
|
||||
mav.addObject("shops", shopRepository.findAll());
|
||||
return mav;
|
||||
}
|
||||
|
||||
@GetMapping("/addShopForm")
|
||||
public ModelAndView addShopForm() {
|
||||
ModelAndView mav = new ModelAndView("add-shop-form");
|
||||
Shop shop = new Shop();
|
||||
mav.addObject("shop", shop);
|
||||
return mav;
|
||||
}
|
||||
|
||||
@PostMapping("/saveShop")
|
||||
public String saveShop(@ModelAttribute Shop shop) {
|
||||
shopRepository.save(shop);
|
||||
return "redirect:/shops";
|
||||
}
|
||||
|
||||
@GetMapping("/showShopUpdateForm")
|
||||
public ModelAndView showUpdateForm(@RequestParam Long shopId) {
|
||||
ModelAndView mav = new ModelAndView("add-shop-form");
|
||||
Optional<Shop> optionalShop = shopRepository.findById(shopId);
|
||||
Shop shop = new Shop();
|
||||
if (optionalShop.isPresent()) {
|
||||
shop = optionalShop.get();
|
||||
}
|
||||
mav.addObject("shop", shop);
|
||||
return mav;
|
||||
}
|
||||
|
||||
@GetMapping("/deleteShop")
|
||||
public String deleteShop(@RequestParam Long shopId) {
|
||||
shopRepository.deleteById(shopId);
|
||||
return "redirect:/shops";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.example.test_security2db.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import jakarta.validation.constraints.Email;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class UserDto {
|
||||
private Long id;
|
||||
|
||||
@NotEmpty
|
||||
private String firstName;
|
||||
|
||||
@NotEmpty
|
||||
private String lastName;
|
||||
|
||||
@NotEmpty(message = "Email should not be empty")
|
||||
@Email
|
||||
private String email;
|
||||
|
||||
@NotEmpty(message = "Password should not be empty")
|
||||
private String password;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.example.test_security2db.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "books")
|
||||
public class Book {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id")
|
||||
private long id;
|
||||
|
||||
@Column(name = "title")
|
||||
private String title;
|
||||
|
||||
@Column(name = "author")
|
||||
private String author;
|
||||
|
||||
@Column(name = "price")
|
||||
private double price;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.example.test_security2db.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "book_details")
|
||||
public class BookDetail {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
@Column(name = "description")
|
||||
private String description;
|
||||
|
||||
@OneToOne
|
||||
@JoinColumn(name = "book_id")
|
||||
private Book book;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.example.test_security2db.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "roles")
|
||||
public class Role {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false, unique = true)
|
||||
private String name;
|
||||
|
||||
@ManyToMany(mappedBy = "roles")
|
||||
private List<User> users = new ArrayList<>();
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.example.test_security2db.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "shops")
|
||||
public class Shop {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id")
|
||||
private long id;
|
||||
|
||||
@Column(name = "name")
|
||||
private String name;
|
||||
|
||||
@Column(name = "city")
|
||||
private String city;
|
||||
|
||||
@Column(name = "address")
|
||||
private String address;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.example.test_security2db.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
@Column(nullable = false, unique = true)
|
||||
private String email;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String password;
|
||||
|
||||
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
|
||||
@JoinTable(
|
||||
name = "users_roles",
|
||||
joinColumns = { @JoinColumn(name = "user_id", referencedColumnName = "id") },
|
||||
inverseJoinColumns = { @JoinColumn(name = "role_id", referencedColumnName = "id") }
|
||||
)
|
||||
private List<Role> roles = new ArrayList<>();
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.test_security2db.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import com.example.test_security2db.entity.BookDetail;
|
||||
|
||||
@Repository
|
||||
public interface BookDetailRepository extends JpaRepository<BookDetail, Long> {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.test_security2db.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import com.example.test_security2db.entity.Book;
|
||||
|
||||
@Repository
|
||||
public interface BookRepository extends JpaRepository<Book, Long> {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.test_security2db.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import com.example.test_security2db.entity.Role;
|
||||
|
||||
public interface RoleRepository extends JpaRepository<Role, Long> {
|
||||
|
||||
Role findByName(String name);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.test_security2db.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import com.example.test_security2db.entity.Shop;
|
||||
|
||||
@Repository
|
||||
public interface ShopRepository extends JpaRepository<Shop, Long> {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.test_security2db.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import com.example.test_security2db.entity.User;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
|
||||
User findByEmail(String email);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.example.test_security2db.service;
|
||||
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.example.test_security2db.entity.User;
|
||||
import com.example.test_security2db.repository.UserRepository;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class CustomUserDetailsService implements UserDetailsService {
|
||||
|
||||
private UserRepository userRepository;
|
||||
|
||||
public CustomUserDetailsService(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String usernameOrEmail) throws UsernameNotFoundException {
|
||||
|
||||
User user = userRepository.findByEmail(usernameOrEmail);
|
||||
if (user != null) {
|
||||
return new org.springframework.security.core.userdetails.User(user.getEmail(),
|
||||
user.getPassword(),
|
||||
user.getRoles().stream()
|
||||
.map((role) -> new SimpleGrantedAuthority(role.getName()))
|
||||
.collect(Collectors.toList()));
|
||||
} else {
|
||||
throw new UsernameNotFoundException("Invalid email or password");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.example.test_security2db.service;
|
||||
|
||||
import com.example.test_security2db.dto.UserDto;
|
||||
import com.example.test_security2db.entity.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface UserService {
|
||||
|
||||
void saveUser(UserDto userDto);
|
||||
|
||||
User findUserByEmail(String email);
|
||||
|
||||
List<UserDto> findAllUsers();
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.example.test_security2db.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import com.example.test_security2db.dto.UserDto;
|
||||
import com.example.test_security2db.entity.Role;
|
||||
import com.example.test_security2db.entity.User;
|
||||
import com.example.test_security2db.repository.RoleRepository;
|
||||
import com.example.test_security2db.repository.UserRepository;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
private final RoleRepository roleRepository;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
@Autowired
|
||||
public UserServiceImpl(UserRepository userRepository,
|
||||
RoleRepository roleRepository,
|
||||
PasswordEncoder passwordEncoder) {
|
||||
this.userRepository = userRepository;
|
||||
this.roleRepository = roleRepository;
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void saveUser(UserDto userDto) {
|
||||
User user = new User();
|
||||
user.setName(userDto.getFirstName() + " " + userDto.getLastName());
|
||||
user.setEmail(userDto.getEmail());
|
||||
user.setPassword(passwordEncoder.encode(userDto.getPassword()));
|
||||
|
||||
Role role = roleRepository.findByName("ROLE_USER");
|
||||
if (role == null) {
|
||||
role = checkRoleExist();
|
||||
}
|
||||
user.setRoles(Arrays.asList(role));
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User findUserByEmail(String email) {
|
||||
return userRepository.findByEmail(email);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserDto> findAllUsers() {
|
||||
List<User> users = userRepository.findAll();
|
||||
return users.stream()
|
||||
.map((user) -> mapToUserDto(user))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private UserDto mapToUserDto(User user) {
|
||||
UserDto userDto = new UserDto();
|
||||
String[] str = user.getName().split(" ");
|
||||
userDto.setFirstName(str[0]);
|
||||
userDto.setLastName(str[1]);
|
||||
userDto.setEmail(user.getEmail());
|
||||
return userDto;
|
||||
}
|
||||
|
||||
private Role checkRoleExist() {
|
||||
Role role = new Role();
|
||||
role.setName("ROLE_USER");
|
||||
return roleRepository.save(role);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
spring.application.name=test-security2db
|
||||
spring.datasource.url=jdbc:h2:file:./testdb/students
|
||||
|
||||
server.port=8081
|
||||
|
||||
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.jpa.show-sql=true
|
||||
spring.main.allow-circular-references=true
|
||||
|
||||
|
||||
spring.jpa.open-in-view=false
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.sql.init.mode=always
|
||||
spring.h2.console.enabled=true
|
||||
@@ -0,0 +1,48 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:th="http://www.thymeleaf.org"
|
||||
>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>О приложении</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet"
|
||||
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
|
||||
crossorigin="anonymous">
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" th:href="@{/index}">Книжный магазин</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
|
||||
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/index}">Главная</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/logout}">Выйти</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<br/><br/>
|
||||
<div class="container">
|
||||
<div class="row col-md-8 offset-md-2">
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<h2 class="text-center">О приложении</h2>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<p>Приложение «Книжный магазин» для учета книг и книжных магазинов.</p>
|
||||
<p>Разработчик: Макаров Игорь</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,56 @@
|
||||
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Книжный магазин</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet"
|
||||
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
|
||||
crossorigin="anonymous">
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" th:href="@{/index}">Книжный магазин</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
|
||||
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/books}">Книги</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/index}">Главная</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<div class="container">
|
||||
|
||||
<hr/>
|
||||
|
||||
<h3>Добавить книгу</h3>
|
||||
<hr/>
|
||||
|
||||
<form th:action="@{/saveBook}" th:object="${book}" method="POST">
|
||||
|
||||
<input type="text" th:field="*{title}" class="form-control col-4 mb-4" placeholder="Введите название"/>
|
||||
|
||||
<input type="text" th:field="*{author}" class="form-control col-4 mb-4" placeholder="Введите автора"/>
|
||||
|
||||
<input type="text" th:field="*{price}" class="form-control col-4 mb-4" placeholder="Введите цену"/>
|
||||
|
||||
<button type="submit" class="btn btn-primary col-2">Сохранить</button>
|
||||
|
||||
<input type="hidden" th:field="*{id}"/>
|
||||
|
||||
</form>
|
||||
<hr/>
|
||||
<a th:href="@{/books}">Назад к списку</a>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,56 @@
|
||||
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Книжный магазин</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet"
|
||||
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
|
||||
crossorigin="anonymous">
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" th:href="@{/index}">Книжный магазин</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
|
||||
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/details}">Описания</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/index}">Главная</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<div class="container">
|
||||
|
||||
<hr/>
|
||||
|
||||
<h3>Добавить описание</h3>
|
||||
<hr/>
|
||||
|
||||
<form th:action="@{/saveDetail}" th:object="${detail}" method="POST">
|
||||
|
||||
<input type="text" th:field="*{description}" class="form-control col-4 mb-4" placeholder="Введите описание"/>
|
||||
|
||||
<select class="form-control col-4 mb-4" name="bookId">
|
||||
<option th:each="book : ${books}" th:value="${book.id}" th:text="${book.title}"></option>
|
||||
</select>
|
||||
|
||||
<button type="submit" class="btn btn-primary col-2">Сохранить</button>
|
||||
|
||||
<input type="hidden" th:field="*{id}"/>
|
||||
|
||||
</form>
|
||||
<hr/>
|
||||
<a th:href="@{/details}">Назад к списку</a>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,56 @@
|
||||
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Книжный магазин</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet"
|
||||
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
|
||||
crossorigin="anonymous">
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" th:href="@{/index}">Книжный магазин</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
|
||||
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/shops}">Магазины</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/index}">Главная</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<div class="container">
|
||||
|
||||
<hr/>
|
||||
|
||||
<h3>Добавить магазин</h3>
|
||||
<hr/>
|
||||
|
||||
<form th:action="@{/saveShop}" th:object="${shop}" method="POST">
|
||||
|
||||
<input type="text" th:field="*{name}" class="form-control col-4 mb-4" placeholder="Введите название"/>
|
||||
|
||||
<input type="text" th:field="*{city}" class="form-control col-4 mb-4" placeholder="Введите город"/>
|
||||
|
||||
<input type="text" th:field="*{address}" class="form-control col-4 mb-4" placeholder="Введите адрес"/>
|
||||
|
||||
<button type="submit" class="btn btn-primary col-2">Сохранить</button>
|
||||
|
||||
<input type="hidden" th:field="*{id}"/>
|
||||
|
||||
</form>
|
||||
<hr/>
|
||||
<a th:href="@{/shops}">Назад к списку</a>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,67 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:th="http://www.thymeleaf.org"
|
||||
>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Расчет стоимости</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet"
|
||||
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
|
||||
crossorigin="anonymous">
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" th:href="@{/index}">Книжный магазин</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
|
||||
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/index}">Главная</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<br/><br/>
|
||||
<div class="container">
|
||||
<div class="row col-md-8 offset-md-2">
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<h2 class="text-center">Расчет стоимости книг</h2>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<form method="post" th:action="@{/calc}">
|
||||
<div class="form-group mb-3">
|
||||
<label class="form-label">Цена книги</label>
|
||||
<input class="form-control" type="text" name="price" placeholder="Введите цену"/>
|
||||
</div>
|
||||
<div class="form-group mb-3">
|
||||
<label class="form-label">Количество</label>
|
||||
<input class="form-control" type="text" name="count" placeholder="Введите количество"/>
|
||||
</div>
|
||||
<div class="form-group mb-3">
|
||||
<label class="form-label">Скидка %</label>
|
||||
<input class="form-control" type="text" name="discount" placeholder="Введите скидку"/>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<button class="btn btn-primary" type="submit">Рассчитать</button>
|
||||
</div>
|
||||
</form>
|
||||
<br/>
|
||||
<div th:if="${result != null}">
|
||||
<div class="alert alert-info">
|
||||
Итого: <span th:text="${result}"></span>
|
||||
</div>
|
||||
</div>
|
||||
<a th:href="@{/index}">На главную</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,56 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:th="http://www.thymeleaf.org"
|
||||
>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Книжный магазин</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet"
|
||||
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
|
||||
crossorigin="anonymous">
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" th:href="@{/index}">Книжный магазин</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
|
||||
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/books}">Книги</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/shops}">Магазины</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/details}">Описания</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/calc}">Расчет</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/about}">О приложении</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/login}">Login</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/register}">Register</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<br/><br/>
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<h1 class="text-center">Книжный магазин</h1>
|
||||
<p class="text-center">Главная страница</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,78 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Книжный магазин</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet"
|
||||
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
|
||||
crossorigin="anonymous">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" th:href="@{/index}">Книжный магазин</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
|
||||
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/books}">Книги</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/shops}">Магазины</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/index}">Главная</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<div class="container">
|
||||
|
||||
<hr/>
|
||||
<a th:href="@{/addBookForm}" class="btn btn-primary">Добавить книгу</a>
|
||||
<br/><br/>
|
||||
<table class="table table-bordered table-striped" id="bookTable">
|
||||
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Название</th>
|
||||
<th>Автор</th>
|
||||
<th>Цена</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="book: ${books}">
|
||||
<td th:text="${book.title}" />
|
||||
<td th:text="${book.author}" />
|
||||
<td th:text="${book.price}" />
|
||||
<td>
|
||||
<a th:href="@{/showBookUpdateForm(bookId=${book.id})}" class="btn btn-info">Изменить</a>
|
||||
|
||||
<a th:href="@{/deleteBook(bookId=${book.id})}" class="btn btn-danger ml-2">Удалить</a>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
|
||||
<script type="text/javascript" src="https://cdn.datatables.net/v/bs4/dt-1.10.25/datatables.min.js"></script>
|
||||
<script>
|
||||
$(document).ready(function() {
|
||||
$("#bookTable").DataTable({
|
||||
'aoColumnDefs': [{
|
||||
'bSortable': false,
|
||||
'aTargets': [-1]
|
||||
}]
|
||||
});
|
||||
})
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,76 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Книжный магазин</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet"
|
||||
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
|
||||
crossorigin="anonymous">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" th:href="@{/index}">Книжный магазин</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
|
||||
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/books}">Книги</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/details}">Описания</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/index}">Главная</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<div class="container">
|
||||
|
||||
<hr/>
|
||||
<a th:href="@{/addDetailForm}" class="btn btn-primary">Добавить описание</a>
|
||||
<br/><br/>
|
||||
<table class="table table-bordered table-striped" id="detailTable">
|
||||
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Описание</th>
|
||||
<th>Книга</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="detail: ${details}">
|
||||
<td th:text="${detail.description}" />
|
||||
<td th:text="${detail.book != null ? detail.book.title : ''}" />
|
||||
<td>
|
||||
<a th:href="@{/showDetailUpdateForm(detailId=${detail.id})}" class="btn btn-info">Изменить</a>
|
||||
|
||||
<a th:href="@{/deleteDetail(detailId=${detail.id})}" class="btn btn-danger ml-2">Удалить</a>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
|
||||
<script type="text/javascript" src="https://cdn.datatables.net/v/bs4/dt-1.10.25/datatables.min.js"></script>
|
||||
<script>
|
||||
$(document).ready(function() {
|
||||
$("#detailTable").DataTable({
|
||||
'aoColumnDefs': [{
|
||||
'bSortable': false,
|
||||
'aTargets': [-1]
|
||||
}]
|
||||
});
|
||||
})
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,78 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Книжный магазин</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet"
|
||||
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
|
||||
crossorigin="anonymous">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" th:href="@{/index}">Книжный магазин</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
|
||||
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/books}">Книги</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/shops}">Магазины</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/index}">Главная</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<div class="container">
|
||||
|
||||
<hr/>
|
||||
<a th:href="@{/addShopForm}" class="btn btn-primary">Добавить магазин</a>
|
||||
<br/><br/>
|
||||
<table class="table table-bordered table-striped" id="shopTable">
|
||||
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Название</th>
|
||||
<th>Город</th>
|
||||
<th>Адрес</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="shop: ${shops}">
|
||||
<td th:text="${shop.name}" />
|
||||
<td th:text="${shop.city}" />
|
||||
<td th:text="${shop.address}" />
|
||||
<td>
|
||||
<a th:href="@{/showShopUpdateForm(shopId=${shop.id})}" class="btn btn-info">Изменить</a>
|
||||
|
||||
<a th:href="@{/deleteShop(shopId=${shop.id})}" class="btn btn-danger ml-2">Удалить</a>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
|
||||
<script type="text/javascript" src="https://cdn.datatables.net/v/bs4/dt-1.10.25/datatables.min.js"></script>
|
||||
<script>
|
||||
$(document).ready(function() {
|
||||
$("#shopTable").DataTable({
|
||||
'aoColumnDefs': [{
|
||||
'bSortable': false,
|
||||
'aTargets': [-1]
|
||||
}]
|
||||
});
|
||||
})
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,87 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:th="http://www.thymeleaf.org"
|
||||
>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Книжный магазин</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet"
|
||||
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
|
||||
crossorigin="anonymous">
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" th:href="@{/index}">Книжный магазин</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
|
||||
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/register}">Register</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<br/><br/>
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-md-6 offset-md-3">
|
||||
|
||||
<div th:if="${param.error}">
|
||||
<div class="alert alert-danger">Invalid Email or Password</div>
|
||||
</div>
|
||||
<div th:if="${param.logout}">
|
||||
<div class="alert alert-success"> You have been logged out.</div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<h2 class="text-center">Login Form</h2>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<form
|
||||
method="post"
|
||||
role="form"
|
||||
th:action="@{/login}"
|
||||
class="form-horizontal"
|
||||
>
|
||||
<div class="form-group mb-3">
|
||||
<label class="control-label"> Email</label>
|
||||
<input
|
||||
type="text"
|
||||
id="username"
|
||||
name="username"
|
||||
class="form-control"
|
||||
placeholder="Enter email address"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-group mb-3">
|
||||
<label class="control-label"> Password</label>
|
||||
<input
|
||||
type="password"
|
||||
id="password"
|
||||
name="password"
|
||||
class="form-control"
|
||||
placeholder="Enter password"
|
||||
/>
|
||||
</div>
|
||||
<div class="form-group mb-3">
|
||||
<button type="submit" class="btn btn-primary">Submit</button>
|
||||
<span> Not registered ?
|
||||
<a th:href="@{/register}">Register/Signup here</a>
|
||||
</span>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,114 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:th="http://www.thymeleaf.org"
|
||||
>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Книжный магазин</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet"
|
||||
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
|
||||
crossorigin="anonymous">
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" th:href="@{/index}">Книжный магазин</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
|
||||
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/login}">Login</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<br/><br/><br/>
|
||||
<div class="container">
|
||||
<div class="row col-md-8 offset-md-2">
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<h2 class="text-center">Registration</h2>
|
||||
</div>
|
||||
<div th:if="${param.success}">
|
||||
<div class="alert alert-info">
|
||||
You have successfully registered our app!
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<form
|
||||
method="post"
|
||||
role="form"
|
||||
th:action="@{/register/save}"
|
||||
th:object="${user}"
|
||||
>
|
||||
<div class="form-group mb-3">
|
||||
<label class="form-label">First Name</label>
|
||||
<input
|
||||
class="form-control"
|
||||
id="firstName"
|
||||
name="firstName"
|
||||
placeholder="Enter first name"
|
||||
th:field="*{firstName}"
|
||||
type="text"
|
||||
/>
|
||||
<p th:errors="*{firstName}" class="text-danger"
|
||||
th:if="${#fields.hasErrors('firstName')}"></p>
|
||||
</div>
|
||||
|
||||
<div class="form-group mb-3">
|
||||
<label class="form-label">Last Name</label>
|
||||
<input
|
||||
class="form-control"
|
||||
id="lastName"
|
||||
name="lastName"
|
||||
placeholder="Enter last name"
|
||||
th:field="*{lastName}"
|
||||
type="text"
|
||||
/>
|
||||
<p th:errors="*{lastName}" class="text-danger"
|
||||
th:if="${#fields.hasErrors('lastName')}"></p>
|
||||
</div>
|
||||
|
||||
<div class="form-group mb-3">
|
||||
<label class="form-label">Email</label>
|
||||
<input
|
||||
class="form-control"
|
||||
id="email"
|
||||
name="email"
|
||||
placeholder="Enter email address"
|
||||
th:field="*{email}"
|
||||
type="email"
|
||||
/>
|
||||
<p th:errors="*{email}" class="text-danger"
|
||||
th:if="${#fields.hasErrors('email')}"></p>
|
||||
</div>
|
||||
|
||||
<div class="form-group mb-3">
|
||||
<label class="form-label">Password</label>
|
||||
<input
|
||||
class="form-control"
|
||||
id="password"
|
||||
name="password"
|
||||
placeholder="Enter password"
|
||||
th:field="*{password}"
|
||||
type="password"
|
||||
/>
|
||||
<p th:errors="*{password}" class="text-danger"
|
||||
th:if="${#fields.hasErrors('password')}"></p>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<button class="btn btn-primary" type="submit">Register</button>
|
||||
<span>Already registered? <a th:href="@{/login}">Login here</a></span>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,65 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:th="http://www.thymeleaf.org"
|
||||
>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Роли пользователей</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet"
|
||||
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
|
||||
crossorigin="anonymous">
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" th:href="@{/index}">Книжный магазин</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
|
||||
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/users}">Пользователи</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/index}">Главная</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/logout}">Выйти</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<br/><br/>
|
||||
<div class="container">
|
||||
<div class="row col-md-8 offset-md-2">
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<h2 class="text-center">Добавить роль</h2>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<form method="post" th:action="@{/saveRole}">
|
||||
<div class="form-group mb-3">
|
||||
<label class="form-label">Пользователь</label>
|
||||
<select class="form-control" name="userId">
|
||||
<option th:each="user : ${users}" th:value="${user.id}" th:text="${user.email}"></option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group mb-3">
|
||||
<label class="form-label">Роли</label><br/>
|
||||
<input type="checkbox" name="roles" value="ROLE_USER"/> USER<br/>
|
||||
<input type="checkbox" name="roles" value="ROLE_ADMIN"/> ADMIN<br/>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<button class="btn btn-primary" type="submit">Сохранить</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,63 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"
|
||||
xmlns:th="http://www.thymeleaf.org"
|
||||
>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Registration and Login System</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet"
|
||||
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
|
||||
crossorigin="anonymous">
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" th:href="@{/index}">Книжный магазин</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
|
||||
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/logout}">Logout</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/books}">Книги</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/shops}">Магазины</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" aria-current="page" th:href="@{/roles}">Роли</a>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<div class="container">
|
||||
<div class="row col-md-10">
|
||||
<h2>List of Registered Users</h2>
|
||||
</div>
|
||||
<table class="table table-bordered table-hover">
|
||||
<thead class="table-dark">
|
||||
<tr>
|
||||
<th>First Name</th>
|
||||
<th>Last Name</th>
|
||||
<th>Email</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="user : ${users}">
|
||||
<td th:text="${user.firstName}"></td>
|
||||
<td th:text="${user.lastName}"></td>
|
||||
<td th:text="${user.email}"></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.test_security2db;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class TestSecurity2dbApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user