.....
.and()
.oauth2Login()
.loginPage("/loginForm")
// 코드 추가
.userInfoEndpoint()
.userService(null)
config에 oauth패키지 추가 후 PrincipalOAuth2UserService 생성
@Service
public class PrincipalOauth2UserService extends DefaultOAuth2UserService {
// 구글로부터 받은 userRequest 데이터에 대한 후처리가 되는 함수
@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
}
...
@Autowired
private PrincipalOauth2UserService principalOauth2UserService;
...
// 코드 추가
.userInfoEndpoint()
.userService(principalOauth2UserService)
// 구글로 부터 받은 userRequest데이터를 후처리하는 함수
@Override
@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
// registrationId로 어떤 oauth로 로그인 했는지 알 수 있음
System.out.println("userRequest : " + userRequest.getClientRegistration());
System.out.println("userRequest = " + userRequest.getAccessToken().getTokenValue());
// 구글 로그인 버튼 클릭 -> 구글 로그인 창 -> 로그인을 완료
// -> code를 리턴(Oauth-client라이브러리 -> AccessToken 요청
// userRequest정보 -> loadUser함수 호출 -> 구글로부터 회원 프로필을 받아줌
System.out.println("userRequest = " + super.loadUser(userRequest).getAttributes());
OAuth2User oAuth2User = super.loadUser(userRequest);
// 회원가입을 강제로 진행해볼 예점
return super.loadUser(userRequest);
}
getClientRegistration : ClientRegistration{registrationId='google', clientId='510342946043-hesh5cb2t1asij1ktqdvvp8mrik37ohk.apps.googleusercontent.com', clientSecret='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', clientAuthenticationMethod=org.springframework.security.oauth2.core.ClientAuthenticationMethod@4fcef9d3, authorizationGrantType=org.springframework.security.oauth2.core.AuthorizationGrantType@5da5e9f3, redirectUri='{baseUrl}/{action}/oauth2/code/{registrationId}', scopes=[email, profile], providerDetails=org.springframework.security.oauth2.client.registration.ClientRegistration$ProviderDetails@3b91c570, clientName='Google'}
getAccessToken = ya29.a0Ael9sCPz9XVTqr2LdMGnh6AI1B8zyCERtbyOgMfcd0mtqeOk2mgjlMsUHy_2qXtfxxxcZ_Gh3CDkfxG2aziJz2raQh-CV-7MbuFKcKUoutHrarbMxif8JDDJ0DFyzz457fZGsv4K4DfyPcpdkltcymPqAJx0LAaCgYKAY0SARASFQF4udJhZPEig6jBbyILauToC8ASnQ0165
getAttributes = {sub=105156291955329144943, name=박민, given_name=민, family_name=박, picture=https://lh3.googleusercontent.com/a/AGNmyxaP5PqWp_Owhh4DmwwCmzSYx_4114WaKnhyuOpQ=s96-c, email=magicofclown@gmail.com, email_verified=true, locale=ko}
{sub=105156291955329144943,
name=박민,
given_name=민,
family_name=박,
picture=https://lh3.googleusercontent.com/a/AGNmyxaP5PqWp_Owhh4DmwwCmzSYx_4114WaKnhyuOpQ=s96-c, email=magicofclown@gmail.com,
email_verified=true,
locale=ko}
username = google_{sub=105156291955329144943
password = "암호화(겟인데어)"
email = "magicofclown@gmail.com"
role = "ROLE_USER"
provider = "google"
providerId = 105156291955329144943
@Entity
@Data
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String username;
private String password;
private String email;
private String role; // ROLE_USER, ROLE_ADMIN
// provider / providerId 추가
private String provider; // 값을 받아온 도메인('google')
private String providerId; // 도메인에서 사용하는 id('sub값')
private Timestamp loginDate; // 로그인 한 날짜
@CreationTimestamp
private Timestamp createDate;
}