00:00

@Secured Annotation in Spring Security

Last updated:

@Secured is Spring Security's original method security annotation. It takes a list of authority names, and the annotated method may only be called by a user who has at least one of them. There are no expressions, no arguments and no bean calls: just "who may call this".

That simplicity is both its strength and its limit. This tutorial explains how @Secured is enabled and evaluated, the rule about full authority names that catches many developers, how class-level annotations combine with method-level ones, and when to move to @PreAuthorize. The example is a warehouse inventory service.

1. What Is @Secured?

@Secured lives in org.springframework.security.access.annotation. Its value is an array of strings. Spring Security's SecuredAuthorizationManager grants access when the current user holds any of the listed authorities. The comparison is exact: @Secured("ROLE_MANAGER") matches the authority ROLE_MANAGER, and no prefix is added for you.

It is disabled by default. Turn it on with @EnableMethodSecurity(securedEnabled = true). Like every method security annotation, it is enforced by a Spring proxy, as explained in method-level security.

2. Why Use @Secured?

  • Readable role lists. @Secured({"ROLE_CLERK", "ROLE_MANAGER"}) needs no knowledge of SpEL.
  • Compile-time friendly. Authority names can be constants, so a typo becomes a compile error rather than a silent denial.
  • Existing code. Many older Spring applications use it, and it remains fully supported.

It cannot express conditions on arguments or return values, combine rules with and, or call beans. For those, use @PreAuthorize.

3. How @Secured Is Evaluated

Evaluation of @SecuredThe proxy reads the authority list from the method, or from the class if the method has none, and grants access if the user holds any listed authority; otherwise it throws AuthorizationDeniedException. Method call through the proxy Method has @Secured? use its list Otherwise class @Secured use the class list User has any listed authority? Yes: run No: 403
Figure 1: a method-level @Secured overrides the class-level one; any one matching authority is enough.
Annotation valueMatches the authorityNotes
@Secured("ROLE_MANAGER")ROLE_MANAGERCorrect way to require a role
@Secured("MANAGER")MANAGERUsually a mistake: users built with roles("MANAGER") do not match
@Secured({"ROLE_CLERK", "ROLE_MANAGER"})Either oneAny-of semantics; there is no "all of"
@Secured("inventory:export")inventory:exportWorks for permission-style authorities too

4. A Complete Example: Warehouse Inventory

Stock clerks look up stock levels, warehouse managers see write-off reports, an export requires a dedicated permission, and auditors have their own service. The example uses Java 21, Spring Boot 4.1.2 and Spring Security 7.1.2. Authority names are kept in a constants class so the compiler catches typos.

package com.javabytechie.warehouse;

public final class Authorities {
    public static final String CLERK = "ROLE_STOCK_CLERK";
    public static final String MANAGER = "ROLE_WAREHOUSE_MANAGER";
    public static final String AUDITOR = "ROLE_AUDITOR";
    public static final String EXPORT = "inventory:export";

    private Authorities() {
    }
}

5. Step-by-Step Code Explanation

  1. securedEnabled = true registers the interceptor for @Secured. Without it, every method in this example would be open to any logged-in user.
  2. Constants with full names. Authorities.MANAGER is ROLE_WAREHOUSE_MANAGER, including the prefix, because @Secured compares exact strings. The same constants are used when creating the users.
  3. Any-of lists. stockLevel accepts clerks or managers.
  4. A permission. exportInventory requires inventory:export, which only Rina has, regardless of roles.
  5. Class and method levels. AuditService is restricted to auditors at class level. monthlySummary declares its own list, which replaces the class rule rather than adding to it.

6. Expected Output

Terminal
$ curl -u kamal:kamal-pass localhost:8080/api/stock/SKU-881
HTTP/1.1 200
SKU-881: 148 units in aisle 12

$ curl -u kamal:kamal-pass localhost:8080/api/stock/write-off-report
HTTP/1.1 403
{"timestamp":"2026-11-18T12:14:57.237Z","status":403,"error":"Forbidden","path":"/api/stock/write-off-report"}

$ curl -u rina:rina-pass localhost:8080/api/stock/write-off-report
HTTP/1.1 200
["SKU-310 water damage: 12 units"]

