поменял на ласт версию спринга

This commit is contained in:
Николай 2023-05-11 16:52:19 +04:00
parent 1317a2824d
commit b468a84b1a
16 changed files with 57 additions and 50 deletions

View File

@ -1,6 +1,6 @@
plugins { plugins {
id 'java' id 'java'
id 'org.springframework.boot' version '2.6.3' id 'org.springframework.boot' version '3.0.2'
id 'io.spring.dependency-management' version '1.1.0' id 'io.spring.dependency-management' version '1.1.0'
} }
@ -28,7 +28,7 @@ dependencies {
implementation 'com.h2database:h2:2.1.210' implementation 'com.h2database:h2:2.1.210'
implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5' implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
implementation 'org.hibernate.validator:hibernate-validator' implementation 'org.hibernate.validator:hibernate-validator'
implementation 'org.springdoc:springdoc-openapi-ui:1.6.5' implementation 'org.springdoc:springdoc-openapi-ui:1.6.5'

View File

@ -12,3 +12,4 @@ public class PasswordEncoderConfiguration {
return new BCryptPasswordEncoder(); return new BCryptPasswordEncoder();
} }
} }

View File

@ -5,19 +5,21 @@ import com.LabWork.app.MangaStore.model.Default.UserRole;
import com.LabWork.app.MangaStore.service.UserService; import com.LabWork.app.MangaStore.service.UserService;
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.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 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.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.web.SecurityFilterChain;
@Configuration @Configuration
@EnableWebSecurity @EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true) @EnableGlobalMethodSecurity(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;
@ -35,33 +37,38 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
} }
} }
@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()
.authorizeRequests() .authorizeHttpRequests()
.antMatchers(UserSignupMvcController.SIGNUP_URL).permitAll() .requestMatchers(UserSignupMvcController.SIGNUP_URL).permitAll()
.antMatchers(HttpMethod.GET, LOGIN_URL).permitAll() .requestMatchers(HttpMethod.GET, LOGIN_URL).permitAll()
.anyRequest().authenticated() .anyRequest().authenticated()
.and() .and()
.formLogin() .formLogin()
.loginPage(LOGIN_URL).permitAll() .loginPage(LOGIN_URL).permitAll()
.and() .and()
.logout().permitAll(); .logout().permitAll();
return http.build();
} }
@Override @Bean
protected void configure(AuthenticationManagerBuilder auth) throws Exception { public AuthenticationManager authenticationManagerBean(HttpSecurity http) throws Exception {
auth.userDetailsService(userService); AuthenticationManagerBuilder authenticationManagerBuilder = http
.getSharedObject(AuthenticationManagerBuilder.class);
authenticationManagerBuilder.userDetailsService(userService);
return authenticationManagerBuilder.build();
} }
@Override @Bean
public void configure(WebSecurity web) { public WebSecurityCustomizer webSecurityCustomizer() {
web.ignoring() return web -> web.ignoring()
.antMatchers("/css/**") .requestMatchers("/css/**")
.antMatchers("/js/**") .requestMatchers("/js/**")
.antMatchers("/templates/**") .requestMatchers("/templates/**")
.antMatchers("/webjars/**"); .requestMatchers("/webjars/**")
.requestMatchers("/vk.jpg");
} }
} }

View File

