springboot spring security 接口403 自定义处理

需要解决的问题

在弹出页面中有调用接口,当当前登录用户没有该权限时,就会小弹窗报错,不过报错的提示是FORBIDDEN对用户不友好,考虑将FORBIDDEN改成:无权限,请联系管理员这个问题

需要实现的样式

在这里插入图片描述

查找过程

我先设想 弹窗报错应该是是前端框架EASYUI的事,所以就是后端调用接口时检查登录人员无接口权限触发报错。前端收到错误信息 弹出显示错误信息
在这里插入图片描述
除此之外看本地代码没啥思绪,嘿嘿百度大法好,搜索 spring boot 自定义处理403(实际用这个搜索的结果收获很小)然后找到类似再疯狂查看推荐的博客最后得到理清了处理这个问题的思绪。---------》
111在idp的resources->static->error文件夹下添加 403.html(这个文件是框架默认的自定义显示错误页面的。例如,该文件夹下没有自定义的403.html页面,出现403错误的页面会显示上图所示,如果该文件夹下有403.html,403.html就会替代掉上图。如下图)
在这里插入图片描述
222 上面111是说页面出现403时自定义处理,222来讲调用接口出现403(即弹出显示403权限不足提示)。需要实现403权限不足处理类AccessDeniedHandler

最后得到的处理方案—针对修改弹窗提示

1先写实现处理类【最开始使用的是msg方式,不会显示FORBIDDEN,可是也不显示权限不足,错误弹窗页面一片空白//然后改成message弹窗提示就对了】
下面这个是第2种写法亲测都可以成功
在这里插入图片描述

2修改配置【方框框起的是实现自动注入,圆框是配置】
在这里插入图片描述
3到此就运行成功,以下是代码【其实代码也是照抄别人的博客的,参考链接放在最后,可以看看】

/**
 * <p>
 * 自定义权限不足处理程序
 * </p>
 *
 * @author:lll
 * @date:Created in 2021/4/28 9:17
 */
//@Component
//public class MyAccessDeniedHandler implements AccessDeniedHandler {
//
//    @Override
//    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
//        httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN); httpServletResponse.setHeader("Content-Type","application/json;charset=utf-8") ;
//        PrintWriter out = httpServletResponse.getWriter();
//        out.write("{\"status\":\"error\",\"msg\":\"权限不足\"}");
        out.write("{\"status\":\"error\",\"message\":\"权限不足,请联系管理 员!\"}");
//        out.flush();
//        out.close();
//    }
//}

/**
 * @Auther: lixiupeng
 * @Date: 2018/11/21 15:54
 * @Description: 自定义权限不足处理
 */
//@Service
//public class MyAccessDeniedHandler implements AccessDeniedHandler {
//    private String errorPage;
//    @Override
//    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
//        //判断是否为ajax请求
//        if (httpServletRequest.getHeader("accept").indexOf("application/json") > -1
//                || (httpServletRequest.getHeader("X-Requested-With") != null && httpServletRequest.getHeader("X-Requested-With").equals(
//                "XMLHttpRequest"))) {
//            //设置状态为403,无权限状态
//            httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
//            //设置格式以及返回json数据 方便前台使用reponseJSON接取
//            httpServletResponse.setCharacterEncoding("UTF-8");
//            httpServletResponse.setContentType("application/json; charset=utf-8");
//            PrintWriter out = httpServletResponse.getWriter();
//            JSONObject json = new JSONObject();
//            json.put("message","权限不足,请联系管理员");
//            out.append(json.toString());
//        }else if(!httpServletResponse.isCommitted()){//非ajax请求
//            if(errorPage!=null){
//                // Put exception into request scope (perhaps of use to a view)
//                httpServletRequest.setAttribute(WebAttributes.ACCESS_DENIED_403, e);
//
//                // Set the 403 status code.
//                httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
//
//                // forward to error page.
//                RequestDispatcher dispatcher = httpServletRequest.getRequestDispatcher(errorPage);
//                dispatcher.forward(httpServletRequest, httpServletResponse);
//            }else{
//                httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN,e.getMessage());
//            }
//        }
//    }
    public void setErrorPage(String errorPage) {
        if ((errorPage != null) && !errorPage.startsWith("/")) {
            throw new IllegalArgumentException("errorPage must begin with '/'");
        }
        this.errorPage = errorPage;
    }
//}

参考的文章链接

https://blog.csdn.net/weixin_38373006/article/details/84339843

https://blog.csdn.net/qq_45017999/article/details/109023378