Open
Conversation
- CustomRequestEntityConverter: Converter<OAuth2AuthorizationCodeGrantRequest, RequestEntity<?>>의 구현체로, 애플 소셜로그인의 경우 client_secret을 동적으로 생성하도록 분기 처리 한다. - DefaultAuthorizationCodeTokenResponseClient: WebSecurityConfig 에서 oauth2Login 부분의 tokenEndpoint 로 설정할 객체로, CustomRequestEntityConverter 를 requestEntityConverter 로 설정하고 Bean 등록을 한다.
- 애플 소셜로그인의 경우 Attribute 추출 로직을 다르게 핸들링함 (userinfo 를 받아오는 엔드포인트가 없고, 사용자 인증 성공 이후 응답 받은 id_token 을 파싱하여 사용자 정보를 얻어야 함) - 기존에 CustomOAuth2UserService 에서 직접 DefaultOAuth2UserService 객체를 생성하였으나, 외부에서 주입 받도록 WebSecurityConfig 수정
- API 명세용 S3 버킷 이름 수정
|
minnseong
approved these changes
Apr 7, 2024
Member
minnseong
left a comment
There was a problem hiding this comment.
바쁘실텐데 잘 리펙토링 하시느라 수고 많으셨습니다.
새로운 것들 많이 배워 갑니다!
| String registrationId = request.getClientRegistration().getRegistrationId(); | ||
| MultiValueMap<String, String> params = (MultiValueMap<String, String>) entity.getBody(); | ||
| if(registrationId.contains("apple")) { | ||
| params.set("client_secret", appleOAuth2UtilService.createAppleClientSecret( |
Member
There was a problem hiding this comment.
r3 : 매직넘버를 상수로 처리하면 좋을 것 같습니다.
DonggyuJin
approved these changes
Apr 7, 2024
DonggyuJin
left a comment
There was a problem hiding this comment.
바쁘신 와중에도 대단하십니다 .. 👍 저두 열심히 학습해서 새 마이브러리에 적용해보겠습니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.




🧑💻 작업 사항
Background
앱스토어 출시 전 애플 소셜로그인을 급하게 구현 했을 때, client secret을 동적으로 생성해야 하는 부분이 타 소셜로그인과 달라서 Spring Security 로 처리하기 어렵다고 판단했습니다.
그래서 애플 소셜로그인만 따로 API 를 만들어서 구현했는데,,
최근에 다음과 같은 방법으로 Spring Security 로 처리할 수 있음을 알게 되었습니다!!
application.yml 에서 client-secret에 미리 사인된 client-secret JWT를 주입한다. 다만 이 JWT는 6개월 뒤에 만료되므로, 6개월 뒤에 새로 만든 JWT로 바꾸어주어야 한다.
레퍼런스: Sign in with Apple Oauth2 Support #9047, Support JWT for Client Authentication #8175
requestEntityConverter 에 직접 만든 CustomRequestEntityConverter를 주입한다. 이 CustomRequestEntityConverter는 소셜 로그인이 애플인 경우에 client-secret 을 동적으로 생성하도록 분기 처리 해준다.
레퍼런스: [Spring Security Oauth2.0 Client] Apple 로그인
토큰 유효 기간이 그래도 6개월인데, 굳이 소셜로그인 할 때 마다 토큰을 만들어줘야 함? vs 향후 6개월 마다 설정 파일 들어가서 새로운 토큰으로 업데이트 해줄 자신 있음?
위의 두 생각들을 고려해 본 바,
저는 6개월 마다 키를 갱신해줄 자신이 없기 때문에,, 2번째 방법으로 개선해보았습니다.
CustomRequestEntityConverter
사용자 인증이 완료된 후, 인증 서버로부터 사용자 정보 접근을 위한 AuthorizationCode를 받아올 때 거치는 Converter 입니다.
registrationId 가 apple 이라면 appleOAuth2UtilService.createAppleClientSecret()으로 client secret을 생성해 client_secret 파라미터에 담아줍니다.
리턴 되는 RequestEntity는 인증 서버로 요청되고, 그 응답은 다음 코드에 전달됩니다.
CustomOAuth2UserService
애플 소셜로그인은 사용자 정보를 받아오는 리소스 서버가 없고, 인증 서버에서 곧바로 id_token 을 통해 사용자 정보를 제공해주기 때문에, 이 부분도 분기 처리 해주었습니다.
의존성 주입 설정 변경
리팩터링 과정에서 OAuth2UserService 객체가 ApplicationContext 에서 관리될 수 있도록 직접 생성하지 않고, 외부에서 주입되게끔 수정하였습니다.
기존
수정 후
Bean 순환 참조 이슈가 발생해 Service 어노테이션을 제거하고 WebSecurityConfig에서 Bean 등록을 해주었습니다.
리팩터링 결과
객체 지향적인 코드가 얼마나 변경에 유연하게 대처할 수 있는지 몸소 체감할 수 있었습니다. 😊
🔗 링크
🐰 시급한 정도
🐢 천천히 : 급하지 않습니다.
📖 참고 사항
Apple Developer 에서 /login/oauth2/code/apple URL 추가했습니다.
config 레포지토리에서는 redirect-uri 부분을 아직 수정 안했는데, prod 배포 전에 값 수정해두도록 하겠습니다!