찾다
Javajava지도 시간Spring Boot의 OAuth 인증: Google과 GitHub 로그인 통합 가이드

OAuth 2.0으로 보안 강화: Spring Boot에서 소셜 로그인 구현

최신 웹 개발 세계에서는 애플리케이션을 보호하고 사용자를 위해 최대한 원활한 인증을 제공하는 것이 최우선 과제입니다. OAuth 2.0은 API 보안에 도움이 될 뿐만 아니라 사용자가 Google 및 GitHub와 같은 플랫폼에서 기존 계정으로 로그인할 수 있게 해주는 강력한 도구입니다. 이를 통해 모든 사람이 작업을 더 쉽게 할 수 있습니다. 사용자는 또 다른 비밀번호를 기억할 필요가 없으며 개발자는 신뢰할 수 있는 인증 관리 방법을 얻게 됩니다.

이 블로그에서는 Spring Boot 애플리케이션에서 OAuth 2.0을 설정하는 방법을 단계별로 안내하겠습니다. 인증을 위해 Google과 GitHub를 모두 통합할 예정이므로 사용자는 로그인에 사용할 서비스를 선택할 수 있습니다. 또한 JWT(JSON 웹 토큰)를 사용하여 API 엔드포인트를 보호하는 방법도 보여 드리겠습니다. 인증된 사용자는 자신이 원하는 리소스에 액세스할 수 있습니다.

새로운 앱을 구축하든 기존 앱에 보안을 추가하든 이 가이드는 Spring Boot 애플리케이션을 안전하고 사용자 친화적으로 유지하는 데 필요한 도구를 제공합니다.

https://start.spring.io/ 방문

프로젝트 생성

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

zip을 다운로드하고 압축을 푼 후 프로젝트를 IDE에 로드하세요.

Spring Boot의 "OAuth2 클라이언트" 종속성은 OAuth 2.0 인증을 Google 및 GitHub와 같은 제공업체와 통합하는 것을 단순화합니다. 사용자를 공급자의 로그인 페이지로 리디렉션하고, 토큰을 관리하고, API 엔드포인트를 보호하는 등 전체 OAuth 로그인 흐름을 처리합니다. 이 종속성을 추가하면 Spring Boot 애플리케이션에서 안전하고 사용자 친화적인 인증을 쉽게 활성화할 수 있습니다.

Spring Boot의 "Spring Web" 종속성은 웹 애플리케이션 개발에 매우 ​​중요합니다. RESTful API 생성, MVC 아키텍처 지원, HTML 보기 제공 기능과 같은 필수 기능을 제공합니다. Spring Web을 사용하면 HTTP 요청 및 응답을 쉽게 처리하고, 라우팅을 관리하고, 다른 Spring 구성 요소와 통합할 수 있으므로 강력한 웹 애플리케이션 구축의 기본 부분이 됩니다.

애플리케이션 구성

Google 및 GitHub에서 OAuth 2.0 인증을 위해 Spring Boot 애플리케이션을 설정하려면 application.properties 파일을 구성해야 합니다. 이 파일에는 OAuth 클라이언트 자격 증명, 로깅 수준, JWT 구성 등 애플리케이션에 대한 필수 설정이 포함되어 있습니다.

spring.application.name=oauth2-authentication-service
server.port=8000

#for google
spring.security.oauth2.client.registration.google.client-id=YOUR_GOOGLE_CLIENT_ID
spring.security.oauth2.client.registration.google.client-secret=YOUR_GOOGLE_CLIENT_SECRET

#for github
spring.security.oauth2.client.registration.github.client-id=YOUR_GITHUB_CLIENT_ID
spring.security.oauth2.client.registration.github.client-secret= YOUR_GITHUB_CLIENT_SECRET

OAuth 클라이언트 구성: YOUR_GOOGLE_CLIENT_ID, YOUR_GOOGLE_CLIENT_SECRET, YOUR_GITHUB_CLIENT_ID 및 YOUR_GITHUB_CLIENT_SECRET를 애플리케이션 등록 시 Google 및 GitHub에서 얻은 자격 증명으로 바꾸세요.

OAuth 2.0 인증을 위해 Google 및 GitHub에 애플리케이션을 등록하려면 https://console.cloud.google.com으로 이동해야 합니다.

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

API 서비스 클릭

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

자격증명 -> 자격 증명 만들기 -> OAuth 클라이언트 ID

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

OAuth 클라이언트 ID -> OAuth 클라이언트 ID 생성

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

애플리케이션 유형웹 애플리케이션

으로 선택하세요.

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

애플리케이션 이름 지정

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

