Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/com/team/buddyya/common/config/ConverterConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.team.buddyya.common.config;

import com.team.buddyya.common.service.EncryptedConverter;
import com.team.buddyya.common.service.EncryptionService;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ConverterConfig {

@Autowired
private EncryptionService encryptionService;

@PostConstruct
public void init() {
EncryptedConverter.setEncryptionService(encryptionService);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

public enum CommonExceptionType implements BaseExceptionType {

FILE_UPLOAD_ERROR(9000, HttpStatus.INTERNAL_SERVER_ERROR, "Error occurred during file upload.");
FILE_UPLOAD_ERROR(9000, HttpStatus.INTERNAL_SERVER_ERROR, "Error occurred during file upload."),
ENCRYPTION_ERROR(9001, HttpStatus.INTERNAL_SERVER_ERROR, "Encryption/Decryption error");

private final int errorCode;
private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.team.buddyya.common.service;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

@Converter
public class EncryptedConverter implements AttributeConverter<String, String> {

private static EncryptionService encryptionService;

public static void setEncryptionService(EncryptionService service) {
EncryptedConverter.encryptionService = service;
}

@Override
public String convertToDatabaseColumn(String attribute) {
if (attribute == null) {
return null;
}
return encryptionService.encrypt(attribute);
}

@Override
public String convertToEntityAttribute(String dbData) {
if (dbData == null) {
return null;
}
return encryptionService.decrypt(dbData);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.team.buddyya.common.service;

import com.team.buddyya.common.exception.CommonException;
import com.team.buddyya.common.exception.CommonExceptionType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

@Service
public class EncryptionService {

@Value("${encryption.aes-key}")
private String secretKey;

private String ALGORITHM = "AES";

public String encrypt(String plainText) {
try {
SecretKeySpec key = new SecretKeySpec(secretKey.getBytes("UTF-8"), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encrypted = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encrypted);
} catch (Exception e) {
throw new CommonException(CommonExceptionType.ENCRYPTION_ERROR);
}
}

public String decrypt(String encryptedText) {
try {
SecretKeySpec key = new SecretKeySpec(secretKey.getBytes("UTF-8"), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
return new String(decrypted);
} catch (Exception e) {
throw new CommonException(CommonExceptionType.ENCRYPTION_ERROR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public ResponseEntity<UserResponse> getMyProfile(@AuthenticationPrincipal Custom
return ResponseEntity.ok(studentService.getUserInfo(userDetails.getStudentInfo(), userId));
}

@GetMapping("/phoneNumber")
public ResponseEntity<String> getPhoneNumber(@AuthenticationPrincipal CustomUserDetails userDetails) {

return ResponseEntity.ok(studentService.getPhoneNumber(userDetails.getStudentInfo()));
}

@PatchMapping
public ResponseEntity<UserResponse> updateUser(
@AuthenticationPrincipal CustomUserDetails userDetails,
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/team/buddyya/student/domain/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.team.buddyya.certification.domain.StudentIdCard;
import com.team.buddyya.chatting.domain.ChatroomStudent;
import com.team.buddyya.common.domain.BaseTime;
import com.team.buddyya.common.service.EncryptedConverter;
import com.team.buddyya.notification.domain.ExpoToken;
import jakarta.persistence.*;
import lombok.Builder;
Expand All @@ -30,7 +31,8 @@ public class Student extends BaseTime {
@GeneratedValue(strategy = IDENTITY)
private Long id;

@Column(length = 11, nullable = false, unique = true)
@Convert(converter = EncryptedConverter.class)
@Column(length = 128, nullable = false, unique = true)
private String phoneNumber;

@Column(length = 64, nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import com.team.buddyya.student.dto.response.UserResponse;
import com.team.buddyya.student.exception.StudentException;
import com.team.buddyya.student.exception.StudentExceptionType;
import com.team.buddyya.student.repository.*;
import com.team.buddyya.student.repository.BlockRepository;
import com.team.buddyya.student.repository.StudentRepository;
import com.team.buddyya.student.repository.UniversityRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -170,4 +172,9 @@ public void logout(StudentInfo studentInfo) {
}
student.getAvatar().setLoggedOut(true);
}

public String getPhoneNumber(StudentInfo studentInfo) {
Student student = findStudentService.findByStudentId(studentInfo.id());
return student.getPhoneNumber();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE student
MODIFY COLUMN phone_number VARCHAR(128) NOT NULL;