Java Backend Interview Q&A← All stacks

Spring Security — Interview Q&A Matrix

Parent stack: Spring Ecosystem Technology: Spring Security Levels: Senior Questions: 1

Each entry contains a Question, the Expected Depth, an Ideal Answer, and Red Flags.

Senior

Q1 — Securing an API with Spring Security and OAuth2

Question: How would you configure a Spring Boot 3 REST API as an OAuth2 resource server that validates JWTs and applies method-level authorization?

Expected Depth:

Ideal Answer:

yaml
spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: https://id.example.com/realms/payments
java
@Bean
SecurityFilterChain api(HttpSecurity http) throws Exception {
    http
        .sessionManagement(session ->
                session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
        .csrf(csrf -> csrf.disable())
        .cors(Customizer.withDefaults())
        .authorizeHttpRequests(authorize -> authorize
                .requestMatchers("/actuator/health/liveness").permitAll()
                .requestMatchers(HttpMethod.GET, "/payments/**")
                    .hasAuthority("SCOPE_payments:read")
                .anyRequest().authenticated())
        .oauth2ResourceServer(oauth2 -> oauth2
                .jwt(Customizer.withDefaults()));

    return http.build();
}
java
final class AudienceValidator implements OAuth2TokenValidator<Jwt> {
        private final String requiredAudience;

        AudienceValidator(String requiredAudience) {
                this.requiredAudience = requiredAudience;
        }

        @Override
        public OAuth2TokenValidationResult validate(Jwt jwt) {
                if (jwt.getAudience().contains(requiredAudience)) {
                        return OAuth2TokenValidationResult.success();
                }

                OAuth2Error error = new OAuth2Error(
                                "invalid_token",
                                "The token audience is not accepted",
                                null);
                return OAuth2TokenValidationResult.failure(error);
        }
}

@Bean
JwtDecoder jwtDecoder(OAuth2ResourceServerProperties properties) {
    NimbusJwtDecoder decoder = JwtDecoders.fromIssuerLocation(
            properties.getJwt().getIssuerUri());

    OAuth2TokenValidator<Jwt> issuer = JwtValidators.createDefaultWithIssuer(
            properties.getJwt().getIssuerUri());
    OAuth2TokenValidator<Jwt> audience = new AudienceValidator("payments-api");

    decoder.setJwtValidator(
            new DelegatingOAuth2TokenValidator<>(issuer, audience));
    return decoder;
}
java
@EnableMethodSecurity
@Configuration
class MethodSecurityConfig {}

@PreAuthorize("hasAuthority('SCOPE_payments:write')")
public void refund(String paymentId) {
    paymentService.refund(paymentId);
}

Red Flags:


Scoring Rubric

Level Pass bar
JuniorExplains authentication, authorization, and bearer-token basics.
SeniorValidates tokens fully and applies least-privilege endpoint and method rules.
ArchitectDefines identity, key rotation, revocation, audit, and failure-handling standards.