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 {
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'
}
@ -13,28 +13,50 @@ repositories {
}
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-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-data-jpa'
implementation 'com.h2database:h2:2.1.210'
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"
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
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') {

View File

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

View File

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

View File

@ -1,6 +1,6 @@
package ru.ulstu.is.sbapp.controllers;
import jakarta.validation.Valid;
import javax.validation.Valid;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

View File

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

View File

@ -1,6 +1,6 @@
package ru.ulstu.is.sbapp.controllers;
import jakarta.validation.Valid;
import javax.validation.Valid;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

View File

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

View File

@ -1,6 +1,6 @@
package ru.ulstu.is.sbapp.controllers;
import jakarta.validation.Valid;
import javax.validation.Valid;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

View File

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

View File

@ -1,6 +1,6 @@
package ru.ulstu.is.sbapp.controllers;
import jakarta.validation.Valid;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,6 @@
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.PageRequest;
import org.springframework.data.domain.Sort;

View File

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