Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- critical rendering path
- css framework
- OverTheWire
- Git
- nodejs
- --watch
- draganddrop
- screencapture
- MySQL
- Camera Movement
- unity
- Spring Boot
- linux
- Google Developer API
- Google Refund
- spread 연산자
- docker
- Unity Editor
- express
- react
- mongoDB
- server
- java
- Camera Zoom
- SDK upgrade
- rpg server
- Packet Network
- springboot
- Digital Ocean
- Unity IAP
Archives
- Today
- Total
우당탕탕 개발일지
[SpringBoot] 민감한 데이터 .env 에 보관/사용하기 본문
메일보내기를 하면서 내 이메일과 앱 비밀번호를 application.properties에 넣어야하는데, application.properties는 깃에 올라가기 때문에 보안상의 이슈가 있다. 그래서 .env파일로 옮기는 방법을 찾아보았다.
루트디렉토리( build.gradle 이 있는 위치)에 .env 파일을 생성하여 다음과같이 원하는 변수를 설정한다.
##.env
mail.username=myusername
mail.password=mypassword
.gitignore 파일에 .env를 추가한다.git에 커밋되지 않도록 도와준다.
build.gradle에 dependencies를 추가한다. .env안의 값을 가져올 수 있게 하는 라이브러리이다.
implementation 'io.github.cdimascio:dotenv-java:3.0.0'
Spring Boot 의 main Application 클래스에서 (main함수. run클릭시 시작되는곳) , SpringApplicaion.run() 이 호출되기 전에 .env를 로드한다.
import io.github.cdimascio.dotenv.Dotenv;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
// Load .env variables
Dotenv dotenv = Dotenv.configure().load();
// Automatically set all .env variables to System properties
dotenv.entries().forEach(entry -> System.setProperty(entry.getKey(), entry.getValue()));
SpringApplication.run(Application.class, args);
}
}
application.properties 내용을 수정한다.
spring.mail.host=smtp.naver.com
spring.mail.port=587
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
#spring.mail.username=myusername
#spring.mail.password=mypassword
spring.datasource.username=${mail.username}
spring.datasource.password=${mail.password}
번외
사실 이것도 완전 100% 안전한 것은 아니라고 한다. 권장하는 방법은 환경변수로 설정하는 방법이다.
우선 나는 로컬환경에서 구현중이기도하고, 나중에 배포할때는 환경변수로 설정해보는것도 좋을것같다. 환경변수로 민감데이터를 관리하는것은 매우 간단하다
먼저, 아래처럼 환경변수를 설정해준다. 배포시에 리눅스 아니면 우분투이겠지?
export MAIL_USERNAME=myemail@gmail.com
export MAIL_PASSWORD=mysecurepassword
application.properties 에 다음과같이 환경변수를 불러오면 그만이다.
spring.mail.username=${MAIL_USERNAME}
spring.mail.password=${MAIL_PASSWORD}
'Server' 카테고리의 다른 글
[nestJs] 파일 다운로드 시 서버가 재실행되는 문제 (feat. --watch) (0) | 2025.01.17 |
---|---|
[SpringBoot] 네이버 메일 전송하기 (0) | 2025.01.12 |
[SpringBoot] 7. 스프링부트 배포하기(완) (0) | 2025.01.10 |
[SpringBoot] 6. 데이터베이스의 객체와 연관관계 (Repository 계층) (1) | 2025.01.08 |
[SpringBoot] 5. 트랜잭션 (Service 계층) (0) | 2025.01.06 |