Chaturmind
LearnDSASystem DesignBlogPremium
Sign inGet started
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML

Company

  • Blog
  • Premium
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Spring Security & JWT Auth

Spring Security Basics

  • Spring Security Overview
  • JWT Authentication

Authorization

  • Role-Based Access Control
  • Password Encoding
  • OAuth2 & Social Login Basics
Chaturmind
← Spring Security & JWT Auth

Spring Security Basics

  • Spring Security Overview
  • JWT Authentication

Authorization

  • Role-Based Access Control
  • Password Encoding
  • OAuth2 & Social Login Basics
HomeLearnSpring BootSpring SecurityOAuth2 & SSO
✓ FreeIntermediate· 13 min read

OAuth2 Basics

Understand OAuth2 flows, implement Google sign-in, and use Spring Security's OAuth2 resource server.

Published March 5, 2025


OAuth2 Basics with Spring Security

OAuth2 is an authorization framework that lets a third-party application access user resources on another service without exposing the user's credentials.

OAuth2 Roles

  • Resource Owner: the user
  • Client: your application
  • Authorization Server: issues tokens (Google, GitHub, your own Keycloak)
  • Resource Server: holds the protected resources (your API)

Authorization Code Flow (most common)

1. User clicks "Sign in with Google"
2. Browser redirects to Google's authorization endpoint
3. User grants permission on Google
4. Google redirects back with an authorization code
5. Your server exchanges the code for access_token + id_token
6. Your server validates the token and creates a session

Spring Boot OAuth2 Login (Social Login)

<!-- pom.xml -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
# application.yml
spring:
  security:
    oauth2:
      client:
        registration:
          google:
            client-id: ${GOOGLE_CLIENT_ID}
            client-secret: ${GOOGLE_CLIENT_SECRET}
            scope: openid,email,profile
          github:
            client-id: ${GITHUB_CLIENT_ID}
            client-secret: ${GITHUB_CLIENT_SECRET}
@Configuration
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        return http
            .authorizeHttpRequests(auth -> auth
                .requestMatchers("/", "/login").permitAll()
                .anyRequest().authenticated())
            .oauth2Login(oauth2 -> oauth2
                .loginPage("/login")
                .successHandler(oAuth2SuccessHandler()) // custom handler
                .userInfoEndpoint(ui ->
                    ui.userService(customOAuth2UserService())) // enrich user
            )
            .build();
    }
}

Custom OAuth2 User Service

@Service
@RequiredArgsConstructor
public class CustomOAuth2UserService extends DefaultOAuth2UserService {

    private final UserRepository userRepository;

    @Override
    public OAuth2User loadUser(OAuth2UserRequest request) {
        OAuth2User oauthUser = super.loadUser(request);

        String email = oauthUser.getAttribute("email");
        String name  = oauthUser.getAttribute("name");
        String provider = request.getClientRegistration().getRegistrationId();

        // Upsert user in our DB
        User user = userRepository.findByEmail(email)
            .orElseGet(() -> new User(email, name, provider));
        user.setLastLoginAt(Instant.now());
        userRepository.save(user);

        return oauthUser;
    }
}

OAuth2 Resource Server (JWT)

For APIs secured by an Authorization Server (Keycloak, Auth0):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: https://accounts.google.com
          # or: jwk-set-uri: https://accounts.google.com/.well-known/openid-configuration
http.oauth2ResourceServer(oauth2 -> oauth2.jwt(Customizer.withDefaults()));
// Spring auto-validates token signature, expiry, and issuer

Key Concepts

TermMeaning
access_tokenShort-lived credential to access resources
refresh_tokenLong-lived token to get new access_tokens
id_tokenOpenID Connect: JWT about the user
scopeWhat permissions were granted
PKCEProof Key for Code Exchange — prevents code interception

Interview Tips

  1. OAuth2 vs OpenID Connect: OAuth2 is for authorization; OIDC extends OAuth2 to add authentication (the id_token).
  2. Client Credentials flow: for machine-to-machine (no user), used for microservice APIs.
  3. Never store tokens in localStorage — use httpOnly cookies or the Backend for Frontend (BFF) pattern.

Previous

Password Encoding

AI Tutor

Lesson: OAuth2 Basics

Quick actions

AI responses can be inaccurate. Verify critical information.