Skip to content
Ameen Abozaid
Planned lessonJNR-040-L02

Method security

  • Lesson 2 of 6
  • Junior
On This Lesson 4 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

protect service methods with annotations.

Planned topics

  • Turn it on with @EnableMethodSecurity.
  • @PreAuthorize("hasRole('ADMIN')") runs before the method.
  • SpEL can use method arguments and the current user: @PreAuthorize("#id == authentication.principal.id").
  • Put it on the service layer so every caller, not only controllers, is covered.
  • URL rules are a first layer; method rules are a second layer (defense in depth).
Draft outline details

Demonstration

java
@PreAuthorize("hasRole('ADMIN')")
public void deleteProduct(long id) { ... }

Check your understanding

Which annotation enables @PreAuthorize?

Show answer

@EnableMethodSecurity.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.