add spring security

This commit is contained in:
Anton Romanov 2023-12-04 18:57:43 +04:00
parent e0bf9f5edc
commit 86343ae3a6
3 changed files with 90 additions and 5 deletions

20
pom.xml
View File

@ -21,6 +21,26 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.h2database</groupId> <groupId>com.h2database</groupId>
<artifactId>h2</artifactId> <artifactId>h2</artifactId>

View File

@ -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<UserRole> rolesFromDto(Set<UserRoleDto> 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<UserRole> roles = this.rolesFromDto(userDto.getRoles());
if (!roles.isEmpty()) {
user.setRoles(roles);
}
return user;
}
}

View File

@ -1,6 +1,15 @@
spring.jpa.hibernate.ddl-auto=update #spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/giproclientsandfilesdb #spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/giproclientsandfilesdb
spring.datasource.username=root #spring.datasource.username=root
spring.datasource.password= #spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true #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