We use spring and spring-security-3.2. We recently add @PreAuthorize annotations to RestAPI (previously this was URL based).
@PreAuthorize("hasPermission('salesorder','ViewSalesOrder')") @RequestMapping(value = "/restapi/salesorders/", method = RequestMethod.GET) public ModelAndView getSalesOrders(){}
We already have a global exception handler that annotates with - @ControllerAdvice and a custom PermissionEvaluator, everything works fine except for the error message.
Let's say some user accesses the API. At the moment, without the permission of "ViewSalesOrder", then spring by default throws an exception "Access is denied", but did not report which permission is missing (its requirement to indicate which permission is missing).
Is it possible to throw an exception that also includes the permission name, so the final error message should look like this: "Access is denied, you need ViewSalesOrder permission" (here the permission name should be from the @PreAuthorize annotation)?
Please note that we have 100 such restAPIs, so the overall solution will be highly appreciated.
Harsh source share