zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    windows10使用arcgis注意事项
    andriod arcgis加载影像TIF
    FeatureTable()
    arcgis Listview
    ArcGIS for Android图层记录数,图层选择记录,图层字段数
    ArcGIS for Android 点击选择
    ArcGIS for Android地图上实际距离与对应的屏幕像素值计算
    Android如何检查对象的类型
    北美内地电影票房总排行榜
    XSS漏洞的渗透利用另类玩法
  • 原文地址:https://www.cnblogs.com/dalianpai/p/15177497.html
Copyright © 2011-2022 走看看