Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
관련 이슈
closed #14
작업한 내용
실습 1: Session 방식 구현
1. SecurityConfig 설정
Session 방식을 사용하기 위해
SecurityConfig에서 다음과 같이 설정:주요 설정 내용:
formLogin(): Spring Security의 기본 폼 로그인 활성화loginProcessingUrl(): 로그인 처리 URL을/api/v1/members/login으로 설정logout(): 로그아웃 URL 및 성공 후 리다이렉트 URL 설정2. 로그인 서비스 구현
Session 방식에서는
AuthenticationManager를 사용하여 인증을 처리하고, Spring Security가 자동으로 세션 생성:동작 과정:
AuthenticationManager.authenticate()호출CustomUserDetailsService에서 사용자 조회PasswordEncoder로 비밀번호 검증Authentication객체 생성SecurityContextHolder에 저장 → 자동으로 HttpSession에도 저장됨JSESSIONID쿠키 발급3. 로그아웃 구현
서버 측 세션 무효화:
동작 과정:
SecurityContext에서Authentication객체 가져오기SecurityContextLogoutHandler.logout()호출JSESSIONID쿠키 삭제4. 회원가입 구현
Session 방식과 JWT 방식 모두 동일하게 구현:
비밀번호 처리:
BCryptPasswordEncoder를 사용하여 비밀번호를 해시$2a$10$...)5. DB 저장 확인
회원가입 후
users테이블을 확인하면:password컬럼에 BCrypt 해시 값이 저장됨created_at,updated_at은 JPA Auditing으로 자동 설정됨실습 2: JWT 방식 구현
1. SecurityConfig 설정
JWT 방식을 사용하기 위해
SecurityConfig에서 다음과 같이 설정:주요 설정 내용:
formLogin().disable(): Session 기반 로그인 비활성화addFilterBefore():JwtAuthFilter를 필터 체인에 추가exceptionHandling(): 인증 실패 시 커스텀 에러 응답 반환2. JwtUtil 구현
JWT 토큰 생성 및 검증을 위한 유틸리티 클래스 구현:
설정 값:
jwt.secret:90ee33b340aed322434e3b5b1b142b19(HMAC SHA-256 키)jwt.expiration:14400000(4시간, 밀리초)3. JwtAuthFilter 구현
모든 요청에 대해 JWT 토큰을 검증하는 필터 구현:
동작 과정:
Authorization헤더에서Bearer {token}형식의 토큰 추출BadCredentialsException발생 → 401 반환SecurityContext에 인증 정보 저장4. AuthenticationEntryPointImpl 구현
인증 실패 시 커스텀 에러 응답을 반환하는 클래스 구현:
동작:
JwtAuthFilter에서 발생한AuthenticationException을 받아서 처리5. 로그인 서비스 구현
JWT 방식에서는 로그인 성공 시 토큰을 발급:
동작 과정:
passwordEncoder.matches())CustomUserDetails생성6. 로그인 응답 DTO
JWT 방식에서는
accessToken을 포함:7. 회원가입 구현
JWT 방식과 Session 방식 모두 동일하게 구현 (비밀번호 해싱 방식 동일).
🔄 두 방식의 차이점
Session 방식
JSESSIONID쿠키로 세션 식별JWT 방식
📝 구현된 API 목록
공통 (Session/JWT 모두)
POST /api/v1/members/signup: 회원가입POST /api/v1/members/login: 로그인Session 방식 전용
POST /api/v1/members/logout: 로그아웃 (세션 무효화)JWT 방식
🎯 결론
두 가지 인증 방식을 모두 구현하여 각각의 특징과 장단점을 이해. Session 방식은 전통적이고 안정적이며, JWT 방식은 확장성이 좋고 Stateless한 구조를 제공. 프로젝트의 요구사항에 따라 적절한 방식을 선택할 수 있음.
PR Point 및 참고사항, 스크린샷