How do I return a 403 Forbidden in Spring MVC?

后端 未结 5 1015
太阳男子
太阳男子 2020-12-29 03:14

I want my controller to return the right HTTP response code when the user lacks permission to view a particular page.

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-29 03:50

    Create an Exception annotated with @ResponseStatus e.g. like this:

    @ResponseStatus(HttpStatus.FORBIDDEN)
    public class ForbiddenException extends RuntimeException {
    }
    

    Now just throw that Exception in your handler method and the response will have status 403.

提交回复
热议问题