스프링부트 OAuth2 - 2. Github 인증

https://spring.io/guides/tutorials/spring-boot-oauth2/ 를 참조하여 작성하였습니다.

스프링 공식 홈페이지의 OAuth2 가이드를 보면 다른 가이드와는 다르게 다소 스크롤의 압박이 느껴진다. 따라서 크게 세 부분으로 나누어 포스팅이 진행될 예정이다.

  1. Facebook으로 로그인
  2. Github로 로그인
  3. OAuth2 인증 서버 구축

Github로 로그인

이번 포스팅에서는 지난 포스팅에서 다루었던Facebook으로 로그인에 이어서 Github계정으로 OAuth2 인증을 하는 과정에 관한 글이다. 방식은 비슷해서 금방 구현이 가능하다.

우선 index.html 파일에 Github로 로그인 할 수 있는 링크를 추가하자.

1
2
3
4
5
6
7
8
9
<h1>로그인</h1>
<div class="container unauthenticated">
<div>
Facebook : <a href="/login/facebook">클릭</a>
</div>
<div>
Github : <a href="/login/github">클릭</a>
</div>
</div>

원래는 provider(facebook, github, google 등)마다 엔드포인트에 대한 처리가 달라져야겠지만 이번 시리즈의 포스팅에서는 따로 처리하지 않아도 인증 처리와 관련된 응답에서 name이라는 필드를 공통적으로 가지고 있기 때문에 특별히 변경할 사항은 없다.

Github 인증 필터 추가

Application.java에서 /login/github에 대한 필터 추가가 필요하다 ssoFilter()에 추가해보자.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
private Filter ssoFilter() {

CompositeFilter filter = new CompositeFilter();
List<Filter> filters = new ArrayList<>();

OAuth2ClientAuthenticationProcessingFilter facebookFilter = new OAuth2ClientAuthenticationProcessingFilter("/login/facebook");
OAuth2RestTemplate facebookTemplate = new OAuth2RestTemplate(facebook(), oauth2ClientContext);
facebookFilter.setRestTemplate(facebookTemplate);
UserInfoTokenServices tokenServices = new UserInfoTokenServices(facebookResource().getUserInfoUri(), facebook().getClientId());
tokenServices.setRestTemplate(facebookTemplate);
facebookFilter.setTokenServices(tokenServices);
filters.add(facebookFilter);

OAuth2ClientAuthenticationProcessingFilter githubFilter = new OAuth2ClientAuthenticationProcessingFilter("/login/github");
OAuth2RestTemplate githubTemplate = new OAuth2RestTemplate(github(), oauth2ClientContext);
githubFilter.setRestTemplate(githubTemplate);
tokenServices = new UserInfoTokenServices(githubResource(), getUserInfoUri(), github().getClientId());
githubFilter.setTokenServices(tokenServices);
filters.add(githubFilter);

filter.setFilters(filters);
return filter;
}

// ... 중략 ...

@Bean
@ConfigurationProperties("github.client")
public OAuth2ProtectedResourceDetails github() {
return new AuthorizationCodeResourceDetails();
}

@Bean
@ConfigurationProperties("github.resource")
public ResourceServerProperties githubResource() {
return new ResourceServerProperties();
}

이전에 등록한 Facebook 필터로 인해 CompositeFilter를 구성하여 List로 추가하는 방식을 사용하였다. github(), githubResource()를 추가하여 나머지 내용도 보충하였다.

또한 application.yml에 github 관련 설정을 추가한다. 선행되어야 하는 OAuth2 앱을 작성해야 하는데 github OAuth2 앱을 만드는 방법은 여기를 참조하자.

1
2
3
4
5
6
7
8
9
github:
client:
clientId: CLIENT-ID
clientSecret: CLIENT-SECRET
accessTokenUri: https://github.com/login/oauth/access_token
userAuthorizationUri: https://github.com/login/oauth/authorize
clientAuthenticationScheme: form
resource:
userInfoUri: https://api.github.com/user

결과 확인

결과를 확인해보자. 깃허브 로그인 링크를 클릭하면 다음과 같은 화면을 볼 수 있고, 승인을 하면 다시 index.html로 돌아와서 사용자정보가 확인되는 것을 볼 수 있다.

Github OAuth2 확인

소스코드

https://github.com/hwiVeloper/SpringBootStudy/tree/18f34d30f05db7f0fa998b6a6cae3b0019c05331/spring-boot-oauth2