Dev Learning Lab

The labs on this site need JavaScript to run. The same content is written out below.

Learn › Security › Authorization

Access control

If a logged-in user only changes the number in the URL, can they see someone else's order?

Why it exists — Not showing a link to someone else's order on the screen is not enough to stop access, because anyone can build and send a request directly from the address bar or with curl. Access control makes the server check the role and the owner of the resource on every request, so it refuses a request that is not allowed, whatever the screen shows.

Where it's used today — It is needed for every API that looks up, changes, or deletes a resource that has an owner, such as an order, a post, or an attachment, by its number. Places that are divided by role, such as admin addresses, are blocked with the URL rules of SecurityFilterChain. Whose resource it is cannot be known from the role alone, so you either add the logged-in user to the condition when you look the resource up in the DB, or check the owner with an expression of method security.

Being logged in only means that "who you are has been confirmed" (authentication). It does not mean "you may view this resource" (authorization). What happens if kim logs in and then changes only the order number in the URL to request lee's order? Depending on what the server checks, the order is either shown as it is or the request is refused.

Checked against real execution · A Spring Boot 4.1.1 and Spring Security 7.1.1 server was started on 127.0.0.1, and the response codes and bodies were checked for all 30 combinations

Concepts in this lab

Authentication and authorization
Authentication checks who a user is. Authorization decides whether that verified user has permission to use a specific resource or feature. Authentication happens first, and authorization is decided based on its result. In HTTP status codes, a failed authentication corresponds to 401 and a failed authorization corresponds to 403.
Access control and ownership checks
Why it exists — Authentication only tells who made the request, and does not tell whether that person may handle this resource. Access control has the server decide separately for each resource whether it is allowed, so that even a logged-in user reaches only the allowed resources.
Where it's used today — It is used for features in which a user must handle only their own things, such as the order history on a My Page, editing and deleting your own posts, and downloading your own files. People who have to handle other people's resources, such as support agents and administrators, are allowed separately after their role is checked.
Access control is the job of having the server decide, and enforce, which resources a verified user may view or change. If the server checks only that the user is logged in and does not check who owns the resource, one user can view another person's resource by changing just a number in the URL or the request. This kind of vulnerability is called Insecure Direct Object Reference (IDOR). To prevent it, after looking up the resource, the server code compares the owner of the resource with the user who is logged in now, or it looks up the resource only "among the user's own" from the start. Hiding a button or a link on the screen is not access control, and the check must always be made on the server, on every request.
Role-based access control (RBAC)
Why it exists — If you set what each user can do one by one, the settings must be changed and are easy to miss whenever someone joins or the work they hold changes. RBAC ties permissions to roles and gives users only roles, so that when a person changes, only the role needs changing.
Where it's used today — It is used to open admin screen addresses only to the ADMIN role, or in an internal system to divide the menus and actions available to employees, team leaders, and administrators.
A method in which users are given roles such as USER and ADMIN, and what each role may do is decided in advance. In Spring Security, you write the role that is needed for a URL or a method, as in hasRole("ADMIN"). The rules are simple and easy to manage in one place, but a role only says "what kind of user this is", so it cannot tell apart two users who have the same role. That is why a rule such as "you can view only your own orders" cannot be expressed with roles alone, and you must use it together with a check that compares the owner of the resource.
HTTP status codes
Why it exists — If results were reported only as text in the body, each server would write them differently, and browsers, proxies, and monitoring tools could not tell whether a request succeeded. A status code reports the result as a fixed number, so these tools can do caching, retries, and error counting without being adjusted for each server.
Where it's used today — It is used to branch handling by the kind of error, such as a frontend that sends the user to the login screen on a 401 and to a "page not found" notice on a 404. In operations, you watch the ratio of 5xx responses and, if it suddenly rises, treat it as an outage and send an alert.
A three-digit number that shows the result of the server's handling of a request. 2xx means success, 3xx means a move to another location (redirection), 4xx means a problem with the client's request, and 5xx means an error on the server side. Common ones are 200 (success), 201 (created), 400 (bad request), 401 (authentication required), 403 (no permission), 404 (not found), and 500 (server error).
Spring Security and SecurityFilterChain
Why it exists — If you wrote security checks yourself as Servlet filters, you had to arrange by hand, in every project, the order and the URLs of the filters for login checks, permission checks, and CSRF checks. Spring Security provides such filters in a fixed order, and SecurityFilterChain lets you choose which check to use for which URL.
Where it's used today — You configure form login, JWT verification, social login, and restrictions on admin URLs in a SecurityFilterChain bean. For groups with different rules, such as /api/** URLs and page URLs, you use more than one chain to split them.
Spring Security is Spring's security framework for handling authentication and authorization, and it works as a set of Servlet filters. A SecurityFilterChain defines which security filters apply to which requests and in what order. Before a request reaches the controller, the filters run one after another and do work such as checking the authentication information and checking permissions.

