SpringSecurity(三)——自定义优化器

在SpringSecurity中,如果我们在认证或者授权的过程中出现了异常会被ExceptionTranslationFilter捕 获到。在ExceptionTranslationFilter中会去判断是认证失败还是授权失败出现的异常。

一、自定义验证异常类

创建exception包,在exception包下创建自定义CustomerAuthenticationException类,继承 AuthenticationException类

java 复制代码
/**
 * 自定义 认证 验证异常类
 */
public class CustomerAuthenticationException extends AuthenticationException {
    public CustomerAuthenticationException(String message){
        super(message);
    }
}

二、登录用户访问无权限资源处理器

创建CustomerAccessDeniedHandler认证用户访问无权限资源时处理器类。

抓捕到AccessDeniedException异常后,进入此处理器

java 复制代码
/**
 * 认证用户  访问无权限资源时处理器
 */
@Component
public class CustomerAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request,
                       HttpServletResponse response,
                       AccessDeniedException accessDeniedException) throws IOException {
        //设置客户端的响应的内容类型
        response.setContentType("application/json;charset=UTF-8");
        //获取输出流
        ServletOutputStream outputStream = response.getOutputStream();
        //消除循环引用
        String result = JSON.toJSONString(R.error().code(700).message("无权限访问, 请联系管理员!"),
                SerializerFeature.DisableCircularReferenceDetect);

        outputStream.write(result.getBytes(StandardCharsets.UTF_8));
        outputStream.flush();
        outputStream.close();
    }
}

三、匿名用户访问资源处理器

java 复制代码
/**
 * 匿名用户  访问无权限资源的处理类
 */
@Component
public class AnonymousAuthenticationHandler implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request,
                         HttpServletResponse response,
                         AuthenticationException authException) throws IOException {
        //设置客户端的响应的内容类型
        response.setContentType("application/json;charset=UTF-8");
        String result = null;
        //获取输出流
        ServletOutputStream outputStream = response.getOutputStream();
        // System.out.println("异常消息:"+authException.getMessage()+",对象:"+authException);
        if (authException instanceof BadCredentialsException) {
            // 用户名未找到,可以在这里添加自定义处理逻辑
            result = JSON.toJSONString(R.error()
                            .code(HttpServletResponse.SC_UNAUTHORIZED)
                            .message(authException.getMessage()),
                    SerializerFeature.DisableCircularReferenceDetect);
        } else if (authException instanceof InternalAuthenticationServiceException) {
            result = JSON.toJSONString(R.error()
                            .code(HttpServletResponse.SC_UNAUTHORIZED)
                            .message("用户名为空!"),
                    SerializerFeature.DisableCircularReferenceDetect);
        } else {
            // 其他身份验证异常处理
            result = JSON.toJSONString(R.error().code(600).message("匿名用户无权限访问!"),
                    SerializerFeature.DisableCircularReferenceDetect);  //消除循环引用
        }
        outputStream.write(result.getBytes(StandardCharsets.UTF_8));
        outputStream.flush();
        outputStream.close();
    }
}

四、改造认证校验过滤器 && 认证失败处理器

java 复制代码
/**
 * 认证校验失败处理类
 */
@Component
public class LoginFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request,
                                        HttpServletResponse response,
                                        AuthenticationException exception) throws IOException, ServletException {
        //设置客户端响应编码格式
        response.setContentType("application/json;charset=UTF-8");
        //获取输出流
        ServletOutputStream outputStream= response.getOutputStream();
        String message = null;//提示信息
        int code = 500;//错误编码
        //判断异常类型
        if(exception instanceof AccountExpiredException){
            message = "账户过期,登录失败!";
        }else if(exception instanceof BadCredentialsException){
            message = "用户名或密码错误,登录失败!";
        }else if(exception instanceof CredentialsExpiredException){
            message = "密码过期,登录失败!";
        }else if(exception instanceof DisabledException){
            message = "账户被禁用,登录失败!";
        }else if(exception instanceof LockedException){
            message = "账户被锁,登录失败!";
        }else if(exception instanceof InternalAuthenticationServiceException){
            message = "账户不存在,登录失败!";
        }else if(exception instanceof CustomerAuthenticationException){
            message = exception.getMessage();
            code = 600;
        }else{
            message = "登录失败!";
        }
        //将错误信息转换成JSON
        String result = JSON.toJSONString(R.error().code(code).message(message));
        outputStream.write(result.getBytes(StandardCharsets.UTF_8));
        outputStream.flush();
        outputStream.close();
    }
}

五、配置自定义处理器

相关推荐
麦兜*1 小时前
Spring Boot 企业级动态权限全栈深度解决方案,设计思路,代码分析
java·spring boot·后端·spring·spring cloud·性能优化·springcloud
序属秋秋秋1 小时前
《C++初阶之内存管理》【内存分布 + operator new/delete + 定位new】
开发语言·c++·笔记·学习
ruan1145142 小时前
MySQL4种隔离级别
java·开发语言·mysql
quant_19863 小时前
R语言如何接入实时行情接口
开发语言·经验分享·笔记·python·websocket·金融·r语言
Hellyc6 小时前
基于模板设计模式开发优惠券推送功能以及对过期优惠卷进行定时清理
java·数据库·设计模式·rocketmq
lifallen6 小时前
Paimon LSM Tree Compaction 策略
java·大数据·数据结构·数据库·算法·lsm-tree
hdsoft_huge6 小时前
SpringBoot 与 JPA 整合全解析:架构优势、应用场景、集成指南与最佳实践
java·spring boot·架构
百锦再7 小时前
详细解析 .NET 依赖注入的三种生命周期模式
java·开发语言·.net·di·注入·模式·依赖
程序员的世界你不懂7 小时前
基于Java+Maven+Testng+Selenium+Log4j+Allure+Jenkins搭建一个WebUI自动化框架(2)对框架加入业务逻辑层
java·selenium·maven
风吹落叶花飘荡7 小时前
2025 Next.js项目提前编译并在服务器
服务器·开发语言·javascript