Search code examples
springspring-mvcspring-securityspring-security-acl

Protecting method calls in Spring multiactionController using methodNameResolver


I am using Spring 3 and implemented MVC using simpleUrlMapping. I am having CustomerController class. In CustomerController I am having three methods:

  1. View customer
  2. Add customer
  3. Delete customer

The above actions are getting called using method name resolver.

My requirement over here depending upon the logged in user and privilege I want to protect the corresponding method calls.

Delete customer method should be called by the privilege user and not by all the user.

I am using Spring Security as well. Is there any way to protect the delete customer method with Spring security?


Solution

  • options:

    @RequestMapping
     public void deleteCustomer(HttpServletRequest request) {
        if(request.isUserInRole("ROLE_ADMIN"){
          // do deletion
        };
    
     }
    

    or use @EnableGlobalMethodSecurity

     @PreAuthorize("hasRole('ROLE_ADMIN')")
     @RequestMapping
     public void deleteCustomer(HttpServletRequest request) {