<!-- Oauth 소셜로그인 -->
<div class="login__facebook">
<button onclick="javascript:location.href='/oauth2/authorization/facebook'">
<i class="fab fa-facebook-square"></i>
<span>Facebook으로 로그인</span>
</button>
</div>
<!-- Oauth 소셜로그인end -->
설정이 끝났다면 facebook 로그인 버튼에 onclick 이벤트를 지정해준다.
여기서
href='/oauth2/authorization/facebook' 은 고정 href 값이다.
페이스북 외 다른 소셜로그인(네이버, 카카오 등) 도 동일하다.
왜냐하면
스프링 개발팀에서 그렇게 정해놨기 때문이다
아무튼 다시본론으로 들어가면
이렇게 해주면
이렇게 페이스북 로그인 껍데기가 구현된다 ( 아직 서비스 로직을 구현안했기때문에)
즉
이제 1번 과정까지 한것이고, 이제 페이스북으로 부터 정보를 받아오는 과정을 해야할텐데,
정보가 어떻게 넘어올까?
@Service
public class OAuth2DetailsService extends DefaultOAuth2UserService {
@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
System.out.println("OAuth2 서비스 ");
OAuth2User oAuth2User = super.loadUser(userRequest);
System.out.println(oAuth2User.getAttributes());
return null;
}
}
OAuth2DetailsService에 이렇게 적어보자
loadUser는 Override 해오면 된다.
그리고 OAuth2User 타입으로 super.loadUser(userRequest); 넣으면 정보를 파싱해서 가져오는데
그걸 sysout 으로 찍어본다.
이렇게하면 페이스북 로그인 시 500에러가 뜰텐데(이미 로그인 상태라면 키자마자 에러뜰거), 일단 무시하고 sysout 찍은것을 보면
이런식으로 회원정보를 찍어준다.
id와 name은 yml에서 설정한 public_profile 에 해당되고 email은 yml의 email 에 해당된다.
이제 이 정보를 통해서 회원가입과 로그인을 진행하면 된다.
'Spring > JPA + Security' 카테고리의 다른 글
[Security + JPA + OAuth2] 카카오 로그인 구현하기 -1 세팅하기 (0) | 2023.01.17 |
---|---|
[Security + JPA + OAuth2] - 페이스북 로그인 - 4 (0) | 2023.01.17 |
[Security + JPA + OAuth2] - 페이스북 로그인 - 2 (0) | 2023.01.15 |
[Security + JPA + OAuth2] - 페이스북 로그인 - 1 (0) | 2023.01.15 |
[Spring] AOP 처리하기 - 유효성 검사 자동화 (0) | 2023.01.15 |
댓글