Compare commits

...

2 Commits

Author SHA1 Message Date
e54fbdb7b7 lab6 vue 2023-09-03 19:41:59 +04:00
2fddeddb73 lab6vue 2023-06-13 16:16:57 +04:00
15 changed files with 72 additions and 75 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'
} }
@ -18,22 +18,37 @@ jar {
dependencies { dependencies {
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor" annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'com.h2database:h2:2.1.210'
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 'com.auth0:java-jwt:4.4.0'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
implementation 'org.springframework.boot:spring-boot-devtools' implementation 'org.hibernate.validator:hibernate-validator'
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'
implementation 'org.webjars:bootstrap:5.1.3' implementation 'org.springdoc:springdoc-openapi-ui:1.6.5'
implementation 'org.webjars:jquery:3.6.0'
implementation 'org.webjars:font-awesome:6.1.0'
implementation 'com.h2database:h2:2.1.210'
implementation 'jakarta.validation:jakarta.validation-api:3.0.0'
implementation 'org.hibernate.validator:hibernate-validator:7.0.1.Final'
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
// annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
// implementation 'org.springframework.boot:spring-boot-starter-web'
// implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
// implementation 'org.springframework.boot:spring-boot-starter-security'
// implementation 'com.auth0:java-jwt:4.4.0'
// implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
// 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 'com.h2database:h2:2.1.210'
// implementation 'jakarta.validation:jakarta.validation-api:3.0.0'
// implementation 'org.hibernate.validator:hibernate-validator:7.0.1.Final'
// implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
// testImplementation 'org.springframework.boot:spring-boot-starter-test'
} }
tasks.named('test') { tasks.named('test') {

Binary file not shown.

View File

@ -79,6 +79,11 @@ export default {
editedSong: new Song(), editedSong: new Song(),
} }
}, },
beforeCreate() {
if (localStorage.getItem("token") == null) {
this.$router.push("/login");
}
},
methods: { methods: {
getSongs(){ getSongs(){
axios.get(this.URL + "song") axios.get(this.URL + "song")

View File

@ -1,12 +1,5 @@
package ru.ulstu.is.sbapp.configuration; package ru.ulstu.is.sbapp.configuration;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
@ -14,7 +7,12 @@ import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.filter.GenericFilterBean; import org.springframework.web.filter.GenericFilterBean;
import ru.ulstu.is.sbapp.database.service.UserService; import ru.ulstu.is.sbapp.database.service.UserService;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
public class JwtFilter extends GenericFilterBean { public class JwtFilter extends GenericFilterBean {

View File

@ -2,15 +2,15 @@ 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.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
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.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import ru.ulstu.is.sbapp.controllers.UserController; import ru.ulstu.is.sbapp.controllers.UserController;
import ru.ulstu.is.sbapp.database.model.Role; import ru.ulstu.is.sbapp.database.model.Role;
@ -18,24 +18,17 @@ import ru.ulstu.is.sbapp.database.service.UserService;
@Configuration @Configuration
@EnableWebSecurity @EnableWebSecurity
@EnableMethodSecurity( @EnableMethodSecurity(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";
public static final String SPA_URL_MASK = "/{path:[^\\.]*}"; public static final String SPA_URL_MASK = "/{path:[^\\.]*}";
private final UserService userService; private final UserService userService;
private final JwtFilter jwtFilter; private final JwtFilter jwtFilter;
public SecurityConfiguration(UserService userService) {
public SecurityConfiguration(UserService userService)
{
this.userService = userService; this.userService = userService;
this.jwtFilter = new JwtFilter(userService); this.jwtFilter = new JwtFilter(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) {
@ -43,41 +36,35 @@ public class SecurityConfiguration {
userService.addUser(admin, admin, admin, Role.ADMIN); userService.addUser(admin, admin, admin, Role.ADMIN);
} }
} }
@Bean @Override
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
log.info("Creating security configuration");
http.cors() http.cors()
.and() .and()
.csrf().disable() .csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and() .and()
.authorizeHttpRequests() .authorizeRequests()
.requestMatchers("", SPA_URL_MASK).permitAll() .antMatchers("/", SPA_URL_MASK).permitAll()
.requestMatchers("/", SPA_URL_MASK).permitAll() .antMatchers(HttpMethod.POST, UserController.URL_LOGIN).permitAll()
.requestMatchers(HttpMethod.POST, UserController.URL_LOGIN).permitAll()
.requestMatchers(HttpMethod.POST, UserController.URL_SIGN_UP).permitAll()
.requestMatchers(HttpMethod.POST, UserController.URL_WHO_AM_I).permitAll()
.anyRequest() .anyRequest()
.authenticated() .authenticated()
.and() .and()
.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class) .addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class)
.anonymous(); .anonymous();
return http.userDetailsService(userService).build();
} }
@Override
@Bean protected void configure(AuthenticationManagerBuilder builder) throws Exception {
public WebSecurityCustomizer webSecurityCustomizer() { builder.userDetailsService(userService);
return (web) -> web.ignoring() }
.requestMatchers(HttpMethod.OPTIONS, "/**") @Override
.requestMatchers("/*.js") public void configure(WebSecurity web) {
.requestMatchers("/*.html") web.ignoring()
.requestMatchers("/*.css") .antMatchers(HttpMethod.OPTIONS, "/**")
.requestMatchers("/assets/**") .antMatchers("/**/*.{js,html,css,png}")
.requestMatchers("/favicon.ico") .antMatchers("/swagger-ui/index.html")
.requestMatchers("/.js", "/.css") .antMatchers("/webjars/**")
.requestMatchers("/swagger-ui/index.html") .antMatchers("/swagger-resources/**")
.requestMatchers("/webjars/**") .antMatchers("/v3/api-docs/**");
.requestMatchers("/swagger-resources/**")
.requestMatchers("/v3/api-docs/**")
.requestMatchers("/h2-console/**");
} }
} }

View File

@ -1,6 +1,5 @@
package ru.ulstu.is.sbapp.controllers; package ru.ulstu.is.sbapp.controllers;
import jakarta.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.OpenAPI30Configuration; import ru.ulstu.is.sbapp.configuration.OpenAPI30Configuration;
@ -8,6 +7,7 @@ import ru.ulstu.is.sbapp.database.model.Artist;
import ru.ulstu.is.sbapp.database.model.Song; import ru.ulstu.is.sbapp.database.model.Song;
import ru.ulstu.is.sbapp.database.service.AlbumService; import ru.ulstu.is.sbapp.database.service.AlbumService;
import javax.validation.Valid;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;

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.OpenAPI30Configuration; import ru.ulstu.is.sbapp.configuration.OpenAPI30Configuration;
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.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.sbapp.configuration.OpenAPI30Configuration; import ru.ulstu.is.sbapp.configuration.OpenAPI30Configuration;
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.access.annotation.Secured; import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.sbapp.database.model.User; import ru.ulstu.is.sbapp.database.model.User;

View File

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

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;

View File

@ -1,8 +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;
import java.util.List; import java.util.List;

View File

@ -1,10 +1,8 @@
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 jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import javax.persistence.*;
import java.util.Objects; import java.util.Objects;
@Entity @Entity

View File

@ -1,8 +1,8 @@
package ru.ulstu.is.sbapp.database.model; package ru.ulstu.is.sbapp.database.model;
import jakarta.persistence.*;
import ru.ulstu.is.sbapp.controllers.UserSignUpDTO; import ru.ulstu.is.sbapp.controllers.UserSignUpDTO;
import javax.persistence.*;
import java.util.Objects; import java.util.Objects;
@Entity @Entity

View File

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