스터디를 통해 스프링부트와 AWS로 혼자 구현하는 웹 서비스(저자 이동욱) 서적을 공부하는 중입니다.
공부/실습한 내용을 정리한 포스팅입니다.
책에 모르는 부분이 있으면 구글링하거나 챗gpt에 물어봐서 보충하였습니다.
(아직 초보라 모르는 부분이 많아 이것저것 다 적었습니다.)
참고한 사이트 출처는 포스팅 맨 하단에 적었습니다.
※ 인증이란 사용자의 신원을 검증하는 프로세스를 뜻함. 가장 간단한 예시로는 ID, PW 통해 로그인하는 행위를 인증이라 할 수 있음. 인가는 인증 이후의 프로세스로 인증된 사용자가 어떠한 자원에 접근할 수 있는지를 확인하는 절차.
로그인 요청
: Resource Owner가 '구글로 로그인하기' 등의 버튼을 클릭하여 로그인 요청. Client는 OAuth 프로세스를 시작하기 위해 사용자의 브라우저를 Authorization Server로 보냄.
이때, Client는 Authorizaion Server가 제공하는 Authorization URL에 response_type, client_id, redirect_uri, scope 등의 매개변수를 쿼리 스트링으로 포함하여 보냄.
resopose_type은 반드시 code로 설정해야 함.
(인증이 성공할 경우 Authorization Code를 얻을 수 있음.)
로그인페이지 제공, ID/PW 제공
: Client가 빌드한 Authorization URL로 이동된 Resource Owner는 제공된 로그인 페이지에서 ID와 PW 등을 입력하여 인증.
Authorizaiton Code 발급, Redirect URI 리디렉트.
: 인증 성공 시, Authorization Server는 제공된 Redirect URI로 Redirect 시킴.
이때, Redirect URI에 Authorization Code 포함하여 Redirect 시킴. 구글의 경우, Code를 쿼리스트링에 포함.
※ Authorization Code - Client가 Access Token을 획득하기 위해 사용하는 임시코드. 이 코드는 수명이 매우 짧음.(일반적으로 1~10분)
※ Authorization Code와 Access Token 교환은 token 엔드 포인트에서 이뤄짐.
token 엔드포인트에서 Access Token 발급받기 위한 HTTP요청은 application/x-www-form-urlencoded
의 형식에 맞춰 전달.
필수 전달 매개변수는 grant_type(authorization_code여야 함), code(발급 받은 Authorization Code, redirect_uri, client_id, client_secret이다.
로그인 성공
: Client가 Resource Owner에게 로그인 성공 사실을 알림.
Access Token으로 리소스 접근
: 이후 Resource Owner가 Resource Server의 리소스가 필요한 기능을 Client에게 요청. 그럼 Client는 위 과정에서 발급하고 저장해둔 Resource Owner의 Access Token을 사용하여(API 호출) 제한된 리소스를 접근하여 Resource Owner에게 자사 서비스 제공.
spring-security-oauth2-autoconfigure
라이브러리 덕분.spring-security-oauth2-autoconfigure
라이브러리를 사용할 경우 SpringBoot2에서도 1.5에서 쓰던 설정을 그대로 사용할 수 있음.Spring Security Oauth2 Client
라이브러리 사용Spring Security Oauth2 Client
사용해야 하는 이유Support Lifetime for Spring Security OAuth 2.x
At the start of 2018, we announced the Spring Security OAuth project is officially in maintenance mode. We’ve already discontinued support for 2.0.x, in line with Boot’s 1.x End-of-Life (EOL), as well as 2.1.x and 2.2.x. And our plan is to discontinue the remaining support in the near future.
The currently supported branches are 2.3.x and 2.4.x. The 2.3.x line will reach EOL in March 2020. We will support the 2.4.x line at least one year after reaching feature parity.
To that end, with the release of Spring Security 5.2, we are strongly encouraging users to start migrating their legacy OAuth 2.0 client and resource server applications to the new support in Spring Security 5.2.
Spring Security OAuth 2.0 Roadmap Update
spring-security-oauth2-autoconfigure
라이브러리 썼는지 확인application.properties
혹은 application.yml
정보 차이(참고 사이트) [Spring Boot] application.properties와 application.yml의 차이점
※ CommonOAuth2Provider
enum
: 구글, 깃허브, 페이스북, 옥타 등 Oauth2 제공자 기본 설정값은 Spring Security에서 이 enum을 통해서 제공.
이외의 다른 소셜 로그인을 추가한다면 직접 다 추가해야 함.
application.properties 파일에서 Client 자격 증명 읽고
나머지 Client 속성에 대해 이미 Spring Security에 정의한CommonOauth2Provider 열거형을 사용.
(참고문서) CommonOAuth2Provider (spring-security-docs 6.1.1 API)
인증/인가는 어디에 어떻게 구현해야 할까? - 지마켓 기술블로그
OAuth 2.0 개념과 동작원리
Access Token과 Refresh Token이란 무엇인가?
[Spring Boot] OAuth2 소셜 로그인 가이드 (구글, 페이스북, 네이버, 카카오)
[Java] Spring Boot OAuth 2 Client 이해하기 -1 : 정의, 흐름, 인증방식 종류
[Spring Security] OAuth 네이버 로그인하기
스프링 시큐리티 5 – OAuth2 로그인