lab6 mvc done

This commit is contained in:
Татьяна Артамонова 2023-09-22 16:24:02 +04:00
parent dccbe55510
commit e86f9c2d5a
16 changed files with 93 additions and 67 deletions

View File

@ -1,6 +1,6 @@
plugins { plugins {
id 'java' id 'java'
id 'org.springframework.boot' version '3.0.2' id 'org.springframework.boot' version '2.6.3'
id 'io.spring.dependency-management' version '1.1.0' id 'io.spring.dependency-management' version '1.1.0'
} }
@ -13,28 +13,50 @@ repositories {
} }
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.h2database:h2:2.1.210'
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-devtools' implementation 'org.springframework.boot:spring-boot-devtools'
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect' implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'
implementation 'org.webjars:bootstrap:5.1.3' implementation 'org.webjars:bootstrap:5.1.3'
implementation 'org.webjars:jquery:3.6.0' implementation 'org.webjars:jquery:3.6.0'
implementation 'org.webjars:font-awesome:6.1.0' implementation 'org.webjars:font-awesome:6.1.0'
implementation 'org.jetbrains:annotations:24.0.0' implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.jetbrains:annotations:24.0.0' implementation 'com.h2database:h2:2.1.210'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.hibernate.validator:hibernate-validator'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'com.auth0:java-jwt:4.4.0' implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor" implementation 'org.hibernate.validator:hibernate-validator'
implementation 'org.springdoc:springdoc-openapi-ui:1.6.5'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// implementation 'org.springframework.boot:spring-boot-starter-web'
// implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// implementation 'com.h2database:h2:2.1.210'
// implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
//
// implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
// implementation 'org.springframework.boot:spring-boot-devtools'
// implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'
// implementation 'org.webjars:bootstrap:5.1.3'
// implementation 'org.webjars:jquery:3.6.0'
// implementation 'org.webjars:font-awesome:6.1.0'
//
// implementation 'org.jetbrains:annotations:24.0.0'
// implementation 'org.jetbrains:annotations:24.0.0'
// implementation 'org.springframework.boot:spring-boot-starter-validation'
// implementation 'org.hibernate.validator:hibernate-validator'
// testImplementation 'org.springframework.boot:spring-boot-starter-test'
//
// implementation 'org.springframework.boot:spring-boot-starter-security'
// implementation 'com.auth0:java-jwt:4.4.0'
// implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
// annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
} }
tasks.named('test') { tasks.named('test') {

View File

@ -2,33 +2,29 @@ package ru.ulstu.is.sbapp.configuration;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.web.SecurityFilterChain;
import ru.ulstu.is.sbapp.controllers.UserSignUpMvcController; import ru.ulstu.is.sbapp.controllers.UserSignUpMvcController;
import ru.ulstu.is.sbapp.database.model.UserRole; import ru.ulstu.is.sbapp.database.model.UserRole;
import ru.ulstu.is.sbapp.database.service.UserService; import ru.ulstu.is.sbapp.database.service.UserService;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration @Configuration
@EnableWebSecurity @EnableWebSecurity
@EnableMethodSecurity( @EnableGlobalMethodSecurity(securedEnabled = true)
securedEnabled = true public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
)
public class SecurityConfiguration {
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";
private final UserService userService; private final UserService userService;
public SecurityConfiguration(UserService userService) { public SecurityConfiguration(UserService userService) {
this.userService = userService; this.userService = userService;
createAdminOnStartup(); createAdminOnStartup();
} }
private void createAdminOnStartup() { private void createAdminOnStartup() {
final String admin = "admin"; final String admin = "admin";
if (userService.findByLogin(admin) == null) { if (userService.findByLogin(admin) == null) {
@ -36,32 +32,42 @@ public class SecurityConfiguration {
userService.createUser(admin, admin, admin, UserRole.ADMIN); userService.createUser(admin, admin, admin, UserRole.ADMIN);
} }
} }
@Override
@Bean protected void configure(HttpSecurity http) throws Exception {
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.headers().frameOptions().sameOrigin().and() http.headers().frameOptions().sameOrigin().and()
.cors().and() .cors().and()
.csrf().disable() .csrf().disable()
.authorizeHttpRequests() .authorizeRequests()
.requestMatchers(UserSignUpMvcController.SIGNUP_URL).permitAll() .antMatchers(UserSignUpMvcController.SIGNUP_URL).permitAll()
.requestMatchers(HttpMethod.GET, LOGIN_URL).permitAll() .antMatchers(HttpMethod.GET, LOGIN_URL).permitAll()
.anyRequest().authenticated() .anyRequest().authenticated()
.and() .and()
.formLogin() .formLogin()
.loginPage(LOGIN_URL).permitAll() .loginPage(LOGIN_URL).permitAll()
.defaultSuccessUrl("/artist", true)
.and() .and()
.logout().permitAll(); .logout().permitAll();
return http.userDetailsService(userService).build(); }
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService);
}
@Override
public void configure(WebSecurity web) {
web.ignoring()
.antMatchers("/css/**")
.antMatchers("/js/**")
.antMatchers("/templates/**")
.antMatchers("/webjars/**");
} }
// @Override
@Bean // public void configure(WebSecurity web) {
public WebSecurityCustomizer webSecurityCustomizer() { // web.ignoring()
return (web) -> web.ignoring() // .antMatchers(HttpMethod.OPTIONS, "/**")
.requestMatchers("/css/**") // .antMatchers("/**/*.{js,html,css,png}")
.requestMatchers("/js/**") // .antMatchers("/swagger-ui/index.html")
.requestMatchers("/templates/**") // .antMatchers("/webjars/**")
.requestMatchers("/webjars/**"); // .antMatchers("/swagger-resources/**")
} // .antMatchers("/v3/api-docs/**");
// }
} }

