08、SpringBoot集成SpringSecurity实战:无权限响应Json

前言

配置权限章节中,我们看到当用户没有权限时,浏览器中显示下面代码,这显然对用户来说不友好,因为根本看不懂这些。接下来,我们通过json的形式,响应一个友好的提示。

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun May 16 18:38:30 CST 2021
There was an unexpected error (type=Forbidden, status=403).

实现

创建NoPermissionHandler类,实现AccessDeniedHandler接口

@Component
public class NoPermissionHandler implements AccessDeniedHandler {
   
   
     
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
   
   
     
        Result error = Result.error(ResultCode

版权声明:本文不是「本站」原创文章,版权归原作者所有 | 原文地址: