OAuth 2.0 Client + OAuth 2.0 Resource Server 연동
Client
의존성
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client' //Client
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}클라이언트는 화면을 보여줘야 하기 때문에 템플릿 엔진(타임리프)이 필요하다.
application.yml
클라이언트에서 인가 서버로 임시 토큰을 요청하고 발급받기 위해 필요한 엔드포인트 및 설정 정보
IndexController
index.html
인증을 받지 못하면 화면에 Login 이 나오고, 링크는 인가 서버에게 임시 코드를 받도록 요청하는 링크이다.
인증을 받은 상태에서는
token()함수를 호출하는 버튼이 보인다.
이 함수는 응답으로 받은 액세스 토큰을 로컬 스토리지에 저장하고
/home으로 이동한다.이 액세스 토큰은
Authorization헤더에 보낼 토큰이 된다.
home.html
로그아웃 버튼과 각 함수를 실행하는
Photos버튼과Remote Photos버튼이 있다.
photos()
내부적으로
RestTemplate을 사용해 리소스 서버와 통신하여 데이터를 받아오는 함수
remotePhotos()
바로 리소스 서버에 요청을 보내 데이터를 받아오는 함수
SecurityConfig
기본 인가 설정과 통신에 필요한
RestTemplate빈 등록
RestApiController
http://localhost:8081

Login 클릭

인증이 되었기 때문에 Login 링크 대신
access token버튼이 보임.
access token 클릭

http://localhost:8081/home으로 이동하고 로컬 스토리지에 액세스 토큰이 저장되어 있다.
PreviousOAuth 2.0 Resource Server 권한 구현 - Scope 기반 권한 매핑NextOAuth 2.0 Client + OAuth 2.0 Resource Server 연동
Last updated