• SpringSecurity自定义响应异常信息


    SpringSecurity自定义响应异常信息

    此处的异常信息设置的话,其中还是有坑的,比如你想自定义token过期信息,无效token这些,如果按照SpringSecurity的设置是不会生效的,需要加到资源的配置中。

    如果只是SpringSecurity的话,只需要实现AccessDeniedHandler和AuthenticationEntryPoint这2个接口就可以了。他们都是在ExceptionTranslationFilter中生效的。

    AuthenticationEntryPoint 用来解决匿名用户访问无权限资源时的异常

    ruAccessDeineHandler 用来解决认证过的用户访问无权限资源时的异常

    image-20210823192313538

    如果你想自定义token过期的话,需要实现AuthenticationEntryPoint这个接口,因为token过期了,访问的话也算是匿名访问。但是SpringSecurity的过滤器链中其实是有顺序的,校验token的OAuth2AuthenticationProcessingFilter在它前面,导致一直没有办法生效,所有需要添加到资源的配置上,demo如下:

    /**
     * @author WGR
     * @create 2021/8/23 -- 16:52
     */
    @Component
    public class SimpleAuthenticationEntryPoint implements AuthenticationEntryPoint {
    
        @Override
        public void commence(HttpServletRequest request, HttpServletResponse response,
                             AuthenticationException authException) throws ServletException {
            Throwable cause = authException.getCause();
            try {
                if (cause instanceof InvalidTokenException) {
                    Map map = new HashMap();
                    map.put("error", "无效token");
                    map.put("message", authException.getMessage());
                    map.put("path", request.getServletPath());
                    map.put("timestamp", String.valueOf(new Date().getTime()));
                    response.setContentType("application/json");
                    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
                    try {
                        ObjectMapper mapper = new ObjectMapper();
                        mapper.writeValue(response.getOutputStream(), map);
                    } catch (Exception e) {
                        throw new ServletException();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    image-20210823192757483

    则可以生效,返回信息具体如下:image-20210823192828621

    如果想设置没有权限的自定义异常信息的话:

    /**
     * @author WGR
     * @create 2021/8/23 -- 17:09
     */
    @Component
    public class SimpleAccessDeniedHandler implements AccessDeniedHandler {
        @Override
        public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
            Map map = new HashMap();
            map.put("message", "无权操作");
            map.put("path", request.getServletPath());
            map.put("timestamp", String.valueOf(new Date().getTime()));
            response.setContentType("application/json");
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            try {
                ObjectMapper mapper = new ObjectMapper();
                mapper.writeValue(response.getOutputStream(), map);
            } catch (Exception e) {
                throw new ServletException();
            }
        }
    }
    

    把它设置到springsecurity中,添加进去就可以了,如果不是想要捕获token过期的话,就直接添加进去也可以

    image-20210823194105642

    image-20210823193825241

  • 相关阅读:
    什么是布局?Android中的布局是怎样的?
    如何优化UI布局?
    Android SDK提供的常用控件Widget “常用控件”“Android原生”
    Android中Adapter类的使用 “Adapter”
    Android自定义属性
    Android中View的绘制流程(专题讲解)
    Android的自定义View及View的绘制流程
    如何创建新控件? “复合控件”“定制控件”
    Android应用程序支持不同屏幕(尺寸、密度)
    支持不同Android设备,包括:不同尺寸屏幕、不同屏幕密度、不同系统设置
  • 原文地址:https://www.cnblogs.com/dalianpai/p/15177497.html
Copyright © 2020-2023  润新知