From 86343ae3a623c58b830e866a7177472c85b35a3b Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 4 Dec 2023 18:57:43 +0400 Subject: [PATCH] add spring security --- pom.xml | 20 +++++++ .../gipro/giprolab/services/UserMapper.java | 56 +++++++++++++++++++ src/main/resources/application.properties | 19 +++++-- 3 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/gipro/giprolab/services/UserMapper.java diff --git a/pom.xml b/pom.xml index 1aa79db..668a51a 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,26 @@ org.springframework.boot spring-boot-starter-data-jpa + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-aop + + + jakarta.validation + jakarta.validation-api + + + org.apache.commons + commons-lang3 + + + jakarta.persistence + jakarta.persistence-api + com.h2database h2 diff --git a/src/main/java/com/gipro/giprolab/services/UserMapper.java b/src/main/java/com/gipro/giprolab/services/UserMapper.java new file mode 100644 index 0000000..724a3a1 --- /dev/null +++ b/src/main/java/com/gipro/giprolab/services/UserMapper.java @@ -0,0 +1,56 @@ +package com.gipro.giprolab.services; + +import com.gipro.giprolab.models.User; +import com.gipro.giprolab.models.UserDto; +import com.gipro.giprolab.models.UserRole; +import com.gipro.giprolab.models.UserRoleDto; +import com.gipro.giprolab.repositories.UserRoleRepository; +import org.springframework.stereotype.Service; + +import java.util.Collections; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +@Service +public class UserMapper { + private final UserRoleRepository userRoleRepository; + + public UserMapper(UserRoleRepository userRoleRepository) { + this.userRoleRepository = userRoleRepository; + } + + public Set rolesFromDto(Set strings) { + return Optional.ofNullable(strings).orElse(Collections.emptySet()).stream() + .filter(Objects::nonNull) + .map(role -> userRoleRepository.findById(role.getId()).orElse(null)) + .filter(Objects::nonNull) + .collect(Collectors.toSet()); + } + + public UserDto userEntityToUserDto(User userEntity) { + if (userEntity == null) { + return null; + } + return new UserDto(userEntity); + } + + public User userDtoToUserEntity(UserDto userDto) { + if (userDto == null) { + return null; + } + final User user = new User(); + user.setId(userDto.getId()); + user.setLogin(userDto.getLogin()); + user.setFirstName(userDto.getFirstName()); + user.setLastName(userDto.getLastName()); + user.setEmail(userDto.getEmail()); + user.setActivated(userDto.isActivated()); + final Set roles = this.rolesFromDto(userDto.getRoles()); + if (!roles.isEmpty()) { + user.setRoles(roles); + } + return user; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f408190..d979872 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,6 +1,15 @@ -spring.jpa.hibernate.ddl-auto=update -spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/giproclientsandfilesdb -spring.datasource.username=root -spring.datasource.password= -spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +#spring.jpa.hibernate.ddl-auto=update +#spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/giproclientsandfilesdb +#spring.datasource.username=root +#spring.datasource.password= +#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #spring.jpa.show-sql: true +spring.main.allow-bean-definition-overriding=true +spring.jackson.serialization.fail-on-empty-beans=false +spring.datasource.url=jdbc:h2:file:./data/giproclientsandfilesdb +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password=password +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +spring.h2.console.enabled=true +spring.jpa.hibernate.ddl-auto=update \ No newline at end of file