@ -11,7 +11,6 @@ public class WebConfiguration implements WebMvcConfigurer {
@Override @Override
public void addViewControllers(ViewControllerRegistry registry) { public void addViewControllers(ViewControllerRegistry registry) {
WebMvcConfigurer.super.addViewControllers(registry); WebMvcConfigurer.super.addViewControllers(registry);
registry.addViewController("rest-test");
registry.addViewController("login"); registry.addViewController("login");
} }

View File

@ -4,7 +4,7 @@ import com.LabWork.app.MangaStore.model.Dto.CreatorMangaDto;
import com.LabWork.app.MangaStore.model.Dto.SupportDto.MangaDto; import com.LabWork.app.MangaStore.model.Dto.SupportDto.MangaDto;
import com.LabWork.app.MangaStore.service.CreatorService; import com.LabWork.app.MangaStore.service.CreatorService;
import com.LabWork.app.MangaStore.service.MangaService; import com.LabWork.app.MangaStore.service.MangaService;
import javax.validation.Valid; import jakarta.validation.Valid;
import com.LabWork.app.MangaStore.model.Default.UserRole; import com.LabWork.app.MangaStore.model.Default.UserRole;
import org.slf4j.Logger; import org.slf4j.Logger;

View File

@ -5,7 +5,7 @@ import com.LabWork.app.MangaStore.model.Dto.ReaderMangaDto;
import com.LabWork.app.MangaStore.model.Dto.SupportDto.MangaDto; import com.LabWork.app.MangaStore.model.Dto.SupportDto.MangaDto;
import com.LabWork.app.MangaStore.service.ReaderService; import com.LabWork.app.MangaStore.service.ReaderService;
import com.LabWork.app.MangaStore.service.MangaService; import com.LabWork.app.MangaStore.service.MangaService;
import javax.validation.Valid; import jakarta.validation.Valid;
import com.LabWork.app.MangaStore.model.Default.UserRole; import com.LabWork.app.MangaStore.model.Default.UserRole;
import org.slf4j.Logger; import org.slf4j.Logger;

View File

@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import javax.validation.Valid; import jakarta.validation.Valid;
@Controller @Controller
@RequestMapping(UserSignupMvcController.SIGNUP_URL) @RequestMapping(UserSignupMvcController.SIGNUP_URL)

View File

@ -1,6 +1,6 @@
package com.LabWork.app.MangaStore.model.Default; package com.LabWork.app.MangaStore.model.Default;
import javax.persistence.*; import jakarta.persistence.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View File

@ -3,7 +3,7 @@ package com.LabWork.app.MangaStore.model.Default;
import com.LabWork.app.MangaStore.model.Dto.SupportDto.MangaDto; import com.LabWork.app.MangaStore.model.Dto.SupportDto.MangaDto;
import com.LabWork.app.MangaStore.service.CreatorService; import com.LabWork.app.MangaStore.service.CreatorService;
import com.LabWork.app.MangaStore.service.MangaService; import com.LabWork.app.MangaStore.service.MangaService;
import javax.persistence.*; import jakarta.persistence.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View File

@ -1,6 +1,6 @@
package com.LabWork.app.MangaStore.model.Default; package com.LabWork.app.MangaStore.model.Default;
import javax.persistence.*; import jakarta.persistence.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View File

@ -1,8 +1,8 @@
package com.LabWork.app.MangaStore.model.Default; package com.LabWork.app.MangaStore.model.Default;
import javax.persistence.*; import jakarta.persistence.*;
import javax.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import javax.validation.constraints.Size; import jakarta.validation.constraints.Size;
import java.util.Objects; import java.util.Objects;
@Entity @Entity

View File

@ -2,8 +2,8 @@ package com.LabWork.app.MangaStore.model.Dto;
import com.LabWork.app.MangaStore.model.Default.UserRole; import com.LabWork.app.MangaStore.model.Default.UserRole;
import javax.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import javax.validation.constraints.Size; import jakarta.validation.constraints.Size;
public class UserSignupDto { public class UserSignupDto {
@NotBlank @NotBlank

View File

@ -2,10 +2,10 @@ package com.LabWork.app.MangaStore.util.validation;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.validation.ConstraintViolation; import jakarta.validation.ConstraintViolation;
import javax.validation.Validation; import jakarta.validation.Validation;
import javax.validation.Validator; import jakarta.validation.Validator;
import javax.validation.ValidatorFactory; import jakarta.validation.ValidatorFactory;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;

View File

@ -26,20 +26,18 @@
<span class="navbar-toggler-icon"></span> <span class="navbar-toggler-icon"></span>
</button> </button>
<div class="collapse navbar-collapse" id="navbarNav"> <div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav" th:with="activeLink=${#request.requestURI}" sec:authorize="isAuthenticated()"> <ul class="navbar-nav" sec:authorize="!isAuthenticated()">
<a class="nav-link" href="/" <a class="nav-link" href="/login">Вход</a>
th:classappend="${#strings.equals(activeLink, '/')} ? 'active' : ''">Index</a> </ul>
<ul class="navbar-nav" sec:authorize="isAuthenticated()">
<a class="nav-link" href="/">Index</a>
<!--(login=${#authentication.name})--> <!--(login=${#authentication.name})-->
<a sec:authorize="hasRole('ROLE_ADMIN')" class="nav-link" th:href="@{/creatorAction(login=${#authentication.name})}" <a class="nav-link" sec:authorize="hasRole('ROLE_ADMIN')" th:href="@{/creatorAction(login=${#authentication.name})}">CreatorAction</a>
th:classappend="${#strings.equals(activeLink, '/creatorAction')} ? 'active' : ''">CreatorAction</a> <a class="nav-link" sec:authorize="hasRole('ROLE_USER')" th:href="@{/readerAction(readerLogin=${#authentication.name})}">ReaderAction</a>
<a sec:authorize="hasRole('ROLE_USER')" class="nav-link" th:href="@{/readerAction(readerLogin=${#authentication.name})}" <a sec:authorize="hasRole('ROLE_ADMIN')" class="nav-link" href="/users">Users</a>
th:classappend="${#strings.equals(activeLink, '/readerAction')} ? 'active' : ''">ReaderAction</a> <a class="nav-link" href="/manga">Catalog</a>
<a class="nav-link" href="/manga" <a class="nav-link" href="/swagger-ui/index.html">Документация REST API</a>
th:classappend="${#strings.equals(activeLink, '/manga')} ? 'active' : ''">Catalog</a> <a class="nav-link" href="/h2-console/">Консоль H2</a>
<a sec:authorize="hasRole('ROLE_ADMIN')" class="nav-link" href="/users"
th:classappend="${#strings.equals(activeLink, '/users')} ? 'active' : ''">Пользователи</a>
<a class="nav-link" href="/swagger-ui/index.html" target="_blank">Документация REST API</a>
<a class="nav-link" href="/h2-console/" target="_blank">Консоль H2</a>
<a class="nav-link" href="/logout"> <a class="nav-link" href="/logout">
Выход (<span th:text="${#authentication.name}"></span>) Выход (<span th:text="${#authentication.name}"></span>)
</a> </a>

View File

@ -5,6 +5,7 @@
<head> <head>
</head> </head>
<body> <body>
<div layout:fragment="content_header" th:text="'Главная'"></div>
<div layout:fragment="content"> <div layout:fragment="content">
<div>It's works!</div> <div>It's works!</div>
<a href="123">ERROR</a> <a href="123">ERROR</a>

View File

@ -28,3 +28,4 @@
</div> </div>
</body> </body>
</html> </html>
<!DOCTYPE html>