Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
인증된 Authentication 정보 확인 - SecurityContext를 이용하는 방법
- Loading branch information
Showing
1 changed file
with
5 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
src/main/java/com/codestates/hello_oauth2/home/HelloHomeController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) 리턴 | ||
} | ||
} |