Spring Boot 2.x 버전에서 OAuth2가 안될때

Reading time ~2 minutes

Spring Boot 2.x 그리고 OAuth2

혹시 OAuth2 예제들을 따라하다 남들이 사용한 클래스와 애노테이션을 이용 할때 분명히 똑같이 따라 했는데 안되는 경우가 있을 겁니다. 그런 경우 자신의 스프링 부트 버전이 몇 버전인지 확인을 해 보시기 바랍니다.

만약 2.x 버전에 OAuth2 의존성을 아래와 같이 한다면 진행이 안되는 막막함을 느낄 겁니다.

dependencies {
	compile('org.springframework.security.oauth:spring-security-oauth2')
}

이유는 다른게 아니라 스프링 부트 2.x 버전대에서 변화가 생겨서 그런겁니다. 그것도 무려 2017년 10월 경에 말이죠. 그래서 아직 해당 자료는 많지 않은 경향이 있네요.

최근 우여곡적을 겪으며 해결을 했는데 허무할 정도로 쉬워서 혹시나 같은 문제를 겪는 분들이 있으면 아래를 참조 바랍니다. 일단 무시무시한 변화가 발생한 릴리즈 노트를 보면서 시작하죠!

Spring Boot 2.0.0 M5 Release Notes

OAuth 2.0 Support Functionality from the Spring Security OAuth project is being migrated to core Spring Security. OAuth 2.0 client support has already been added and additional features will be migrated in due course.

If you depend on Spring Security OAuth features that have not yet been migrated you will need to add org.springframework.security.oauth:spring-security-oauth2 and configure things manually. If you only need OAuth 2.0 client support you can use the auto-configuration provided by Spring Boot 2.0. We’re also continuing to support Spring Boot 1.5 so older applications can continue to use that until an upgrade path is provided. (출처)

결론만 말하자면 Spring Security OAuth 프로젝트의 기능들이 일부 Spring Security로 마이그레이션 되는 중이라 스프링 부트 2.x 버전에서는 기존에 OAuth 프로젝트로는 지원이 안되는 기능들이 생겼다는 것 입니다.

일단 UserInfoTokenServices, ResourceServerProperties 클래스와 @EnableOAuth2Client, @EnableOAuth2Sso 애노테이션이 대표적입니다. 주로 OAuth2를 실습 해보려고 찾아보면 구현되어 있는 예제들이 저 클래스들과 애노테이션을 사용하기 때문에 진행이 막혀 버립니다.

가장 간단한 방법 중 하나는 스프링 부트를 1.x 버전대로 바꾸고 새로 시작하면 되지만 일부 프로젝트가 진행 된 상태면 그러기 쉽지 않습니다.

해결방법은 생각보다 간단 합니다!! 아래를 보죠.


OAuth2 Autoconfig

If you have spring-security-oauth2 on your classpath you can take advantage of some auto-configuration to make it easy to set up Authorization or Resource Server. For full details, see the Spring Security OAuth 2 Developers Guide.

This project is a port of the Spring Security OAuth support that came with Spring Boot 1.x. Support was removed in favor of Spring Security 5’s first class OAuth support. To ease migration, this project exists as a bridge between the old Spring Security OAuth support and Spring Boot 2.x. (출처)

이것도 결론만 말하자면 2.x 버전과 1.x 버전의 호환성을 위한 것 입니다.

많은 예제들을 보면

dependencies {
	compile('org.springframework.security.oauth:spring-security-oauth2')
}

해당 디펜덴시를 추가하는데 oauth:spring-security-oauth2 말고

repositories {
	mavenCentral()
	//If you are using a SNAPSHOT version
  maven { url 'https://repo.spring.io/snapshot' }
	//If you are using a milestone or release candidate version
	//현재는 SNAPSHOT 버전이기 때문에 필요 없습니다.
	//maven { url 'https://repo.spring.io/milestone' }
}

dependencies {
	compile 'org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.0.1.BUILD-SNAPSHOT'
}

이렇게 추가하면 2.x 에서도 1.x에서 만들어 둔 것들이 아무 문제 없이 돌아 갑니다.

이유는 위에서 말씀 드린 것 처럼 1.x대와의 호환성을 위해 만들어진 것이기 때문입니다.

그러나 어디까지나 이건 이전 버전대의 방식을 이용하기 위한 방법이고 가장 좋은 방법은 2.x에서 사용하는 방법을 학습 하는 것이겠지만, 아직은 자료가 많지 않고 스프링, 스프링 시큐리티와 OAuth2에 대한 이해도가 높지 않아 정확히 파악하진 못한 관계로 나중에 좀 더 근본적인 해결 방법을 알게 되면 포스팅 하도록 하겠습니다.


추가로

spring-cloud-security를 이용해도 됩니다.

repositories {
	mavenCentral()
	//아래 버전의 경우
	maven { url "https://repo.spring.io/milestone" }
}
dependencies {
compile 'org.springframework.cloud:spring-cloud-security:2.0.0.M3'
}

+ 저 같은 경우는 그레이들로 작업을 해서 그레이들 dependencies만 작성을 하였는데 혹시 아직 메이븐이 익숙치 않아 어려움이 있는 분이 있어 알려주시면 추가하도록 하겠습니다.