[12] ExceptionTranslationFilter

ExceptionTranslationFilter是Spring Security用于处理授权认证异常的关键组件。它在过滤器链中捕获并处理特定的Spring Security异常,如AuthenticationException和AccessDeniedException。用户可以自定义accessDeniedHandler和authenticationEntryPoint来定制异常处理。文章详细分析了代码执行步骤,尤其是在资源认证服务配置中如何注入自定义异常处理器。
摘要由CSDN通过智能技术生成

ExceptionTranslationFilter

介绍

Spring Security授权认证服务的异常处理不能像常规Spring MVC或者Spring Boot那样进行统一异常处理,而是在过滤器上进行了层层拦截,代码阅读起来也有些费劲。而该Filter是用于处理Spring Security部分异常的,开发者可以自定义accessDeniedHandler和authenticationEntryPoint进行配置,根据自己的需求进行异常处理。

代码分析

步骤1

上篇文章已经提及过Spring Security认证服务配置分ResourceServerConfigurerAdapter(资源认证服务配置)和AuthorizationServerSecurityConfigurer(授权认证服务配置),对于授权认证服务,作者还没找到办法自定义注入accessDeniedHandler和authenticationEntryPoint,我们可以看一下WebSecurityConfigurerAdapter的部分代码:

protected final HttpSecurity getHttp() throws Exception {
   
    if (http != null) {
   
        return http;
    }

    DefaultAuthenticationEventPublisher eventPublisher = objectPostProcessor
            .postProcess(new DefaultAuthenticationEventPublisher());
    localConfigureAuthenticationBldr.authenticationEventPublisher(eventPublisher);

    AuthenticationManager authenticationManager = authenticationManager();
    authenticationBuilder.parentAuthenticationManager(authenticationManager);
    authenticationBuilder.authenticationEventPublisher(eventPublisher);
    Map<Class<?>, Object> sharedObjects = createSharedObjects();

    http = new HttpSecurity(objectPostProcessor, authenticationBuilder,
            sharedObjects);
    if (!disableDefaults) {
   
        // @formatter:off
        http
            .csrf().and()
            .addFilter(new WebAsyncManagerIntegrationFilter())
            //添加一个ExceptionHandlingConfigurer,configurer是new出来的,未注入accessDeniedHandler和authenticationEntryPoint
            .exceptionHandling().and()
            .headers().and()
            .sessionManagement().and()
            .securityContext().and()
            .requestCache().and()
            .anonymous().and()
            .servletApi().and()
            .apply(new DefaultLoginPageConfigurer<>())
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值