Skip to content

Commit

Permalink
인증된 Authentication 정보 확인 - SecurityContext를 이용하는 방법
Browse files Browse the repository at this point in the history
  • Loading branch information
wish9 committed Mar 23, 2023
1 parent 4efc61f commit 3bd91fc
Showing 1 changed file with 5 additions and 0 deletions.
@@ -1,12 +1,17 @@
package com.codestates.hello_oauth2.home;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HelloHomeController {
@GetMapping("/hello-oauth2")
public String home() {
var oAuth2User = (OAuth2User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); // SecurityContext(현재 스레드 인증정보)에서 인증된 Authentication 객체를 통해 Principal 객체 얻기
System.out.println(oAuth2User.getAttributes() // OAuth2User 객체에 저장되어 있는 사용자의 정보 중에서
.get("email")); // 사용자의 이메일 정보 얻기
return "hello-oauth2"; // oauth2(구글) 인증 성공시에만 볼 수 있는 화면(hello-oauth2.html) 리턴
}
}

0 comments on commit 3bd91fc

Please sign in to comment.