본문 바로가기
TIL/Spring

[Spring Boot] OAuth2의 Authorization Code Grant Type 처리 중 발생했던 에러 내용 정리

by DandyU 2018. 12. 19.

Spring Boot 2.0에서 인터넷으로 구할 수 있는 OAuth2 예제(2.0 기반이 아닌 프로젝트)를

따라하다보면 Authorization Code Grant Type에서 아래와 같은 에러가 발생할 수 있음

 

"User must be authenticated with Spring Security before authorization can be completed."

 

Spring Boot 2.0이 아닌 이하 1.5.x에서는 위 에러 메시지 대신에 로그인 입력 창이 보이는데 2.0에서는 보이지 않음

 

버전에 따른 차이라면 Dependency인데 

1.5.x 이하에서는 아래와 같고,

- groupId: org.springframework.security.oauth

- artifactId: spring-security-oauth2

2.0 이상부터는 

- groupId: org.springframework.security.oauth.boot

- artifactId: spring-security-oauth2-autoconfigure

 

1.5.x이하에서는 Spring Security OAuth(2.2+)를 사용했는데, 2.0에서는 Spring Security 5로 바뀌었음

Spring Security 5는 글 작성일 기준으로 Resource Server, Authorization Server는 현재 개발(?)중임

 

[참고]

 

Spring Security 5가 아직 지원되지 않는 항목이 있어 Spring Security OAuth2를 Spring Boot 2.0에 사용해야 되는데

기존 Dependency는 사용할 수 없고 새로 추가된 Dependency를 사용해야함

 

Dependency를 변경하면 발생하는 문제가 있는데

Authorization Code Grant Type이 갑자기 아래와 같은 에러 메시지와 함께 로그인 입력 창이 보이지 않음

 

"User must be authenticated with Spring Security before authorization can be completed."

 

로그인을 안했으니 당연한 상황인데.. 자동으로 로그인 입력 창이 보여야 하는데 안됨, 수동으로 로그인 페이지를 실행하게 함

 

@EnableWebSecurity

class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

 

    @Override

    protected void configure(HttpSecurity http) throws Exception {

        http.formLogin();

    }

 

}

 

로그인을 하면, 또 아래와 같은 에러 메시지를 확인할 수 있음

 

error="invalid_request", error_description="At least one redirect_uri must be registered with the client."

 

내가 사용한 Redirect URI가 등록되지 않았다는 메시지.. Redirect URI를 application.yml에 아래와 같이 등록함(참고로 테스트한 URI는 http://localhost:8080/oauth/authorize?response_type=code&scope=any&client_id=foo&redirect_uri=http://localhost:8080/test/authorization-code)

 

security:

  oauth2:

    client:

      client-id: foo

      client-secret: bar

      registered-redirect-uri:

        - http://localhost:8080/test/authorization-code

 

위 두 가지 문제점을 해결하면 기존 Spring Boot 2.0 이하의 버전에서 처럼 Authorization Code Grant Type이 정상 처리됨

 
 

 

'TIL > Spring' 카테고리의 다른 글

JPA::@Transactional(readOnly = true)  (0) 2023.04.12
Spring::JPA 사용 시 일부 컬럼값만 가져오기  (0) 2023.02.14
Spring::Spring Boot 3.0  (0) 2023.01.29

댓글