Bird
0
0

In a Spring Security configuration, how do you ensure that only users with the role 'ADMIN' can access all actuator endpoints under /actuator/**?

easy📝 Syntax Q3 of 15
Spring Boot - Actuator
In a Spring Security configuration, how do you ensure that only users with the role 'ADMIN' can access all actuator endpoints under /actuator/**?
Ahttp.authorizeHttpRequests().requestMatchers("/actuator/**").permitAll().and().httpBasic();
Bhttp.authorizeHttpRequests().requestMatchers("/actuator/**").hasRole("ADMIN").and().formLogin();
Chttp.authorizeHttpRequests().anyRequest().authenticated().and().csrf().disable();
Dhttp.authorizeHttpRequests().requestMatchers("/actuator/**").hasAuthority("USER").and().formLogin();
Step-by-Step Solution
Solution:
  1. Step 1: Use requestMatchers for actuator endpoints

    The matcher /actuator/** targets all actuator endpoints.
  2. Step 2: Restrict access to role 'ADMIN'

    Using hasRole("ADMIN") ensures only users with this role can access these endpoints.
  3. Step 3: Enable form login

    Adding formLogin() enables a login page for authentication.
  4. Final Answer:

    http.authorizeHttpRequests().requestMatchers("/actuator/**").hasRole("ADMIN").and().formLogin(); correctly restricts actuator endpoints to ADMIN users.
  5. Quick Check:

    Matcher + hasRole + formLogin [OK]
Quick Trick: Use requestMatchers with hasRole for endpoint restriction [OK]
Common Mistakes:
  • Using permitAll() instead of hasRole() for actuator endpoints
  • Assigning wrong authority like USER instead of ADMIN
  • Not specifying the correct endpoint pattern

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes