ASPCode.net logo
  • Home 
  • Blogs 
  • Tags 
Blog
  1. Home
  2. Articles
  3. Java Spring Login With Github Account

Java Spring Login With Github Account

Posted on May 18, 2024  (Last modified on October 11, 2024) • 3 min read • 486 words
Java
 
Spring
 
Git
 
Java
 
Spring
 
Git
 
Share via
ASPCode.net
Link copied to clipboard

Video is in Swedish

On this page
  • Integrating GitHub Authentication with Java Spring
  • Why GitHub Authentication?
  • Setting up GitHub Authentication in Java Spring
    • Step 1: Create a GitHub App
    • Step 2: Configure Spring Security
    • Step 3: Implement GitHub Authentication
    • Step 4: Configure OAuth2Properties
    • Step 5: Enable OAuth2Login
    • Step 6: Test GitHub Authentication
  • Conclusion
  • Video
  • Sourcecode

Integrating GitHub Authentication with Java Spring  

In today’s digital age, secure authentication is crucial for any web application. One popular way to achieve this is by integrating social media platforms like GitHub into your login process. In this article, we’ll explore how to implement GitHub authentication using Java Spring.

Why GitHub Authentication?  

GitHub is a widely used platform with millions of users worldwide. By allowing users to log in with their GitHub accounts, you can:

  1. Simplify the registration process: Users don’t need to create new accounts or remember additional login credentials.
  2. Increase user engagement: GitHub authentication provides an easy and familiar way for users to access your application.
  3. Enhance security: GitHub’s OAuth 2.0 protocol ensures secure authentication and authorization.

Setting up GitHub Authentication in Java Spring  

To get started, you’ll need:

  1. A GitHub Developer account
  2. The Spring Boot framework (version 2.4.x or higher)
  3. The Spring Security module

Here are the steps to integrate GitHub authentication with your Java Spring application:

Step 1: Create a GitHub App  

Create a new GitHub app by following these steps:

  1. Log in to your GitHub account and navigate to the Developer settings page.
  2. Click on “New OAuth App” and fill in the required information (e.g., name, description, and redirect URI).
  3. Note down the Client ID and Client Secret, as you’ll need them later.

Step 2: Configure Spring Security  

In your Java Spring project, add the following dependencies to your pom.xml file (if using Maven) or build.gradle file (if using Gradle):

<!-- Maven -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

<!-- Gradle -->
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-security'
}

Step 3: Implement GitHub Authentication  

Create a new class that extends OAuth2UserService and implements the loadUserByUsername method:

@Service
public class GithubOAuth2UserService extends OAuth2UserService<OidcUserRequest, OidcUser> {

    @Override
    public OidcUser loadUserByUsername(String username) throws UsernameNotFoundException {
        // Implement your custom logic to handle GitHub authentication here
        return super.loadUserByUsername(username);
    }
}

Step 4: Configure OAuth2Properties  

In your application.properties file, add the following configuration:

spring:
  security:
    oauth2:
      client:
        registration:
          github:
            clientId: YOUR_CLIENT_ID
            clientSecret: YOUR_CLIENT_SECRET
            redirectUri: http://localhost:8080/login/oauth2/code/github

Replace YOUR_CLIENT_ID and YOUR_CLIENT_SECRET with the values from your GitHub app.

Step 5: Enable OAuth2Login  

In your SecurityConfig class, enable OAuth2 login:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.oauth2Login()
                .userInfoEndpointUrl("/userinfo")
                .userNameAttribute("login");
    }
}

Step 6: Test GitHub Authentication  

Start your Spring Boot application and navigate to the login page. You should see a button to log in with your GitHub account.

Conclusion  

Integrating GitHub authentication with Java Spring is a straightforward process that can enhance the security and user experience of your web application. By following these steps, you can provide an easy and secure way for users to access your application using their GitHub accounts.

Remember to replace YOUR_CLIENT_ID and YOUR_CLIENT_SECRET with the values from your GitHub app, and adjust the redirect URI according to your application’s requirements.

Video  

Swedish

Sourcecode  

Sourcecode
 
 Java Configuration Spring
Authentication Vs Authorization Part 1 
On this page:
  • Integrating GitHub Authentication with Java Spring
  • Why GitHub Authentication?
  • Setting up GitHub Authentication in Java Spring
    • Step 1: Create a GitHub App
    • Step 2: Configure Spring Security
    • Step 3: Implement GitHub Authentication
    • Step 4: Configure OAuth2Properties
    • Step 5: Enable OAuth2Login
    • Step 6: Test GitHub Authentication
  • Conclusion
  • Video
  • Sourcecode
Follow me

I code in Java, C#, Golang, SQL and more

     
© 2024 Systementor AB
ASPCode.net
Code copied to clipboard