$ curl -u rina:rina-pass localhost:8080/api/exports/inventory
HTTP/1.1 200
sku,units
SKU-881,148
SKU-310,40

$ curl -u kamal:kamal-pass localhost:8080/api/exports/inventory
HTTP/1.1 403
{"timestamp":"2026-11-18T12:14:57.448Z","status":403,"error":"Forbidden","path":"/api/exports/inventory"}

$ curl -u olu:olu-pass localhost:8080/api/audit/movements
HTTP/1.1 200
["SKU-881 +200 (receiving)","SKU-881 -52 (orders)"]

$ curl -u rina:rina-pass localhost:8080/api/audit/movements
HTTP/1.1 403
{"timestamp":"2026-11-18T12:14:57.581Z","status":403,"error":"Forbidden","path":"/api/audit/movements"}

$ curl -u rina:rina-pass localhost:8080/api/audit/summary
HTTP/1.1 200
November: 3 write-offs, 0 discrepancies

Kamal reads stock levels but cannot see write-offs or export. Rina, a manager with the export permission, can do both. Olu, the auditor, reads stock movements through the class-level rule, while Rina is refused there. The monthly summary has its own method-level rule that also admits managers, so Rina may read it.

7. Advantages and Limitations

AdvantagesLimitations
Simple, declarative role listsNo expressions, arguments, return values or bean calls
Constants give compile-time checking of namesNo "all of these" rule; only any-of
Works for roles and permission-style authoritiesFull authority names required; easy to forget ROLE_
Long-standing and widely understoodMust be enabled explicitly with securedEnabled

8. When to Choose @Secured

@Secured is a sound choice when every rule in a service is a plain list of roles or permissions and is likely to stay that way, for example an internal back-office tool with a handful of fixed roles. Keeping authority names in constants, as in the example, gives you compile-time checking that string expressions cannot offer.

Choose @PreAuthorize instead as soon as a rule depends on who owns the data, on method arguments or on more than one condition. In a new project, most teams standardise on @PreAuthorize from the start so that every rule is written the same way.

9. Migrating from @Secured to @PreAuthorize

Teams often start with @Secured and later need a condition that it cannot express. The translation is mechanical, so it can be done one method at a time. Remember that hasRole adds the prefix, so ROLE_ disappears from role names, while permission-style authorities move to hasAuthority or hasAnyAuthority unchanged:

@SecuredEquivalent @PreAuthorize
@Secured("ROLE_WAREHOUSE_MANAGER")@PreAuthorize("hasRole('WAREHOUSE_MANAGER')")
@Secured({"ROLE_STOCK_CLERK", "ROLE_WAREHOUSE_MANAGER"})@PreAuthorize("hasAnyRole('STOCK_CLERK', 'WAREHOUSE_MANAGER')")
@Secured("inventory:export")@PreAuthorize("hasAuthority('inventory:export')")
Not possible@PreAuthorize("hasRole('STOCK_CLERK') and #sku.startsWith('SKU-')")

After migrating the last @Secured method, remove securedEnabled = true so the unused interceptor is no longer registered. Run your security tests before and after; a forgotten prefix shows up immediately as a 403 in a test that expected 200.

10. Common Mistakes and Best Practices

  • Forgetting securedEnabled = true. The annotation compiles and is silently ignored.
  • Writing @Secured("ADMIN"). Use the full authority, ROLE_ADMIN.
  • Expecting class and method rules to combine. The method-level annotation replaces the class-level one.
  • Mixing annotation styles on one method. Spring Security rejects more than one method security annotation of the same kind on a method; keep one style per method.
  • Plan for growth. When a rule needs a condition, move it to @PreAuthorize("hasAnyRole('CLERK', 'MANAGER')") and continue from there. The @RolesAllowed page compares the standard Jakarta alternative.

11. Summary

  • @Secured restricts a method to users holding any of the listed authorities.
  • Enable it with @EnableMethodSecurity(securedEnabled = true).
  • It compares exact strings, so roles must be written with the ROLE_ prefix.
  • A method-level @Secured replaces the class-level one.
  • Use @PreAuthorize when you need expressions, arguments or bean calls.

12. References