구글 로그인을 위해서는 먼저 개발자 콘솔을 이용해서 프로젝트를 생성해야 합니다.
https://console.cloud.google.com/
Google 클라우드 플랫폼
로그인 Google 클라우드 플랫폼으로 이동
accounts.google.com
위 페이지에 접속 후에 오른쪽 상단 새 프로젝트 생성 클릭


프로젝트 이름 설정 후 만들기 버튼 클릭

API 서비스 클릭

사용자 인증 정보 -> 사용자 인증 정보 만들기 -> OAuth 클라이언트 ID ->

동의 화면 구성 클릭


애플리케이션 이름 지정 후 저장후 계속
다음 화면에 넘어가면 취소 눌러서

해당 화면 넘어오면 OAuth 클라이언트 ID 클릭

사진과 같이 작성 후 만들기 클릭

OAuth 클라이언트가 설정됨
프로젝트 내 구글 설정
프로젝트에 소셜 로그인을 적용하기 위해서는 OAuth 관련된 라이브러리를 먼저 추가 해야 합니다.
build:gradle
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client

resources 밑에 application-oauth.properties 파일 생성 후 아래와 같이 작성
spring.security.oauth2.client.registration.google.client-id=생성된 클라이언트 아이디
spring.security.oauth2.client.registration.google.client-secret= 생성된 클라이언트 비밀번호
spring.security.oauth2.client.registration.google.scope=email
기존의 application.properties에서는 아래 설정을 추가 (oauth.properties 가 동작하기 위함)
spring.profiles.include=oauth
SecurityConfig 클래스의 filterChain을 아래와 같이 수정
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{
http.authorizeHttpRequests((auth) -> {
auth.requestMatchers("/sample/all").permitAll();
auth.requestMatchers("/sample/member").hasRole("USER");
});
http.formLogin(); // 인가 인증에 문제시 로그인 화면 띄우기 (유저 권한인데 관리자 페이지에 접속 시도하거나 그럴 때)
http.csrf().disable(); // CSRF 토큰 발행하지 않음 설정
// http.logout(); // 인가 인증에 문제시 로그아웃 화면 띄우기, CSRF 토큰을 사용할 때는 반드시 POST 방식으로만 로그아웃 처리해야함
http.logout(); // csrf 토큰 비활성화시 GET 방식으로 로그아웃
http.oauth2Login(); // google login
return http.build();
}