Spring Security integrates with OAuth2 by offloading both authentication and authorization to a dedicated authorization server, then using the tokens that server issues to make access decisions inside the application.
An internal dashboard configured with oauth2Login() redirects unauthenticated users to the company's identity provider, then stores the returned token so subsequent calls to internal APIs are authorized automatically.
// One app can be both: users log in with OAuth2 (oauth2Login), and the
// /api endpoints accept bearer tokens from other clients (resource server)
@Bean
@Order(1)
SecurityFilterChain api(HttpSecurity http) throws Exception {
return http.securityMatcher("/api/**")
.authorizeHttpRequests(a -> a.anyRequest().hasAuthority("SCOPE_dashboard:read"))
.oauth2ResourceServer(o -> o.jwt(Customizer.withDefaults()))
.build();
}
@Bean
@Order(2)
SecurityFilterChain web(HttpSecurity http) throws Exception {
return http
.authorizeHttpRequests(a -> a.anyRequest().authenticated())
.oauth2Login(Customizer.withDefaults()) // authentication via the IdP
.build();
}A concise interview answer is
Spring Security integrates OAuth2 by redirecting authentication to an external authorization server and then using the access token it issues to authorize subsequent requests. This centralizes login logic in one trusted service instead of duplicating credential handling across every application.
Last updated: 1 November 2026
Previous question: You are tasked with designing a secure REST API for a banking application. What security practices would you implement in Spring Boot?
Next question: How do you handle session management in Spring Boot in the context of security?