728x90
개요
개인 프로젝트를 진행하는 중 시큐리티 부분은 아주 중요하고 헷갈리기 때문에 정리해놓기 위해 포스팅 하기로 했다.
gradle 의존성을 추가 한 후 -> 프로젝트 파일 구조에 맞게 패키지와 관련 클래스를 작성한 후 -> 프로젝트에 필요한 환경변수를 세팅하면 기본적인 작업은 끝난다.
나의 환경
Window 11
intelliJ
java 21
spring Boot 3.3.0
spring Security 6
jwt 0.11.5
프로젝트 파일 구조
<- 다음과 같은 구조로 프로젝트를 진행하는데
이 중 시큐리티+jwt를 사용하기에 필요한 패키지는
"config, controller, domain, dto, repository, security, service" 이다.
이 패키지 중 만들 클래스들은 연두색으로 표시한 부분만 만들면 된다.
의존성 설치
// security
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
// lombok
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
// 타임리프에서 스프링시큐리티를 쓸수있게.
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6:3.1.2.RELEASE'
implementation 'org.jsoup:jsoup:1.14.3'
// jwt & json
implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.5'
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.5'
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.5'
// gson - json 메시지를 다루기 위한 라이브러리
implementation 'com.google.code.gson:gson'
// validation사용
implementation 'jakarta.validation:jakarta.validation-api:3.0.2'
필요한 환경변수 세팅
jwt secretKey와 refreshKey에 대해서 .yml 파일에 작성해준다.
시크릿 키를 얻는 방법은 아래에 적어놨다.
PowerShell의 Get-Random cmdlet을 사용하여 64바이트의 랜덤 데이터를 생성하고 이를 16진수로 변환하여 시크릿 키를 얻는다.
# 64바이트의 랜덤 데이터 생성
$bytes = @(0..63 | ForEach-Object { Get-Random -Maximum 256 })
# 16진수로 변환
$hex = $bytes.ForEach({ "{0:x2}" -f $_ }) -join ""
# 결과 출력
$hex
참고로 맥에서는 다음과 같이 하면 시크릿 키를 얻을 수 있다.
openssl rand -hex 64
2편에서.. Security 설정에 대해 알아볼 것이다.
728x90
'Spring > Security' 카테고리의 다른 글
Spring Security + JWT (RefreshToken, AccessToken)를 사용한 로그인, 로그아웃 구현 - 4편 (0) | 2024.07.23 |
---|---|
Spring Security + JWT (RefreshToken, AccessToken)를 사용한 로그인, 로그아웃 구현 - 3편 (1) | 2024.07.22 |
Spring Security + JWT (RefreshToken, AccessToken)를 사용한 로그인, 로그아웃 구현 - 2편 (1) | 2024.07.22 |
Spring Security : Spring Security 개념 및 필터들 (0) | 2024.07.10 |
[Spring Security] - 사용자 인증 처리 UserDetailsService 인터페이스 (0) | 2024.06.27 |