diff --git a/.idea/compiler.xml b/.idea/compiler.xml index e15d93b..d476aa7 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -8,6 +8,7 @@ + diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..9c5b81b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/vega-hrm-auth/VegaHRM.Backend.vega-hrm-auth.main.iml b/.idea/modules/vega-hrm-auth/VegaHRM.Backend.vega-hrm-auth.main.iml new file mode 100644 index 0000000..b987421 --- /dev/null +++ b/.idea/modules/vega-hrm-auth/VegaHRM.Backend.vega-hrm-auth.main.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/vega-hrm-core/VegaHRM.Backend.vega-hrm-core.main.iml b/.idea/modules/vega-hrm-core/VegaHRM.Backend.vega-hrm-core.main.iml new file mode 100644 index 0000000..4321feb --- /dev/null +++ b/.idea/modules/vega-hrm-core/VegaHRM.Backend.vega-hrm-core.main.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/config/shared.properties b/config/shared.properties index 7f1a820..8d60eb9 100644 --- a/config/shared.properties +++ b/config/shared.properties @@ -26,10 +26,13 @@ spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true logging.level.org.hibernate.SQL=DEBUG logging.level.org.hibernate.type.descriptor.sql=TRACE -#spring.data.redis.sentinel.master=redismaster -#spring.data.redis.sentinel.nodes=10.22.18.138:26379,10.22.18.139:26379,10.22.18.140:26379 -#spring.data.redis.sentinel.password=dLF5ZnOeOHM6/pzTHezV+WNXU+F7ZBGu85f8bwzk -#spring.data.redis.sentinel.database=17 -cors.allowed-origins=http://localhost:4201,http://localhost:4202,http://localhost:4200,http://localhost:4203 + +# redis +redis.host=103.166.141.147 +redis.port=6379 +redis.password=VegaHrm@2025 +redis.database=0 + +cors.allowed-origins=* #vcb-lounge.redis.time-to-live=600 #spring.cache.type=simple \ No newline at end of file diff --git a/vega-hrm-auth/build.gradle b/vega-hrm-auth/build.gradle index 2d9fff3..20d1ffd 100644 --- a/vega-hrm-auth/build.gradle +++ b/vega-hrm-auth/build.gradle @@ -1,5 +1,7 @@ plugins { id 'java' + id 'org.springframework.boot' version '3.4.0' + id 'io.spring.dependency-management' version '1.1.6' } group = 'com.vega.hrm' @@ -10,10 +12,30 @@ repositories { } dependencies { - testImplementation platform('org.junit:junit-bom:5.10.0') - testImplementation 'org.junit.jupiter:junit-jupiter' + +} +dependencies { + implementation 'org.springframework.boot:spring-boot-starter-data-jpa:3.4.0' + implementation 'org.springframework.boot:spring-boot-starter-web:3.4.0' + implementation 'org.projectlombok:lombok:1.18.38' + implementation('org.springframework.boot:spring-boot-starter:3.4.0') { + exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' + } + implementation 'org.springframework.boot:spring-boot-starter-log4j2:3.4.0' + implementation 'org.springframework.boot:spring-boot-starter-validation:3.4.0' + implementation 'de.mkammerer:argon2-jvm:2.1' + annotationProcessor 'org.projectlombok:lombok:1.18.38' + implementation project(":vega-hrm-core") + implementation 'jakarta.annotation:jakarta.annotation-api:2.1.1' + } -test { - useJUnitPlatform() -} \ No newline at end of file +configurations { + all { + // Loại bỏ Logback + exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' + + // Loại bỏ logging mặc định + exclude group: 'ch.qos.logback', module: 'logback-classic' + } +} diff --git a/vega-hrm-auth/src/main/java/com/vega/hrm/AuthHelper.java b/vega-hrm-auth/src/main/java/com/vega/hrm/AuthHelper.java new file mode 100644 index 0000000..4f92392 --- /dev/null +++ b/vega-hrm-auth/src/main/java/com/vega/hrm/AuthHelper.java @@ -0,0 +1,82 @@ +package com.vega.hrm; + +import com.vega.hrm.dto.BoFunctionDto; +import com.vega.hrm.core.entities.BoFunction; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.SecureRandom; +import java.util.Comparator; +import java.util.List; + + +public class AuthHelper { + private AuthHelper() { + throw new IllegalStateException("Utility class"); + } + private static final String UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + private static final String LOWERCASE = "abcdefghijklmnopqrstuvwxyz"; + private static final String DIGITS = "0123456789"; + private static final String SPECIAL_CHARS = "!@#$%^&*()-_+=<>?/"; // tùy chọn + private static final String ALL_CHARS = UPPERCASE + LOWERCASE + DIGITS + SPECIAL_CHARS; + + private static final SecureRandom random = new SecureRandom(); + + public static String hashPassword(String userName, String password) { + try { + var input = userName + password + "&3%@6v"; + var digest = MessageDigest.getInstance("SHA-256"); + var hash = digest.digest(input.getBytes(StandardCharsets.UTF_8)); + var hexString = new StringBuilder(); + for (var b : hash) { + var hex = Integer.toHexString(0xff & b); + if (hex.length() == 1) { + hexString.append('0'); + } + hexString.append(hex); + } + return hexString.toString(); + } catch (Exception ex) { + return null; + } + } + + public static List getChildrenFunction(BoFunction function, List allFunctions) { + return allFunctions.stream() + .filter(x -> x.getParentId().equals(function.getId())) + .sorted(Comparator.comparingLong(BoFunction::getFunctionOrder)) + .map(x -> new BoFunctionDto(x, getChildrenFunction(x, allFunctions))) + .toList(); + } + + + public static String generateValidPassword(int length) { + if (length < 8 || length > 20) { + throw new IllegalArgumentException("Độ dài password phải từ 8 tới 20 ký tự."); + } + + StringBuilder password = new StringBuilder(length); + password.append(pickRandomChar(UPPERCASE)); + password.append(pickRandomChar(DIGITS)); + password.append(pickRandomChar(SPECIAL_CHARS)); + + for (int i = 3; i < length; i++) { + password.append(pickRandomChar(ALL_CHARS)); + } + return shuffleString(password.toString()); + } + + private static char pickRandomChar(String charSet) { + return charSet.charAt(random.nextInt(charSet.length())); + } + + private static String shuffleString(String input) { + char[] chars = input.toCharArray(); + for (int i = chars.length - 1; i > 0; i--) { + int index = random.nextInt(i + 1); + char temp = chars[index]; + chars[index] = chars[i]; + chars[i] = temp; + } + return new String(chars); + } +} diff --git a/vega-hrm-auth/src/main/java/com/vega/hrm/Main.java b/vega-hrm-auth/src/main/java/com/vega/hrm/Main.java deleted file mode 100644 index 361c303..0000000 --- a/vega-hrm-auth/src/main/java/com/vega/hrm/Main.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.vega.hrm; - -//TIP To Run code, press or -// click the icon in the gutter. -public class Main { - - public static void main(String[] args) { - //TIP Press with your caret at the highlighted text - // to see how IntelliJ IDEA suggests fixing it. - System.out.printf("Hello and welcome!"); - - for (int i = 1; i <= 5; i++) { - //TIP Press to start debugging your code. We have set one breakpoint - // for you, but you can always add more by pressing . - System.out.println("i = " + i); - } - } -} \ No newline at end of file diff --git a/vega-hrm-auth/src/main/java/com/vega/hrm/VegaHrmAuthApplication.java b/vega-hrm-auth/src/main/java/com/vega/hrm/VegaHrmAuthApplication.java new file mode 100644 index 0000000..0dffce7 --- /dev/null +++ b/vega-hrm-auth/src/main/java/com/vega/hrm/VegaHrmAuthApplication.java @@ -0,0 +1,15 @@ +package com.vega.hrm; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@SpringBootApplication(scanBasePackages = "com.vega.hrm") +@EnableTransactionManagement +public class VegaHrmAuthApplication { + public static void main(String[] args) { + SpringApplication.run(VegaHrmAuthApplication.class, args); + } +} \ No newline at end of file diff --git a/vega-hrm-auth/src/main/java/com/vega/hrm/controller/UserController.java b/vega-hrm-auth/src/main/java/com/vega/hrm/controller/UserController.java new file mode 100644 index 0000000..b8259d9 --- /dev/null +++ b/vega-hrm-auth/src/main/java/com/vega/hrm/controller/UserController.java @@ -0,0 +1,29 @@ +package com.vega.hrm.controller; + +import com.vega.hrm.core.models.responses.BaseResponse; +import com.vega.hrm.request.user.CreateUserRequest; +import com.vega.hrm.request.user.LoginRequest; +import com.vega.hrm.service.UserService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/auth/user") +@RequiredArgsConstructor +public class UserController { + private final UserService userService; + + @PostMapping("/login") + public ResponseEntity> login(@RequestBody LoginRequest request) { + return ResponseEntity.ok(userService.login(request)); + } + + @PostMapping("/insert") + public ResponseEntity> insert(@RequestBody CreateUserRequest request) { + return ResponseEntity.ok(userService.insert(request)); + } +} diff --git a/vega-hrm-auth/src/main/java/com/vega/hrm/dto/BoFunctionDto.java b/vega-hrm-auth/src/main/java/com/vega/hrm/dto/BoFunctionDto.java new file mode 100644 index 0000000..8d7c193 --- /dev/null +++ b/vega-hrm-auth/src/main/java/com/vega/hrm/dto/BoFunctionDto.java @@ -0,0 +1,30 @@ +package com.vega.hrm.dto; + +import com.vega.hrm.core.entities.BoFunction; +import java.util.List; +import java.util.UUID; +import lombok.Getter; + + +@Getter +public class BoFunctionDto { + private final UUID id; + private final String funcName; + private final Long funcOrder; + private final Long funcDisplay; + private final Long funcLevel; + private final UUID parentId; + private final String funcUrl; + private final List children; + + public BoFunctionDto(BoFunction boFunction, List children) { + this.id = boFunction.getId(); + this.funcName = boFunction.getFunctionName(); + this.funcOrder = boFunction.getFunctionOrder(); + this.funcDisplay = Long.valueOf(String.valueOf(boFunction.getFunctionDisplay())); + this.funcLevel = boFunction.getFunctionLevel(); + this.parentId = boFunction.getParentId(); + this.funcUrl = boFunction.getFunctionUrl(); + this.children = children; + } +} diff --git a/vega-hrm-auth/src/main/java/com/vega/hrm/request/user/CreateUserRequest.java b/vega-hrm-auth/src/main/java/com/vega/hrm/request/user/CreateUserRequest.java new file mode 100644 index 0000000..a80c6a9 --- /dev/null +++ b/vega-hrm-auth/src/main/java/com/vega/hrm/request/user/CreateUserRequest.java @@ -0,0 +1,14 @@ +package com.vega.hrm.request.user; + +import java.util.UUID; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class CreateUserRequest { + private String userName; + private String password; + private String fullName; + private String roleId; +} \ No newline at end of file diff --git a/vega-hrm-auth/src/main/java/com/vega/hrm/request/user/LoginRequest.java b/vega-hrm-auth/src/main/java/com/vega/hrm/request/user/LoginRequest.java new file mode 100644 index 0000000..b3ad0e5 --- /dev/null +++ b/vega-hrm-auth/src/main/java/com/vega/hrm/request/user/LoginRequest.java @@ -0,0 +1,9 @@ +package com.vega.hrm.request.user; + +import lombok.Getter; + +@Getter +public class LoginRequest { + private String userName; + private String password; +} diff --git a/vega-hrm-auth/src/main/java/com/vega/hrm/response/LoginResponse.java b/vega-hrm-auth/src/main/java/com/vega/hrm/response/LoginResponse.java new file mode 100644 index 0000000..a6f4f36 --- /dev/null +++ b/vega-hrm-auth/src/main/java/com/vega/hrm/response/LoginResponse.java @@ -0,0 +1,17 @@ +package com.vega.hrm.response; + +import com.vega.hrm.core.entities.BoUser; +import java.util.List; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.Getter; +import lombok.Setter; + +@Data +@Builder +@AllArgsConstructor +public class LoginResponse { + private BoUser user; + private String token; +} diff --git a/vega-hrm-auth/src/main/java/com/vega/hrm/service/UserService.java b/vega-hrm-auth/src/main/java/com/vega/hrm/service/UserService.java new file mode 100644 index 0000000..b34d468 --- /dev/null +++ b/vega-hrm-auth/src/main/java/com/vega/hrm/service/UserService.java @@ -0,0 +1,86 @@ +package com.vega.hrm.service; + +import com.vega.hrm.AuthHelper; +import com.vega.hrm.core.constants.CommonConst; +import com.vega.hrm.core.entities.BoUser; +import com.vega.hrm.core.helpers.JwtHelper; +import com.vega.hrm.core.models.responses.BaseResponse; +import com.vega.hrm.core.repositories.CoreUserRepository; +import com.vega.hrm.core.service.RedisService; +import com.vega.hrm.request.user.CreateUserRequest; +import com.vega.hrm.request.user.LoginRequest; +import com.vega.hrm.response.LoginResponse; +import java.time.Instant; +import java.util.HashMap; +import java.util.UUID; +import lombok.RequiredArgsConstructor; +import org.apache.logging.log4j.util.Strings; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +public class UserService { + private final CoreUserRepository userRepository; + private final RedisService redisService; + + public BaseResponse login(LoginRequest request) { + var user = userRepository.findByUserName(request.getUserName()); + if (user != null && user.getPassword() != null) { + if (user.getPassword() + .equals(AuthHelper.hashPassword(request.getUserName(), request.getPassword()))) { + + var uuid = UUID.randomUUID().toString(); + user.setUuid(uuid); + user.setNumberOfFailedLogins(0L); + user.setLastLoginTime(Instant.now()); + userRepository.save(user); + var claims = new HashMap(); + claims.put("UserId", user.getId()); + claims.put("UserName", user.getUserName()); + var token = JwtHelper.generateToken(user.getUserName(), user.getId(), UUID.randomUUID()); + if (Strings.isBlank(token)) { + return BaseResponse.internalSystemError(); + } + redisService.hSet(CommonConst.TOKEN,user.getId().toString(),token); + + var baseResponseLogin = BaseResponse.builder().code("00"); + baseResponseLogin.data( + LoginResponse.builder() + .user(user) + .token(token) + .build()); + return baseResponseLogin.build(); + } + if (user.getStatus().equals("1")) { + user.setNumberOfFailedLogins(user.getNumberOfFailedLogins() + 1); + userRepository.save(user); + } + } + + return BaseResponse.invalid( + "UserName or Password is valid. Please check."); + } + + @Transactional + public BaseResponse insert(CreateUserRequest request) { + var user = userRepository.findByUserName(request.getUserName()); + if (user != null) { + return BaseResponse.success("User created unsuccessful"); + } + var newUser = new BoUser(); + newUser.setId(UUID.randomUUID()); + newUser.setUserName(request.getUserName()); + newUser.setFullName(request.getFullName()); + newUser.setRoleId(UUID.randomUUID()); + newUser.setPassword(AuthHelper.hashPassword(request.getUserName(), request.getPassword())); + newUser.setStatus("1"); + newUser.setPwdExpireDate(Instant.now().plusSeconds(90L * 24 * 60 * 60)); + newUser.setCreatedDate(Instant.now()); + newUser.setCreatedUser("system"); + newUser.setNumberOfFailedLogins(0L); + newUser.setIsPasswordChanged(0L); + userRepository.save(newUser); + return BaseResponse.success("User created successfully"); + } +} diff --git a/vega-hrm-auth/src/main/resources/application.properties b/vega-hrm-auth/src/main/resources/application.properties new file mode 100644 index 0000000..ef47687 --- /dev/null +++ b/vega-hrm-auth/src/main/resources/application.properties @@ -0,0 +1,9 @@ +spring.application.name=vega-hrm-auth +spring.jpa.open-in-view=false +server.port=8089 +vega.hrm.postgre.enabled=true +vega.jpa.repository.basePackage=com.vega.hrm.core.repositories +vega.jpa.entity.basePackage=com.vega.hrm.core.entities +spring.config.import=file:config/shared.properties +logging.config=file:config/log4j2.properties + diff --git a/vega-hrm-core/build.gradle b/vega-hrm-core/build.gradle index d39a18c..7201eec 100644 --- a/vega-hrm-core/build.gradle +++ b/vega-hrm-core/build.gradle @@ -30,7 +30,8 @@ dependencies { implementation('org.springframework.boot:spring-boot-starter') { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' } - + implementation("org.springframework.data:spring-data-redis:3.5.5") + implementation 'io.lettuce:lettuce-core' implementation 'org.postgresql:postgresql:42.7.3' implementation 'org.springframework.boot:spring-boot-starter-log4j2:3.4.0' implementation 'org.modelmapper:modelmapper:3.2.1' diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/VegaHrmCoreApplication.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/VegaHrmCoreApplication.java similarity index 76% rename from vega-hrm-core/src/main/java/com/vega/hrm/VegaHrmCoreApplication.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/VegaHrmCoreApplication.java index 2594686..29c3fcb 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/VegaHrmCoreApplication.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/VegaHrmCoreApplication.java @@ -1,4 +1,4 @@ -package com.vega.hrm; +package com.vega.hrm.core; public class VegaHrmCoreApplication { diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/core/configs/CharsetFilterConfig.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/configs/CharsetFilterConfig.java new file mode 100644 index 0000000..68d2a8e --- /dev/null +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/configs/CharsetFilterConfig.java @@ -0,0 +1,22 @@ +package com.vega.hrm.core.configs; + +import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; +import org.springframework.web.filter.CharacterEncodingFilter; + +@Configuration +public class CharsetFilterConfig { + @Bean + public FilterRegistrationBean encodingFilter() { + FilterRegistrationBean bean = new FilterRegistrationBean<>(); + CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter(); + encodingFilter.setEncoding("UTF-8"); + encodingFilter.setForceEncoding(true); + bean.setFilter(encodingFilter); + bean.addUrlPatterns("/*"); + bean.setOrder(Ordered.HIGHEST_PRECEDENCE); + return bean; + } +} \ No newline at end of file diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/core/configs/RedisConfig.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/configs/RedisConfig.java new file mode 100644 index 0000000..9933bb5 --- /dev/null +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/configs/RedisConfig.java @@ -0,0 +1,50 @@ +// RedisConfig.java +package com.vega.hrm.core.configs; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisStandaloneConfiguration; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.StringRedisTemplate; + +@Configuration +public class RedisConfig { + + @Value("${redis.host:localhost}") + private String redisHost; + + @Value("${redis.port:6379}") + private int redisPort; + + @Value("${redis.password:}") + private String redisPassword; + + @Value("${redis.database:0}") + private int redisDatabase; + + @Bean + public LettuceConnectionFactory redisConnectionFactory() { + RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); + config.setHostName(redisHost); + config.setPort(redisPort); + config.setDatabase(redisDatabase); + if (redisPassword != null && !redisPassword.isBlank()) { + config.setPassword(redisPassword); + } + return new LettuceConnectionFactory(config); + } + + @Bean + public RedisTemplate redisTemplate(LettuceConnectionFactory connectionFactory) { + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(connectionFactory); + return template; + } + + @Bean + public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory connectionFactory) { + return new StringRedisTemplate(connectionFactory); + } +} \ No newline at end of file diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/configs/VegaDatabaseConfig.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/configs/VegaDatabaseConfig.java similarity index 90% rename from vega-hrm-core/src/main/java/com/vega/hrm/configs/VegaDatabaseConfig.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/configs/VegaDatabaseConfig.java index e33fc2d..a86f928 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/configs/VegaDatabaseConfig.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/configs/VegaDatabaseConfig.java @@ -1,4 +1,4 @@ -package com.vega.hrm.configs; +package com.vega.hrm.core.configs; import com.zaxxer.hikari.HikariDataSource; import java.sql.Connection; @@ -26,7 +26,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; @Slf4j @Order(2) @EnableJpaRepositories( - basePackages = "${vega.jpa.repository.basePackage}", + basePackages = "com.vega.hrm.core.repositories", entityManagerFactoryRef = "vegaHrmEntityManager", transactionManagerRef = "vegaHrmTransactionManager" ) @@ -34,8 +34,8 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; @EnableTransactionManagement public class VegaDatabaseConfig { - @Value("${vega.jpa.entity.basePackage:com.core.entity}") - private String entityBasePackage; +// @Value("com.vega.hrm.core.entities") +// private String entityBasePackage; @Value("${app.datasource.vega.hikari.maximum-pool-size:10}") private Integer hikariMaximumPoolSize; @@ -74,6 +74,13 @@ public class VegaDatabaseConfig { } private DataSource configureHikariDataSource(DataSourceProperties properties) { + String url = properties.getUrl(); + if (url != null && url.contains("Asia/Saigon")) { + url = url.replace("Asia/Saigon", "Asia/Ho_Chi_Minh"); + properties.setUrl(url); + log.warn("Rewrote JDBC URL timezone from Asia/Saigon to Asia/Ho_Chi_Minh"); + } + var dataSource = properties .initializeDataSourceBuilder() .type(HikariDataSource.class) @@ -101,7 +108,7 @@ public class VegaDatabaseConfig { if (validationTimeout != null) { dataSource.setValidationTimeout(validationTimeout); } - + dataSource.setConnectionInitSql("SET TIME ZONE 'Asia/Ho_Chi_Minh'"); // PostgreSQL driver dataSource.setDriverClassName("org.postgresql.Driver"); @@ -119,7 +126,7 @@ public class VegaDatabaseConfig { var em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource); - em.setPackagesToScan(entityBasePackage); + em.setPackagesToScan("com.vega.hrm.core.entities"); em.setPersistenceUnitName("db1"); var vendorAdapter = new HibernateJpaVendorAdapter(); @@ -140,7 +147,7 @@ public class VegaDatabaseConfig { } private void loggingDatabaseSource(LocalContainerEntityManagerFactoryBean em) { - log.info("vcbDatabaseSourceConfig entityBasePackage : {}", entityBasePackage); + log.info("vcbDatabaseSourceConfig entityBasePackage : {}", "com.vega.hrm.core.entities"); try (Connection connection = Objects.requireNonNull(em.getDataSource()).getConnection()) { log.info("Database URL: {}", connection.getMetaData().getURL()); diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/core/constants/CommonConst.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/constants/CommonConst.java new file mode 100644 index 0000000..b85683a --- /dev/null +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/constants/CommonConst.java @@ -0,0 +1,6 @@ +package com.vega.hrm.core.constants; + +public class CommonConst { + public static final String USER_ID = "user_id"; + public static final String TOKEN = "token"; +} diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/constants/KeySecurityConst.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/constants/KeySecurityConst.java similarity index 83% rename from vega-hrm-core/src/main/java/com/vega/hrm/constants/KeySecurityConst.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/constants/KeySecurityConst.java index 95972cf..84323ee 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/constants/KeySecurityConst.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/constants/KeySecurityConst.java @@ -1,4 +1,4 @@ -package com.vega.hrm.constants; +package com.vega.hrm.core.constants; public class KeySecurityConst { public static String KEY_SECURITY_DATABASE = "lDCfYiZ5rxH1G1ElIth9ppqED6MgaLKZ"; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/constants/ResponseCodeConst.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/constants/ResponseCodeConst.java similarity index 86% rename from vega-hrm-core/src/main/java/com/vega/hrm/constants/ResponseCodeConst.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/constants/ResponseCodeConst.java index 965bdfe..c29f72e 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/constants/ResponseCodeConst.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/constants/ResponseCodeConst.java @@ -1,4 +1,4 @@ -package com.vega.hrm.constants; +package com.vega.hrm.core.constants; public class ResponseCodeConst { public static final String SUCCESS = "00"; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/constants/ResponseMessageConst.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/constants/ResponseMessageConst.java similarity index 75% rename from vega-hrm-core/src/main/java/com/vega/hrm/constants/ResponseMessageConst.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/constants/ResponseMessageConst.java index 3c0e4aa..cb3b241 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/constants/ResponseMessageConst.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/constants/ResponseMessageConst.java @@ -1,4 +1,4 @@ -package com.vega.hrm.constants; +package com.vega.hrm.core.constants; public class ResponseMessageConst { public static final String INTERNAL_SYSTEM_ERROR = "Lỗi hệ thống"; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/constants/UserHistoryConstant.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/constants/UserHistoryConstant.java similarity index 98% rename from vega-hrm-core/src/main/java/com/vega/hrm/constants/UserHistoryConstant.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/constants/UserHistoryConstant.java index ec4dfe6..cf5edfd 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/constants/UserHistoryConstant.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/constants/UserHistoryConstant.java @@ -1,4 +1,4 @@ -package com.vega.hrm.constants; +package com.vega.hrm.core.constants; import java.util.HashMap; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/entity/BoFunction.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/entities/BoFunction.java similarity index 95% rename from vega-hrm-core/src/main/java/com/vega/hrm/entity/BoFunction.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/entities/BoFunction.java index a4b29fa..adb4f01 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/entity/BoFunction.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/entities/BoFunction.java @@ -1,4 +1,4 @@ -package com.vega.hrm.entity; +package com.vega.hrm.core.entities; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -42,7 +42,7 @@ public class BoFunction { @NotNull @Column(name = "parent_id", nullable = false) - private Long parentId; + private UUID parentId; @Size(max = 20) @NotNull diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/entity/BoNewFuncAuth.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/entities/BoNewFuncAuth.java similarity index 95% rename from vega-hrm-core/src/main/java/com/vega/hrm/entity/BoNewFuncAuth.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/entities/BoNewFuncAuth.java index 5c48bae..bd768d7 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/entity/BoNewFuncAuth.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/entities/BoNewFuncAuth.java @@ -1,4 +1,4 @@ -package com.vega.hrm.entity; +package com.vega.hrm.core.entities; import jakarta.persistence.Column; import jakarta.persistence.Entity; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/entity/BoRole.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/entities/BoRole.java similarity index 96% rename from vega-hrm-core/src/main/java/com/vega/hrm/entity/BoRole.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/entities/BoRole.java index 2ef49ba..a930200 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/entity/BoRole.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/entities/BoRole.java @@ -1,4 +1,4 @@ -package com.vega.hrm.entity; +package com.vega.hrm.core.entities; import jakarta.persistence.Column; import jakarta.persistence.Entity; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/entity/BoRoleFunc.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/entities/BoRoleFunc.java similarity index 97% rename from vega-hrm-core/src/main/java/com/vega/hrm/entity/BoRoleFunc.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/entities/BoRoleFunc.java index 9bdf6d6..cf5cf62 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/entity/BoRoleFunc.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/entities/BoRoleFunc.java @@ -1,4 +1,4 @@ -package com.vega.hrm.entity; +package com.vega.hrm.core.entities; import jakarta.persistence.Column; import jakarta.persistence.Entity; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/entity/BoUser.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/entities/BoUser.java similarity index 97% rename from vega-hrm-core/src/main/java/com/vega/hrm/entity/BoUser.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/entities/BoUser.java index 90501c3..8f6641d 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/entity/BoUser.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/entities/BoUser.java @@ -1,4 +1,4 @@ -package com.vega.hrm.entity; +package com.vega.hrm.core.entities; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -56,7 +56,7 @@ public class BoUser { private String createdUser; @Column(name = "role_id") - private Long roleId; + private UUID roleId; @NotNull @ColumnDefault("0") diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/exceptions/GlobalExceptionHandler.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/exceptions/GlobalExceptionHandler.java similarity index 90% rename from vega-hrm-core/src/main/java/com/vega/hrm/exceptions/GlobalExceptionHandler.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/exceptions/GlobalExceptionHandler.java index 7e9d863..69e1549 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/exceptions/GlobalExceptionHandler.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/exceptions/GlobalExceptionHandler.java @@ -1,10 +1,10 @@ -package com.vega.hrm.exceptions; +package com.vega.hrm.core.exceptions; -import com.vega.hrm.constants.ResponseCodeConst; -import com.vega.hrm.constants.ResponseMessageConst; -import com.vega.hrm.models.responses.BaseResponse; +import com.vega.hrm.core.constants.ResponseCodeConst; +import com.vega.hrm.core.constants.ResponseMessageConst; +import com.vega.hrm.core.models.responses.BaseResponse; import java.util.HashMap; import java.util.Map; import lombok.extern.slf4j.Slf4j; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/core/filters/AuthorizationFilter.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/filters/AuthorizationFilter.java new file mode 100644 index 0000000..7f74f6f --- /dev/null +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/filters/AuthorizationFilter.java @@ -0,0 +1,78 @@ +package com.vega.hrm.core.filters; + + +import com.vega.hrm.core.constants.CommonConst; +import com.vega.hrm.core.constants.KeySecurityConst; +import com.vega.hrm.core.helpers.JwtHelper; +import com.vega.hrm.core.helpers.LogHelper; +import com.vega.hrm.core.service.RedisService; +import io.jsonwebtoken.ExpiredJwtException; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.List; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import org.apache.logging.log4j.ThreadContext; +import org.apache.logging.log4j.util.Strings; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; +import org.springframework.web.filter.OncePerRequestFilter; + + + +@Component +@Order(2) +@RequiredArgsConstructor +public class AuthorizationFilter extends OncePerRequestFilter { + + private static final List EXCLUDE_URIS = List.of("/auth/user/login"); + private final RedisService redisService; + + @Override + protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull FilterChain filterChain) { + try { + var uri = request.getRequestURI(); + if (EXCLUDE_URIS.contains(uri) || uri.contains("actuator")) { + filterChain.doFilter(request, response); + return; + } + var token = ""; + var header = request.getHeader("Authorization"); + if (header != null && header.startsWith("Bearer ")) { + token = header.substring(7); + } + if (Strings.isBlank(token)) { + throwResponse(response); + return; + } + var claims = JwtHelper.extractUsername(token); + if (claims != null) { + var userId = claims.get("userId").toString(); + var userName = claims.get("userName").toString(); + var roleId = claims.get("roleId").toString(); + + ThreadContext.put("userId", userId); + ThreadContext.put("userName", userName); + ThreadContext.put("roleId", roleId); + + if (token.equals(redisService.hGet(CommonConst.TOKEN, userId))) { + filterChain.doFilter(request, response); + } + } + } catch (ExpiredJwtException e) { + // + } catch (Exception e) { + LogHelper.error(e); + } + throwResponse(response); + } + + private void throwResponse(HttpServletResponse response) { + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + + LogHelper.info("Unauthorized access"); + } +} diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/filters/CorsFilter.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/filters/CorsFilter.java similarity index 96% rename from vega-hrm-core/src/main/java/com/vega/hrm/filters/CorsFilter.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/filters/CorsFilter.java index ea46f4b..fd2a222 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/filters/CorsFilter.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/filters/CorsFilter.java @@ -1,7 +1,7 @@ -package com.vega.hrm.filters; +package com.vega.hrm.core.filters; -import com.vega.hrm.helpers.LogHelper; +import com.vega.hrm.core.helpers.LogHelper; import jakarta.servlet.FilterChain; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/helpers/CommonHelper.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/CommonHelper.java similarity index 99% rename from vega-hrm-core/src/main/java/com/vega/hrm/helpers/CommonHelper.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/CommonHelper.java index fb06741..16b51a7 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/helpers/CommonHelper.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/CommonHelper.java @@ -1,4 +1,4 @@ -package com.vega.hrm.helpers; +package com.vega.hrm.core.helpers; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.servlet.http.HttpServletRequest; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/helpers/CryptoHelper.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/CryptoHelper.java similarity index 96% rename from vega-hrm-core/src/main/java/com/vega/hrm/helpers/CryptoHelper.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/CryptoHelper.java index cb07b08..a7f40fa 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/helpers/CryptoHelper.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/CryptoHelper.java @@ -1,4 +1,4 @@ -package com.vega.hrm.helpers; +package com.vega.hrm.core.helpers; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/helpers/DateHelper.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/DateHelper.java similarity index 99% rename from vega-hrm-core/src/main/java/com/vega/hrm/helpers/DateHelper.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/DateHelper.java index 88d246f..8c7b7cd 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/helpers/DateHelper.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/DateHelper.java @@ -1,4 +1,4 @@ -package com.vega.hrm.helpers; +package com.vega.hrm.core.helpers; import java.text.ParseException; import java.text.SimpleDateFormat; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/helpers/JsonConvertHelper.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/JsonConvertHelper.java similarity index 97% rename from vega-hrm-core/src/main/java/com/vega/hrm/helpers/JsonConvertHelper.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/JsonConvertHelper.java index 5687c5d..9db3a19 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/helpers/JsonConvertHelper.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/JsonConvertHelper.java @@ -1,4 +1,4 @@ -package com.vega.hrm.helpers; +package com.vega.hrm.core.helpers; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/helpers/JwtHelper.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/JwtHelper.java similarity index 91% rename from vega-hrm-core/src/main/java/com/vega/hrm/helpers/JwtHelper.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/JwtHelper.java index c99605c..22a821f 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/helpers/JwtHelper.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/JwtHelper.java @@ -1,4 +1,4 @@ -package com.vega.hrm.helpers; +package com.vega.hrm.core.helpers; import io.jsonwebtoken.Claims; import io.jsonwebtoken.ExpiredJwtException; @@ -10,6 +10,7 @@ import java.nio.charset.StandardCharsets; import java.security.Key; import java.util.Date; import java.util.Map; +import java.util.UUID; import javax.crypto.spec.SecretKeySpec; import lombok.extern.slf4j.Slf4j; @@ -20,11 +21,11 @@ public class JwtHelper { private static final long EXPIRATION_TIME_MS = 600_000; // 10 phut private static final Key SIGNING_KEY = generateSigningKey(); - public static String generateToken(String username, Long userId, Long roleId) { + public static String generateToken(String username, UUID userId, UUID roleId) { return Jwts.builder() .claims(Map.of( - "userId", userId, - "roleId", roleId, + "userId", userId.toString(), + "roleId", roleId.toString(), "userName", username )) .subject(username) diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/helpers/LogHelper.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/LogHelper.java similarity index 97% rename from vega-hrm-core/src/main/java/com/vega/hrm/helpers/LogHelper.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/LogHelper.java index f96f9f4..aa96b87 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/helpers/LogHelper.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/LogHelper.java @@ -1,4 +1,4 @@ -package com.vega.hrm.helpers; +package com.vega.hrm.core.helpers; import lombok.extern.slf4j.Slf4j; import org.apache.logging.log4j.ThreadContext; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/helpers/LogHisTemplateHelper.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/LogHisTemplateHelper.java similarity index 96% rename from vega-hrm-core/src/main/java/com/vega/hrm/helpers/LogHisTemplateHelper.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/LogHisTemplateHelper.java index a5dae2e..ff26c44 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/helpers/LogHisTemplateHelper.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/LogHisTemplateHelper.java @@ -1,4 +1,4 @@ -package com.vega.hrm.helpers; +package com.vega.hrm.core.helpers; import java.util.HashMap; import org.apache.logging.log4j.ThreadContext; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/helpers/StringHelper.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/StringHelper.java similarity index 95% rename from vega-hrm-core/src/main/java/com/vega/hrm/helpers/StringHelper.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/StringHelper.java index 19bc422..e578872 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/helpers/StringHelper.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/helpers/StringHelper.java @@ -1,4 +1,4 @@ -package com.vega.hrm.helpers; +package com.vega.hrm.core.helpers; import java.security.SecureRandom; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/core/models/RedisEccKeyPair.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/RedisEccKeyPair.java new file mode 100644 index 0000000..5837978 --- /dev/null +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/RedisEccKeyPair.java @@ -0,0 +1,16 @@ +package com.vega.hrm.core.models; + +import lombok.Getter; +import lombok.NoArgsConstructor; +@Getter +@NoArgsConstructor +public class RedisEccKeyPair { + private String privateKey; + private String expiration; + + public RedisEccKeyPair(String privateKey) { + this.privateKey = privateKey; + + } + +} diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/models/requests/BaseDecryptedRequest.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/requests/BaseDecryptedRequest.java similarity index 81% rename from vega-hrm-core/src/main/java/com/vega/hrm/models/requests/BaseDecryptedRequest.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/models/requests/BaseDecryptedRequest.java index b7322c4..391aa92 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/models/requests/BaseDecryptedRequest.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/requests/BaseDecryptedRequest.java @@ -1,4 +1,4 @@ -package com.vega.hrm.models.requests; +package com.vega.hrm.core.models.requests; import lombok.Getter; import lombok.NoArgsConstructor; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/models/requests/BaseRequest.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/requests/BaseRequest.java similarity index 59% rename from vega-hrm-core/src/main/java/com/vega/hrm/models/requests/BaseRequest.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/models/requests/BaseRequest.java index d363f4f..52bad64 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/models/requests/BaseRequest.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/requests/BaseRequest.java @@ -1,4 +1,4 @@ -package com.vega.hrm.models.requests; +package com.vega.hrm.core.models.requests; import lombok.Getter; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/models/requests/ChangeStatusRequest.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/requests/ChangeStatusRequest.java similarity index 79% rename from vega-hrm-core/src/main/java/com/vega/hrm/models/requests/ChangeStatusRequest.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/models/requests/ChangeStatusRequest.java index 014f69d..665aef9 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/models/requests/ChangeStatusRequest.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/requests/ChangeStatusRequest.java @@ -1,4 +1,4 @@ -package com.vega.hrm.models.requests; +package com.vega.hrm.core.models.requests; import lombok.Getter; import lombok.Setter; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/models/requests/CreateUserHistoryRequest.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/requests/CreateUserHistoryRequest.java similarity index 89% rename from vega-hrm-core/src/main/java/com/vega/hrm/models/requests/CreateUserHistoryRequest.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/models/requests/CreateUserHistoryRequest.java index 0a596f6..5b0ecf5 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/models/requests/CreateUserHistoryRequest.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/requests/CreateUserHistoryRequest.java @@ -1,4 +1,4 @@ -package com.vega.hrm.models.requests; +package com.vega.hrm.core.models.requests; import lombok.AllArgsConstructor; import lombok.Builder; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/models/requests/GetByIdRequest.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/requests/GetByIdRequest.java similarity index 75% rename from vega-hrm-core/src/main/java/com/vega/hrm/models/requests/GetByIdRequest.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/models/requests/GetByIdRequest.java index cbf9fb5..5ea26dd 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/models/requests/GetByIdRequest.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/requests/GetByIdRequest.java @@ -1,4 +1,4 @@ -package com.vega.hrm.models.requests; +package com.vega.hrm.core.models.requests; import lombok.Getter; import lombok.Setter; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/models/requests/PagingRequest.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/requests/PagingRequest.java similarity index 94% rename from vega-hrm-core/src/main/java/com/vega/hrm/models/requests/PagingRequest.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/models/requests/PagingRequest.java index c572c80..125a346 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/models/requests/PagingRequest.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/requests/PagingRequest.java @@ -1,4 +1,4 @@ -package com.vega.hrm.models.requests; +package com.vega.hrm.core.models.requests; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.Positive; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/models/responses/BaseEncryptedResponse.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/responses/BaseEncryptedResponse.java similarity index 61% rename from vega-hrm-core/src/main/java/com/vega/hrm/models/responses/BaseEncryptedResponse.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/models/responses/BaseEncryptedResponse.java index 322b5b9..a859086 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/models/responses/BaseEncryptedResponse.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/responses/BaseEncryptedResponse.java @@ -1,4 +1,4 @@ -package com.vega.hrm.models.responses; +package com.vega.hrm.core.models.responses; public record BaseEncryptedResponse(String key, String response) { } diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/models/responses/BaseResponse.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/responses/BaseResponse.java similarity index 76% rename from vega-hrm-core/src/main/java/com/vega/hrm/models/responses/BaseResponse.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/models/responses/BaseResponse.java index c664309..fa3550f 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/models/responses/BaseResponse.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/responses/BaseResponse.java @@ -1,8 +1,8 @@ -package com.vega.hrm.models.responses; +package com.vega.hrm.core.models.responses; -import com.vega.hrm.constants.ResponseCodeConst; +import com.vega.hrm.core.constants.ResponseCodeConst; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; @@ -27,28 +27,29 @@ public class BaseResponse { private String message; private T data; - public static BaseResponse Success(String msg) { + + public static BaseResponse success(String msg) { return BaseResponse.builder() .code(ResponseCodeConst.SUCCESS) .message(msg) .build(); } - public static BaseResponse Invalid(String msg) { + public static BaseResponse invalid(String msg) { return BaseResponse.builder() .code(ResponseCodeConst.INVALID) .message(msg) .build(); } - public static BaseResponse NotFound(String msg) { + public static BaseResponse notFound(String msg) { return BaseResponse.builder() .code(ResponseCodeConst.NOT_FOUND) .message(msg) .build(); } - public static BaseResponse InternalSystemError() { + public static BaseResponse internalSystemError() { return BaseResponse.builder() .code(ResponseCodeConst.INTERNAL_SYSTEM_ERROR) .message("Internal System Error") diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/models/responses/PagedList.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/responses/PagedList.java similarity index 90% rename from vega-hrm-core/src/main/java/com/vega/hrm/models/responses/PagedList.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/models/responses/PagedList.java index f7c66c3..a4fb443 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/models/responses/PagedList.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/responses/PagedList.java @@ -1,4 +1,4 @@ -package com.vega.hrm.models.responses; +package com.vega.hrm.core.models.responses; import java.util.List; import lombok.AllArgsConstructor; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/models/responses/PagedListResponse.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/responses/PagedListResponse.java similarity index 84% rename from vega-hrm-core/src/main/java/com/vega/hrm/models/responses/PagedListResponse.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/models/responses/PagedListResponse.java index 5abe019..d7ec962 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/models/responses/PagedListResponse.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/models/responses/PagedListResponse.java @@ -1,7 +1,7 @@ -package com.vega.hrm.models.responses; +package com.vega.hrm.core.models.responses; -import com.vega.hrm.constants.ResponseCodeConst; +import com.vega.hrm.core.constants.ResponseCodeConst; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/core/repositories/CoreFunctionRepository.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/repositories/CoreFunctionRepository.java new file mode 100644 index 0000000..c4eeb51 --- /dev/null +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/repositories/CoreFunctionRepository.java @@ -0,0 +1,15 @@ +package com.vega.hrm.core.repositories; + +import com.vega.hrm.core.entities.BoFunction; +import java.util.List; +import java.util.UUID; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +@Repository +public interface CoreFunctionRepository extends JpaRepository { + +} + diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/core/repositories/CoreRoleRepository.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/repositories/CoreRoleRepository.java new file mode 100644 index 0000000..08fff68 --- /dev/null +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/repositories/CoreRoleRepository.java @@ -0,0 +1,11 @@ +package com.vega.hrm.core.repositories; + +import com.vega.hrm.core.entities.BoRole; +import java.util.UUID; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CoreRoleRepository extends JpaRepository { + BoRole findBoRoleById(UUID id); +} diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/core/repositories/CoreUserRepository.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/repositories/CoreUserRepository.java new file mode 100644 index 0000000..43b7e72 --- /dev/null +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/repositories/CoreUserRepository.java @@ -0,0 +1,12 @@ +package com.vega.hrm.core.repositories; + + +import com.vega.hrm.core.entities.BoUser; +import java.util.UUID; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CoreUserRepository extends JpaRepository { + BoUser findByUserName(String username); +} diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/core/service/RedisService.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/service/RedisService.java new file mode 100644 index 0000000..5ee6fcb --- /dev/null +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/service/RedisService.java @@ -0,0 +1,68 @@ +package com.vega.hrm.core.service; + +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import lombok.RequiredArgsConstructor; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class RedisService { + private final RedisTemplate redisTemplate; + + // String operations + public void set(String key, Object value, long timeout, TimeUnit unit) { + redisTemplate.opsForValue().set(key, value, timeout, unit); + } + + @SuppressWarnings("unchecked") + public T get(String key) { + Object value = redisTemplate.opsForValue().get(key); + return value != null ? (T) value : null; + } + + public boolean exists(String key) { + return Boolean.TRUE.equals(redisTemplate.hasKey(key)); + } + + public void delete(String key) { + redisTemplate.delete(key); + } + + // Hash operations + public void hSet(String key, String field, Object value) { + redisTemplate.opsForHash().put(key, field, value); + } + + @SuppressWarnings("unchecked") + public T hGet(String key, String field) { + Object value = redisTemplate.opsForHash().get(key, field); + return value != null ? (T) value : null; + } + + public void hSetAll(String key, Map values) { + redisTemplate.opsForHash().putAll(key, values); + } + + public Map hGetAll(String key) { + return redisTemplate.opsForHash().entries(key); + } + + public boolean hExists(String key, String field) { + return redisTemplate.opsForHash().hasKey(key, field); + } + + public void hDelete(String key, Object... fields) { + redisTemplate.opsForHash().delete(key, fields); + } + + public Set hKeys(String key) { + return redisTemplate.opsForHash().keys(key); + } + + public long hSize(String key) { + return redisTemplate.opsForHash().size(key); + } +} \ No newline at end of file diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/wrapper/CustomRequestWrapper.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/wrapper/CustomRequestWrapper.java similarity index 97% rename from vega-hrm-core/src/main/java/com/vega/hrm/wrapper/CustomRequestWrapper.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/wrapper/CustomRequestWrapper.java index a5b6928..2d2e8d5 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/wrapper/CustomRequestWrapper.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/wrapper/CustomRequestWrapper.java @@ -1,4 +1,4 @@ -package com.vega.hrm.wrapper; +package com.vega.hrm.core.wrapper; import jakarta.servlet.ReadListener; import jakarta.servlet.ServletInputStream; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/wrapper/CustomResponseWrapper.java b/vega-hrm-core/src/main/java/com/vega/hrm/core/wrapper/CustomResponseWrapper.java similarity index 97% rename from vega-hrm-core/src/main/java/com/vega/hrm/wrapper/CustomResponseWrapper.java rename to vega-hrm-core/src/main/java/com/vega/hrm/core/wrapper/CustomResponseWrapper.java index bab8c42..1e8baa9 100644 --- a/vega-hrm-core/src/main/java/com/vega/hrm/wrapper/CustomResponseWrapper.java +++ b/vega-hrm-core/src/main/java/com/vega/hrm/core/wrapper/CustomResponseWrapper.java @@ -1,4 +1,4 @@ -package com.vega.hrm.wrapper; +package com.vega.hrm.core.wrapper; import jakarta.servlet.ServletOutputStream; import jakarta.servlet.WriteListener; diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/filters/AuthorizationFilter.java b/vega-hrm-core/src/main/java/com/vega/hrm/filters/AuthorizationFilter.java deleted file mode 100644 index 2500c45..0000000 --- a/vega-hrm-core/src/main/java/com/vega/hrm/filters/AuthorizationFilter.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.vega.hrm.filters; - - -import io.jsonwebtoken.ExpiredJwtException; -import jakarta.servlet.FilterChain; -import jakarta.servlet.ServletException; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.List; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; -import org.apache.logging.log4j.ThreadContext; -import org.springframework.core.annotation.Order; -import org.springframework.stereotype.Component; -import org.springframework.web.filter.OncePerRequestFilter; - - - -@Component -@Order(2) -@RequiredArgsConstructor -public class AuthorizationFilter extends OncePerRequestFilter { - - @Override - protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, - FilterChain filterChain) throws ServletException, IOException { - filterChain.doFilter(request, response); - } -} diff --git a/vega-hrm-core/src/main/java/com/vega/hrm/models/RedisEccKeyPair.java b/vega-hrm-core/src/main/java/com/vega/hrm/models/RedisEccKeyPair.java deleted file mode 100644 index df89cb9..0000000 --- a/vega-hrm-core/src/main/java/com/vega/hrm/models/RedisEccKeyPair.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.vega.hrm.models; - -import java.time.Instant; -import lombok.Getter; -import lombok.NoArgsConstructor; -import vn.vnpay.dvnh.backoffice.core.common.utility.DateTimeUtils; -@Getter -@NoArgsConstructor -public class RedisEccKeyPair { - private String privateKey; - private String expiration; - - public RedisEccKeyPair(String privateKey, Instant expiration) { - this.privateKey = privateKey; - this.expiration = DateTimeUtils.format(expiration); - } - -}