Try these

Running with the default settings

  1. kim, logged in, sends GET /orders/2. kim's order is order 1, and order 2 is lee's order. This is the URL with only the number changed to 2.
  2. Authentication: the server checks the login information carried in the request and finds out that the sender is kim with the role USER. So far it has only confirmed "who this is". No one has yet asked whether order 2 may be viewed.
  3. .anyRequest().authenticated(): the URL rule looks only at whether the user is logged in. kim is logged in, so the request passes. This rule does not know whose order the number 2 at the end of the URL refers to.
  4. orders.findById(2): the controller looks up the order by number. lee's order comes back.
  5. return o: the controller returns the order it looked up as it is. Nowhere in the code is the owner of the order (lee) compared with the logged-in user (kim).
  6. The response is 200 OK, and the body contains lee's order (the product and the delivery address), which goes to kim. The login was done properly and the request passed every rule on the server, yet someone else's order became visible.

Result of this run

Code

// The owner of order 1 is kim, and the owner of order 2 is lee. Order 99 does not exist.
// The role of kim and lee is USER, and the role of park is ADMIN.
// Only the needed parts are kept. Order and OrderRepository are types made for this example.
// orders is not a repository generated by Spring Data; it is an example object that looks up
// orders in an in-memory table. findById and findByIdAndOwner are also example methods.
private final OrderRepository orders = new OrderRepository();

@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    return http
        .authorizeHttpRequests(a -> a
            .anyRequest().authenticated())                 // checks only that the user is logged in
        .httpBasic(Customizer.withDefaults())              // how to log in (the result is the same whichever you use)
        .build();
}

@GetMapping("/orders/{id}")
Order one(@PathVariable long id, Authentication auth) {     // auth: the user logged in now
    Order o = orders.findById(id)
        .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
    return o;                                 // does not check whose order it is
}

Assumptions of this model

All 30 combinations were checked against real execution in our test run. A Spring Boot 4.1.1 and Spring Security 7.1.1 server was started on 127.0.0.1 with code of the same structure as on the screen, and the checks were the response code, the owner of the order in the response body, whether the controller ran, and which branch of the code made the decision. The order storage was made as an in-memory table instead of a database, and HTTP Basic was used to log in (the authorization result is the same if you log in with a session or a token). On the same server, a request that is not logged in got 401 with every approach, and that is covered in the "Security filter chain" lab. That a server can answer 404 instead of 403 for another person's resource, to hide the fact that the resource exists, is written in RFC 9110 Section 15.5.4 (403) and Section 15.5.5 (404). Which of 403 and 404 is right is not fixed; the service chooses. In a real service, the existence of a resource can also be revealed by a difference in response time, not only by the response code, but this lab does not cover that. The owner check was written in the controller, but it is also placed in the service layer or written with method security (@PreAuthorize, @PostAuthorize), and that continues in the "Method security" lab. Changing order numbers to values that are hard to guess (such as UUIDs) only makes the numbers harder to guess and does not replace an owner check. This sentence follows a general security recommendation (OWASP's description of Broken Access Control) and is not something that was checked by execution.

Previous lab: Security filter chain · Next lab: Method security

Concepts in this lab

What this lab shows

Settings

Code


      
    

Diagram

Run comparison

Try these