Should spring security method level annotations be applied at the controller layer or the service layer?

“It depends” :). If your application has a service layer through which all your business logic is applied then that is usually a clean place to apply your security constraints and be certain that you haven’t missed out any corner cases.

Web code is generally messier, there’s more of it, it changes more rapidly and you may end up calling the same service methods from multiple places. Someone might add a new controller and forget to secure it properly. Alternatively you might have different types of clients calling the same services.

But it depends on how your application is structured and what your use cases are. You may have a good argument for why you want to secure a controller.

Leave a Comment