• Spring Security(八) —— 异常处理


    一:异常体系

    Spring Security中异常主要分为两大类:

    • AuthenticationException:认证异常
    • AccessDeniedException:授权异常

    其中认证所涉及的异常类型比较多,默认有18种,如下:
    在这里插入图片描述
    相比于认证异常,权限异常类就要少了很多,默认提供的权限异常如下:
    在这里插入图片描述

    二:自定义异常配置

    在实际开发中,默认异常是无法满足需求的,需要大量的自定义异常类,Spring Security提供了两个方法来配置:

    • authenticationEntryPoint():认证自定义异常
    • accessDeniedHandler():授权自定义异常

    至于这两个方法怎么使用,我们可以进入源码看看

    首先是authenticationEntryPoint(),可以看到,该方法要求我们传入AuthenticationEntryPoint的对象

    	public ExceptionHandlingConfigurer<H> authenticationEntryPoint(
    			AuthenticationEntryPoint authenticationEntryPoint) {
    		this.authenticationEntryPoint = authenticationEntryPoint;
    		return this;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5

    进入AuthenticationEntryPoint接口,可以看到只有一个方法,因此在authenticationEntryPoint方法中可以直接传入Lambda表达式,但那样写不美观,配置与实现应该分离,因此我们可以新建一个类去实现这个接口

    public interface AuthenticationEntryPoint {
    	/**
    	 * 处理认证访问相关异常
    	 * @param request 导致AuthenticationException的请求
    	 * @param response 响应
    	 * @param authException 异常信息
    	 */
    	void commence(HttpServletRequest request, HttpServletResponse response,
    			AuthenticationException authException) throws IOException, ServletException;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
        @Override
        public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 400);
            result.put("msg", authException.getMessage());
            response.setContentType("application/json;charset=UTF-8");
            String s = new ObjectMapper().writeValueAsString(result);
            response.getWriter().println(s);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    accessDeniedHandler()方法同理,其源码如下:

    	public ExceptionHandlingConfigurer<H> accessDeniedHandler(
    			AccessDeniedHandler accessDeniedHandler) {
    		this.accessDeniedHandler = accessDeniedHandler;
    		return this;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5

    进入AccessDeniedHandler接口:

    public interface AccessDeniedHandler {
    	/**
    	 * 处理权限访问相关异常
    	 * @param request 导致AccessDeniedException的请求
    	 * @param response 响应
    	 * @param accessDeniedException 异常信息
    	 */
    	void handle(HttpServletRequest request, HttpServletResponse response,
    			AccessDeniedException accessDeniedException) throws IOException,
    			ServletException;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    因此我们可以这样实现:

    public class CustomAccessDeniedHandler implements AccessDeniedHandler {
        @Override
        public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 400);
            result.put("msg", accessDeniedException.getMessage());
            response.setContentType("application/json;charset=UTF-8");
            String s = new ObjectMapper().writeValueAsString(result);
            response.getWriter().println(s);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    最后再在configure中将自定义处理配置进去:

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                    .mvcMatchers("/hello1").permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .formLogin()
                    .successHandler(new CustomAuthenticationSuccessHandler())
                    .failureHandler(new CustomAuthenticationFailureHandler())
                    .and()
                    .logout()
                    .logoutSuccessHandler(new CustomLogoutSuccessHandler())
                    // 异常处理
                    .and()
                    .exceptionHandling()
                    .authenticationEntryPoint(new CustomAuthenticationEntryPoint())  // 认证异常
                    .accessDeniedHandler(new CustomAccessDeniedHandler())  // 授权异常;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    如果有兴趣了解更多相关内容,欢迎来我的个人网站看看:瞳孔的个人网站

  • 相关阅读:
    ​力扣解法汇总1413-逐步求和得到正数的最小值
    Aspect Android埋点统计activity页面使用时长 onResume onPause,并保存时长
    APP瀑布流分页问题——领取,使用等操作即消失问题
    计算机毕业设计(附源码)python智慧校园系统
    Vue 父子组件传值有哪些方式
    算法leetcode|剑指 Offer 27. 二叉树的镜像|226. 翻转二叉树(rust很强)
    人和AI不可能把所有的情况都考虑到
    pdf文件如何添加水印?
    Jmeter 之 “查看结果树” 界面功能介绍
    如何用java 5分钟实现一个最简单的mysql代理服务器?
  • 原文地址:https://blog.csdn.net/tongkongyu/article/details/125990980