If you need to secure REST endpoints based on user roles, what Spring Security configurations would you use?

Securing REST endpoints by role in Spring Security means declaring authorization rules on the HttpSecurity chain that tie specific URL patterns to required roles or authorities, so unauthorized requests are rejected before reaching the controller.

Key Points

Example

A REST API secures /api/orders/** so only users with ROLE_MANAGER can create or delete orders, while any authenticated user can view them via a separate, less restrictive rule.

http.authorizeHttpRequests(auth -> auth
    .requestMatchers(HttpMethod.GET, "/api/orders/**").authenticated()
    .requestMatchers(HttpMethod.POST, "/api/orders/**").hasRole("MANAGER")
    .anyRequest().denyAll());

Interview Tip

A concise interview answer is

I'd use authorizeHttpRequests() in the SecurityFilterChain, chaining requestMatchers() with hasRole() or hasAuthority() checks per endpoint and HTTP method. That gives fine-grained, declarative control over exactly which roles can hit which REST operations.

Last updated: 16 July 2026

Previous question: What are the differences between method security and URL security in Spring Security?

Next question: What are the core classes of Spring Security, and is setup different in Spring MVC and Spring Boot?

More Spring Security - Level II interview questions

All Spring Security - Level II questions