GeezerVison is showing github.com — feat: OAuth로그인, JWT 토큰 생성 · Issue #8 · GreatAlgorithm-Study/ProjectStudy
feat: OAuth로그인, JWT 토큰 생성 #8Description어떤 기능인가요?
- OAuth와 Spring Security를 적용한 로그인 기능 생성
- JWT를 통한 인증 및 권한부여
작업 상세 내용
- OAuth 로그인 구현
- SpringSecurity 적용
- JWT 토큰 생성
- 백엔드 로그인 성공시, 리다이렉션 처리 리팩토링
- 프론트와 연동 - JWT토큰 전달 상세 구현
- JWT 토큰 유효성 검사
- 로그아웃 기능 구현
- 로그아웃 시 블랙리스트 토큰 관리
참고할만한 자료(선택)
Activityyeongleej commented on Dec 29, 2024 ContributorAuthor More actions yeongleej commented on Jan 9, 2025 ContributorAuthor More actions 2025.01.09
진행 내용
- 프론트에서 JWT 토큰 전달
- 프론트에서 전달한 JWT 토큰 유효성 검사(Authentication) 및 예외처리 핸들러 등록
✔️ Spring Security Authentication(인증) 예외 핸들러

AuthenticationEntryPoint → 인증되지 않은 사용자의 접근을 차단 (401)
- jwt가 없거나, 인증되지 않은 사용자(만료된 토큰, 블랙리스트 토큰, 토큰의 일부가 사라짐 등)가 보호된 리소스에 접근할 때 실행
@Component
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException, ServletException {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); // 401 상태 코드
response.setContentType("application/json");
response.getWriter().write("{\"error\": \"Unauthorized\", \"message\": \"인증이 필요합니다.\"}");
}
}
AccessDeniedHandler → 인증은 되었지만, 권한이 없는 경우 차단 (403)
- 인증된 사용자가 권한이 없는 리소스를 요청했을 때 실행
@Component
public class JwtAccessDeniedHandler implements AccessDeniedHandler {
@Override
public void handle(HttpServletRequest request, HttpServletResponse response,
AccessDeniedException accessDeniedException) throws IOException, ServletException {
response.setStatus(HttpServletResponse.SC_FORBIDDEN); // 403 상태 코드
response.setContentType("application/json");
response.getWriter().write("{\"error\": \"Forbidden\", \"message\": \"권한이 부족합니다.\"}");
...
http
.exceptionHandling(exception -> exception
.authenticationEntryPoint(jwtAuthenticationEntryPoint) // 인증 예외 처리
.accessDeniedHandler(jwtAccessDeniedHandler) // 권한 예외 처리
);
Exception 처리 맨날 모르는척 했다가 이번에 제대로 공부해야겠다고 다짐했습니다!! ㅎㅎㅎ히 😄
앞으로 해야할 일
- @AuthenticationPrincipal 적용
- 로그아웃 기능 및 토큰 관리 구현
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|