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

URL security and method security are two complementary layers in Spring Security: URL security gates access based on the request path, while method security gates access based on which specific method is being invoked, regardless of how the call arrived.

Key Points

Example

A URL rule blocks non-admins from reaching /admin/**, but a @PreAuthorize on the deleteUser() service method also blocks the same operation if it's ever invoked from a different, unprotected controller or scheduled job.

// URL security: coarse rules in the filter chain, applied to HTTP requests
http.authorizeHttpRequests(a -> a
        .requestMatchers("/admin/**").hasRole("ADMIN")
        .anyRequest().authenticated());

// Method security: rules on the business methods themselves,
// applied no matter which controller, job or listener calls them
@Configuration
@EnableMethodSecurity
class MethodSecurityConfig {}

@Service
public class UserService {

    @PreAuthorize("hasRole('ADMIN')")
    public void deleteUser(Long id) { ... }

    @PreAuthorize("#username == authentication.name or hasRole('ADMIN')")
    public Profile profile(String username) { ... }

    @PostAuthorize("returnObject.owner == authentication.name")
    public Document load(Long id) { ... }
}

Interview Tip

A concise interview answer is

URL security restricts access based on the request path, configured centrally in the filter chain, while method security uses annotations like @PreAuthorize directly on methods for finer, context-aware control that applies no matter how the method is invoked. In practice I use both—URL rules as a first gate, method rules for precise enforcement.

Last updated: 1 November 2026

Previous question: Can you describe an approach to implement security in service-to-service communication?

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

More Spring Security - Level II interview questions

All Spring Security - Level II questions