이 URL을 사용하여 승인된 리디렉션 URI를 설정하고 여기서 애플리케이션이 8000 포트에서 실행되고 있으므로 애플리케이션 포트는 8000입니다. 그런 다음 만들기를 클릭하세요

http://localhost:8000/login/oauth2/code/google

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

이후 OAuth 클라이언트가 생성되고 클라이언트 ID와 클라이언트 비밀번호를 받습니다.

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

copy both and replace with the the properties of application.properties file

spring.security.oauth2.client.registration.google.client-id=YOUR_GOOGLE_CLIENT_ID
spring.security.oauth2.client.registration.google.client-secret=YOUR_GOOGLE_CLIENT_SECRET

The SecurityConfig class configures security for a Spring Boot application using OAuth2. It defines a SecurityFilterChain bean, which sets up security rules. The authorizeHttpRequests method ensures that all incoming requests require authentication. The .oauth2Login(Customizer.withDefaults()) line enables OAuth2 login functionality with default settings. Finally, the securityFilterChain method returns the configured security filter chain by calling http.build(). This setup ensures that the application is secure and supports OAuth2 authentication for users.

Accessing Your Application via Chrome

When developing and testing your Spring Boot application, it's crucial to know how to interact with it through Postman. If your application is running locally on port 8000, you can access it using the following base URL:

http://localhost:8000

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

we get the similar response like this

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

now we can access the end points.

GitHub Authentication

GitHub Authentication in Spring Boot allows users to log in using their GitHub accounts, streamlining the authentication process and enhancing security. By integrating GitHub as an OAuth 2.0 provider, your application can authenticate users through GitHub's trusted platform. This involves registering your application on GitHub to obtain a Client ID and Client Secret, which are then configured in your Spring Boot application. Users are redirected to GitHub for login, and upon successful authentication, they are redirected back to your application with an access token, allowing secure access to your protected resources. This integration is ideal for applications targeting developers and tech-savvy users.

create GitHub account and go to settings

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

in the left corner we get the developer settings

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

Navigate to OAuth Apps

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

click on create OAuth App

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

we get the interface like this

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

set ** Authorization callback URL ** according to your application port

http://localhost:8000/login/oauth2/code/github

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

and set Homepage URL

http://localhost:8000

after registering the Application we get the Client ID and Client Secret

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

now replace with the Application.properties file properties

spring.security.oauth2.client.registration.github.client-id=Ov23liBMLc5e1ItoONPx
spring.security.oauth2.client.registration.github.client-secret= 

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

Test the GitHub Login

Login with GitHub: When prompted, log in with your GitHub credentials.
Success Redirect: Upon successful authentication, you'll be redirected to the /home page of your application.

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

내 GitHub 저장소에서 사용자 인증 서비스의 전체 소스 코드를 탐색할 수 있습니다. 이 프로젝트에서는 인증을 위해 JWT를 사용한 사용자 등록, 로그인, 보안 액세스 등 다양한 기능을 선보입니다. 자유롭게 확인하고, 기여하고, 자신의 프로젝트에 참고 자료로 사용하세요!

GitHub 저장소: https://github.com/ishrivasayush/oauth2-authentication-service

결론

Google 및 GitHub를 인증 공급자로 사용하여 Spring Boot로 OAuth 2.0을 구현하는 것은 애플리케이션의 보안과 유용성을 향상시키는 강력한 방법입니다. 사용자가 기존 계정으로 로그인할 수 있도록 허용하면 마찰을 줄이고 보다 원활한 사용자 경험을 제공할 수 있습니다. 동시에 JWT로 API 엔드포인트를 보호하면 인증된 사용자만 민감한 리소스에 액세스할 수 있습니다.

이 가이드를 통해 Google 및 GitHub에서 OAuth 자격 증명을 설정하는 것부터 인증을 처리하고 엔드포인트를 보호하도록 Spring Boot 애플리케이션을 구성하는 것까지 모든 것을 다루었습니다. OAuth 2.0을 처음 사용하거나 이를 프로젝트에 통합하려는 경우 이러한 단계는 안전하고 확장 가능한 인증 시스템을 구축하는 데 도움이 됩니다.

보안은 끝없는 여정이지만 올바른 도구와 방법을 사용하면 안전하고 사용자 친화적인 애플리케이션을 구축할 수 있습니다. 이제 탄탄한 기반이 마련되었으므로 더 많은 공급자를 추가하거나, 사용자 경험을 사용자 정의하거나, JWT 구성에 대해 더 자세히 알아보면서 더 자세히 탐색할 수 있습니다. 즐거운 코딩하세요!

