These two are often confused because OIDC is built on top of OAuth2.

The purpose of OAuth2 ist authorization and the question: can this app access data on my behaf? OpenID Connect takes care of identity in addition and answers the question: who is the logged-in user?

Think of OAuth2 as a key for a room, OIDS as a passport, that clears you identity.

For a minimal example. We can first add the dependencies to the pom.xml:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
</dependencies>

And a minmal security configuration in Spring Boot:

@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/", "/public").permitAll()
.anyRequest().authenticated()
)
.oauth2Login(Customizer.withDefaults());
return http.build();
}
}

OIDC providers (Google, Keycloak) inject OidcUser; plain OAuth2 providers (Facebook) inject OAuth2User.

@RestController
public class UserController {
@GetMapping("/")
public String home() {
return "Welcome!";
}
@GetMapping("/user")
public Map<String, Object> oidcUser(@AuthenticationPrincipal OidcUser user) {
return Map.of(
"name", user.getFullName(),
"email", user.getEmail(),
"claims", user.getClaims()
);
}
@GetMapping("/facebook")
public Map<String, Object> oauth2User(@AuthenticationPrincipal OAuth2User user) {
return Map.of("attributes", user.getAttributes());
}
}

And htere the application.yaml config:

spring:
security:
oauth2:
client:
registration:
google:
client-id: YOUR_GOOGLE_CLIENT_ID
client-secret: YOUR_GOOGLE_SECRET
scope: [openid, profile, email] # openid triggers OIDC
facebook:
client-id: YOUR_FACEBOOK_APP_ID
client-secret: YOUR_FACEBOOK_SECRET
scope: [public_profile, email] # no openid — plain OAuth2
keycloak:
client-id: spring-client
client-secret: YOUR_KEYCLOAK_SECRET
authorization-grant-type: authorization_code
scope: [openid, profile, email]
provider:
keycloak:
issuer-uri: http://localhost:8081/realms/demo

Before starting this application, we should start keycloak docker container: docker compose up

# docker-compose.yml
version: '3.9'
services:
keycloak:
image: quay.io/keycloak/keycloak:26.2
container_name: local_keycloak
command: start-dev
environment:
KEYCLOAK_ADMIN: admin
KEYCLOAK_ADMIN_PASSWORD: admin
ports:
- "8081:8080"

Then open http://localhost:8081 and log in with admin / admin.

Keycloak setup checklist

  1. Create a Realm named demo
  2. Create a Client — type OpenID Connect, ID spring-client
  3. Enable Client Authentication (required to generate a client secret)
  4. Set Valid Redirect URIs to http://localhost:8080/login/oauth2/code/keycloak
  5. Copy the secret from the Credentials tab into application.yml
  6. Create a test User under the Users tab so you can log in

Hinterlasse einen Kommentar

I’m Iman

Mein Name ist Iman Dabbaghi. Ich arbeite als Senior Software Engineer in der Schweiz. Außerdem interessiere ich mich sehr für gewaltfreie Kommunikation, Bachata-Tanz und Musik sowie fürs die Persönlichkeitsentwicklung.

Ich habe einen Masterabschluss in Informatik von der Universität Freiburg in Deutschland, bin Spring/Java Certified Professional (OCP), Certified Professional for Software Architecture (CPSA-F) und ein lebenslanger Lernender 🎓.

EN:

My name is Iman Dabbaghi. I work as a Senior Software Engineer in Switzerland. I am also very interessted in nonviolent communication, Bachata dance and music and also for personal development.

I hold a masters degree in computer science from the university of Freiburg in Germany, am a Spring / Java Certified Professional (OCP), Certified Software Architecture (CPSA-F) and Life Long Learner🎓

Let’s connect