View File

@ -1,6 +1,6 @@
package ru.ulstu.is.sbapp.controllers; package ru.ulstu.is.sbapp.controllers;
import jakarta.validation.Valid; import javax.validation.Valid;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.sbapp.configuration.WebConfiguration; import ru.ulstu.is.sbapp.configuration.WebConfiguration;

View File

@ -1,6 +1,6 @@
package ru.ulstu.is.sbapp.controllers; package ru.ulstu.is.sbapp.controllers;
import jakarta.validation.Valid; import javax.validation.Valid;
import org.springframework.security.core.Authentication; 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;

View File

@ -1,6 +1,6 @@
package ru.ulstu.is.sbapp.controllers; package ru.ulstu.is.sbapp.controllers;
import jakarta.validation.Valid; import javax.validation.Valid;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.sbapp.configuration.WebConfiguration; import ru.ulstu.is.sbapp.configuration.WebConfiguration;
import ru.ulstu.is.sbapp.database.service.ArtistService; import ru.ulstu.is.sbapp.database.service.ArtistService;

View File

@ -1,6 +1,6 @@
package ru.ulstu.is.sbapp.controllers; package ru.ulstu.is.sbapp.controllers;
import jakarta.validation.Valid; import javax.validation.Valid;
import org.springframework.security.core.Authentication; 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;

View File

@ -1,6 +1,6 @@
package ru.ulstu.is.sbapp.controllers; package ru.ulstu.is.sbapp.controllers;
import jakarta.validation.Valid; import javax.validation.Valid;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.sbapp.configuration.WebConfiguration; import ru.ulstu.is.sbapp.configuration.WebConfiguration;
import ru.ulstu.is.sbapp.database.service.AlbumService; import ru.ulstu.is.sbapp.database.service.AlbumService;

View File

@ -1,6 +1,6 @@
package ru.ulstu.is.sbapp.controllers; package ru.ulstu.is.sbapp.controllers;
import jakarta.validation.Valid; import javax.validation.Valid;
import org.springframework.security.core.Authentication; 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;

View File

@ -1,7 +1,7 @@
package ru.ulstu.is.sbapp.controllers; package ru.ulstu.is.sbapp.controllers;
import jakarta.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size; import javax.validation.constraints.Size;
public class UserSignUpDTO { public class UserSignUpDTO {
@NotBlank @NotBlank

View File

@ -1,6 +1,6 @@
package ru.ulstu.is.sbapp.controllers; package ru.ulstu.is.sbapp.controllers;
import jakarta.validation.Valid; import javax.validation.Valid;
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;

View File

@ -1,7 +1,7 @@
package ru.ulstu.is.sbapp.database.model; package ru.ulstu.is.sbapp.database.model;
import com.fasterxml.jackson.annotation.JsonManagedReference; import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*; import javax.persistence.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -12,12 +12,12 @@ import java.util.Objects;
@Table(name = "albums") @Table(name = "albums")
public class Album { public class Album {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue
private Long id; private Long id;
private String albumName; private String albumName;
@JsonManagedReference @JsonManagedReference
@OneToMany(fetch = FetchType.EAGER, mappedBy = "album") @OneToMany(cascade = CascadeType.MERGE, mappedBy = "album")
private List<Song> songs; private List<Song> songs;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)

View File

@ -1,7 +1,7 @@
package ru.ulstu.is.sbapp.database.model; package ru.ulstu.is.sbapp.database.model;
import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.*; import javax.persistence.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -12,7 +12,7 @@ import java.util.Objects;
@Table(name = "artists") @Table(name = "artists")
public class Artist { public class Artist {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue
private Long id; private Long id;
private String artistName; private String artistName;
private String genre; private String genre;

View File

@ -1,9 +1,7 @@
package ru.ulstu.is.sbapp.database.model; package ru.ulstu.is.sbapp.database.model;
import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.*; import javax.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.Objects; import java.util.Objects;
@ -11,7 +9,7 @@ import java.util.Objects;
@Table(name = "songs") @Table(name = "songs")
public class Song { public class Song {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue
private Long id; private Long id;
private String songName; private String songName;
private Double duration; private Double duration;

View File

@ -1,8 +1,8 @@
package ru.ulstu.is.sbapp.database.model; package ru.ulstu.is.sbapp.database.model;
import jakarta.persistence.*; import javax.persistence.*;
import jakarta.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size; import javax.validation.constraints.Size;
import java.util.Objects; import java.util.Objects;
@ -10,7 +10,7 @@ import java.util.Objects;
@Table(name = "users") @Table(name = "users")
public class User { public class User {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue
private Long id; private Long id;
@Column(nullable = false, unique = true, length = 64) @Column(nullable = false, unique = true, length = 64)
@NotBlank @NotBlank

View File

@ -1,6 +1,6 @@
package ru.ulstu.is.sbapp.database.service; package ru.ulstu.is.sbapp.database.service;
import jakarta.validation.ValidationException; import javax.validation.ValidationException;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;

View File

@ -1,9 +1,9 @@
package ru.ulstu.is.sbapp.database.util.validation; package ru.ulstu.is.sbapp.database.util.validation;
import jakarta.validation.ConstraintViolation; import javax.validation.ConstraintViolation;
import jakarta.validation.Validation; import javax.validation.Validation;
import jakarta.validation.Validator; import javax.validation.Validator;
import jakarta.validation.ValidatorFactory; import javax.validation.ValidatorFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Set; import java.util.Set;