위 내용은 Spring Boot의 OAuth 인증: Google과 GitHub 로그인 통합 가이드의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
2025 년 상위 4 개의 JavaScript 프레임 워크 : React, Angular, Vue, Svelte2025 년 상위 4 개의 JavaScript 프레임 워크 : React, Angular, Vue, SvelteMar 07, 2025 pm 06:09 PM

이 기사는 2025 년에 상위 4 개의 JavaScript 프레임 워크 (React, Angular, Vue, Svelte)를 분석하여 성능, 확장 성 및 향후 전망을 비교합니다. 강력한 공동체와 생태계로 인해 모두 지배적이지만 상대적으로 대중적으로

카페인 또는 구아바 캐시와 같은 라이브러리를 사용하여 자바 애플리케이션에서 다단계 캐싱을 구현하려면 어떻게해야합니까?카페인 또는 구아바 캐시와 같은 라이브러리를 사용하여 자바 애플리케이션에서 다단계 캐싱을 구현하려면 어떻게해야합니까?Mar 17, 2025 pm 05:44 PM

이 기사는 카페인 및 구아바 캐시를 사용하여 자바에서 다단계 캐싱을 구현하여 응용 프로그램 성능을 향상시키는 것에 대해 설명합니다. 구성 및 퇴거 정책 관리 Best Pra와 함께 설정, 통합 및 성능 이점을 다룹니다.

Node.js 20 : 주요 성능 향상 및 새로운 기능Node.js 20 : 주요 성능 향상 및 새로운 기능Mar 07, 2025 pm 06:12 PM

Node.js 20은 V8 엔진 개선, 특히 더 빠른 쓰레기 수집 및 I/O를 통해 성능을 크게 향상시킵니다. 새로운 기능에는 더 나은 webAssembly 지원 및 정제 디버깅 도구, 개발자 생산성 및 응용 속도 향상이 포함됩니다.

Java의 클래스로드 메커니즘은 다른 클래스 로더 및 대표 모델을 포함하여 어떻게 작동합니까?Java의 클래스로드 메커니즘은 다른 클래스 로더 및 대표 모델을 포함하여 어떻게 작동합니까?Mar 17, 2025 pm 05:35 PM

Java의 클래스 로딩에는 부트 스트랩, 확장 및 응용 프로그램 클래스 로더가있는 계층 적 시스템을 사용하여 클래스로드, 링크 및 초기화 클래스가 포함됩니다. 학부모 위임 모델은 핵심 클래스가 먼저로드되어 사용자 정의 클래스 LOA에 영향을 미치도록합니다.

빙산 : 데이터 호수 테이블의 미래빙산 : 데이터 호수 테이블의 미래Mar 07, 2025 pm 06:31 PM

대규모 분석 데이터 세트를위한 오픈 테이블 형식 인 Iceberg는 데이터 호수 성능 및 확장 성을 향상시킵니다. 내부 메타 데이터 관리를 통한 Parquet/Orc의 한계를 해결하여 효율적인 스키마 진화, 시간 여행, 동시 W를 가능하게합니다.

Spring Boot Snakeyaml 2.0 CVE-2022-1471 문제 고정Spring Boot Snakeyaml 2.0 CVE-2022-1471 문제 고정Mar 07, 2025 pm 05:52 PM

이 기사는 원격 코드 실행을 허용하는 중요한 결함 인 Snakeyaml의 CVE-2022-1471 취약점을 다룹니다. Snakeyaml 1.33 이상으로 Spring Boot 응용 프로그램을 업그레이드하는 방법에 대해 자세히 설명합니다.

Java에서 기능 프로그래밍 기술을 어떻게 구현할 수 있습니까?Java에서 기능 프로그래밍 기술을 어떻게 구현할 수 있습니까?Mar 11, 2025 pm 05:51 PM

이 기사는 Lambda 표현식, 스트림 API, 메소드 참조 및 선택 사항을 사용하여 기능 프로그래밍을 Java에 통합합니다. 간결함과 불변성을 통한 개선 된 코드 가독성 및 유지 관리 가능성과 같은 이점을 강조합니다.

고급 Java 프로젝트 관리, 구축 자동화 및 종속성 해상도에 Maven 또는 Gradle을 어떻게 사용합니까?고급 Java 프로젝트 관리, 구축 자동화 및 종속성 해상도에 Maven 또는 Gradle을 어떻게 사용합니까?Mar 17, 2025 pm 05:46 PM

이 기사에서는 Java 프로젝트 관리, 구축 자동화 및 종속성 해상도에 Maven 및 Gradle을 사용하여 접근 방식과 최적화 전략을 비교합니다.

See all articles

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

mPDF

mPDF

mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기