+7
-1
@@ -29,13 +29,19 @@ dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springframework.security:spring-security-crypto:6.5.3'
|
||||
implementation 'org.mapstruct:mapstruct:1.5.5.Final'
|
||||
implementation 'commons-validator:commons-validator:1.7'
|
||||
|
||||
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'
|
||||
annotationProcessor 'org.projectlombok:lombok'
|
||||
|
||||
compileOnly 'org.projectlombok:lombok'
|
||||
|
||||
developmentOnly 'org.springframework.boot:spring-boot-devtools'
|
||||
|
||||
runtimeOnly 'com.microsoft.sqlserver:mssql-jdbc'
|
||||
runtimeOnly 'com.mysql:mysql-connector-j'
|
||||
runtimeOnly 'org.postgresql:postgresql'
|
||||
annotationProcessor 'org.projectlombok:lombok'
|
||||
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
testImplementation 'org.mockito:mockito-core:5.3.1'
|
||||
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package ru.soune.nocopy.handler.validator;
|
||||
|
||||
import org.apache.commons.validator.routines.DomainValidator;
|
||||
import org.apache.commons.validator.routines.EmailValidator;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.Validator;
|
||||
import ru.soune.nocopy.dto.RegRequest;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.*;
|
||||
|
||||
@Component
|
||||
@@ -89,65 +93,45 @@ public class RegRequestValidator implements Validator {
|
||||
|
||||
private void validateEmail(String email, Errors errors) {
|
||||
if (email == null || email.trim().isEmpty()) {
|
||||
errors.rejectValue("email", "email.is.empty",
|
||||
"Email must not be empty");
|
||||
errors.rejectValue("email", "email.is.empty", "Email must not be empty");
|
||||
return;
|
||||
}
|
||||
|
||||
String trimmedEmail = email.trim().toLowerCase();
|
||||
|
||||
if (trimmedEmail.length() > 128) {
|
||||
errors.rejectValue("email", "email.too.long",
|
||||
"Email must be less than 128 characters");
|
||||
if (trimmedEmail.length() > 254) {
|
||||
errors.rejectValue("email", "email.too.long", "Email is too long");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!trimmedEmail.matches("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$")) {
|
||||
errors.rejectValue("email", "email.invalid.format",
|
||||
"Invalid email format");
|
||||
EmailValidator emailValidator = EmailValidator.getInstance(true, true);
|
||||
if (!emailValidator.isValid(trimmedEmail)) {
|
||||
errors.rejectValue("email", "email.invalid", "Invalid email address");
|
||||
return;
|
||||
}
|
||||
|
||||
String[] parts = trimmedEmail.split("@");
|
||||
if (parts.length != 2) {
|
||||
return;
|
||||
}
|
||||
if (parts.length == 2) {
|
||||
String domain = parts[1];
|
||||
DomainValidator domainValidator = DomainValidator.getInstance();
|
||||
|
||||
String localPart = parts[0];
|
||||
String domain = parts[1];
|
||||
|
||||
if (localPart.length() > 64) {
|
||||
errors.rejectValue("email", "email.local.too.long",
|
||||
"Email username too long");
|
||||
}
|
||||
|
||||
if (!localPart.matches("^[a-z0-9+_.-]+$")) {
|
||||
errors.rejectValue("email", "email.local.invalid.chars",
|
||||
"Email contains invalid characters");
|
||||
}
|
||||
|
||||
validateEmailDomain(domain, errors);
|
||||
}
|
||||
|
||||
private void validateEmailDomain(String domain, Errors errors) {
|
||||
if (domain.length() > 255) {
|
||||
errors.rejectValue("email", "email.domain.too.long",
|
||||
"Email domain too long");
|
||||
}
|
||||
|
||||
String[] domainParts = domain.split("\\.");
|
||||
if (domainParts.length < 2) {
|
||||
errors.rejectValue("email", "email.domain.invalid",
|
||||
"Invalid domain name");
|
||||
}
|
||||
|
||||
String tld = domainParts[domainParts.length - 1];
|
||||
if (tld.length() < 2) {
|
||||
errors.rejectValue("email", "email.tld.invalid",
|
||||
"Invalid domain extension");
|
||||
if (!checkDomainExists(domain)) {
|
||||
errors.rejectValue("email", "email.domain.not.found",
|
||||
"Domain does not exist");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkDomainExists(String domain) {
|
||||
try {
|
||||
InetAddress.getByName(domain);
|
||||
return true;
|
||||
} catch (UnknownHostException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void validatePhone(String phone, Errors errors) {
|
||||
if (phone == null || phone.trim().isEmpty()) {
|
||||
errors.rejectValue("phone", "phone.empty",
|
||||
|
||||
Reference in New Issue
Block a user