Spring Cloud实战 | 第九篇:Spring Cloud整合Spring Security OAuth2认证服务器统一认证自定义异常处理

简介: Spring Cloud实战 | 第九篇:Spring Cloud整合Spring Security OAuth2认证服务器统一认证自定义异常处理

本文完整代码下载点击

微信图片_20230709224442.gif

一. 前言

相信了解过我或者看过我之前的系列文章应该多少知道点我写这些文章包括创建 有来商城youlai-mall 这个项目的目的,想给那些真的想提升自己或者迷茫的人(包括自己–一个工作6年觉得一无是处的菜鸟)提供一块上升的基石。项目是真的从无到有(往期文章佐证),且使用当前主流的开发模式(微服务+前后端分离),最新主流的技术栈(Spring Boot+ Spring Cloud +Spring Cloud Alibaba + Vue),最流行的统一安全认证授权(OAuth2+JWT),好了玩笑开完了大家别当真,总之有兴趣一起的小伙伴欢迎加入~


接下来说下这篇文章的原因,之前我是没想过应用到项目中的OAuth2+JWT这套组合拳这么受大家关注,期间一直有童鞋问怎么自定义Spring Security OAuth2的异常处理、JWT怎么续期、JWT退出等场景下如何失效等问题,所以最近有点时间想把这套统一认证授权完善掉,本篇就以如何自定义Spring Security OAuth2异常处理展开。


往期文章链接:


后端


Spring Cloud实战 | 第一篇:Windows搭建Nacos服务

Spring Cloud实战 | 第二篇:Spring Cloud整合Nacos实现注册中心

Spring Cloud实战 | 第三篇:Spring Cloud整合Nacos实现配置中心

Spring Cloud实战 | 第四篇:Spring Cloud整合Gateway实现API网关

Spring Cloud实战 | 第五篇:Spring Cloud整合OpenFeign实现微服务之间的调用

Spring Cloud实战 | 第六篇:Spring Cloud Gateway+Spring Security OAuth2+JWT实现微服务统一认证授权

Spring Cloud实战 | 最七篇:Spring Cloud Gateway+Spring Security OAuth2集成统一认证授权平台下实现注销使JWT失效方案

Spring Cloud实战 | 最八篇:Spring Cloud +Spring Security OAuth2+ Vue前后端分离模式下无感知刷新实现JWT续期

Spring Cloud实战 | 最九篇:Spring Security OAuth2认证服务器统一认证自定义异常处理

管理前端


vue-element-admin实战 | 第一篇: 移除mock接入后台,搭建有来商城youlai-mall前后端分离管理平台

vue-element-admin实战 | 第二篇: 最小改动接入后台实现根据权限动态加载菜单

微信小程序


vue+uniapp商城实战 | 第一篇:【有来小店】微信小程序快速开发接入Spring Cloud OAuth2认证中心完成授权登录

二. 自定义异常实现代码

直接需要答案的本节走起,添加和修改三个文件即可,异常分析,点击下载完整工程代码


1. 在youlai-auth认证服务器模块添加全局异常处理器AuthExceptionHandler


package com.youlai.auth.exception;


import com.youlai.common.core.result.Result;

import com.youlai.common.core.result.ResultCode;

import lombok.extern.slf4j.Slf4j;

import org.springframework.security.authentication.InternalAuthenticationServiceException;

import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;

import org.springframework.web.bind.annotation.ExceptionHandler;

import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice

@Slf4j

public class AuthExceptionHandler {

   /**

    * 用户名和密码错误

    *

    * @param e

    * @return

    */

   @ExceptionHandler(InvalidGrantException.class)

   public Result handleInvalidGrantException(InvalidGrantException e) {

       return Result.custom(ResultCode.USERNAME_OR_PASSWORD_ERROR);

   }

   /**

    * 账户异常(禁用、锁定、过期)

    *

    * @param e

    * @return

    */

   @ExceptionHandler({InternalAuthenticationServiceException.class})

   public Result handleInternalAuthenticationServiceException(InternalAuthenticationServiceException e) {

       return Result.error(e.getMessage());

   }

}


2. 重写ClientCredentialsTokenEndpointFilter实现客户端自定义异常处理


package com.youlai.auth.filter;

import org.springframework.security.authentication.AuthenticationManager;

import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;

import org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter;

import org.springframework.security.web.AuthenticationEntryPoint;

/**

* 重写filter实现客户端自定义异常处理

*/

public class CustomClientCredentialsTokenEndpointFilter extends ClientCredentialsTokenEndpointFilter {

   private AuthorizationServerSecurityConfigurer configurer;

   private AuthenticationEntryPoint authenticationEntryPoint;

   public CustomClientCredentialsTokenEndpointFilter(AuthorizationServerSecurityConfigurer configurer) {

       this.configurer = configurer;

   }

   @Override

   public void setAuthenticationEntryPoint(AuthenticationEntryPoint authenticationEntryPoint) {

       super.setAuthenticationEntryPoint(null);

       this.authenticationEntryPoint = authenticationEntryPoint;

   }

   @Override

   protected AuthenticationManager getAuthenticationManager() {

       return configurer.and().getSharedObject(AuthenticationManager.class);

   }

   @Override

   public void afterPropertiesSet() {

       setAuthenticationFailureHandler((request, response, e) -> authenticationEntryPoint.commence(request, response, e));

       setAuthenticationSuccessHandler((request, response, authentication) -> {

       });

   }

}



3. AuthorizationServerConfig认证服务器配置修改


/**

* 授权服务配置

*/

@Configuration

@EnableAuthorizationServer

@AllArgsConstructor

public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {


   ......


   @Override

   public void configure(AuthorizationServerSecurityConfigurer security) {

       /*security.allowFormAuthenticationForClients();*/

       CustomClientCredentialsTokenEndpointFilter endpointFilter = new CustomClientCredentialsTokenEndpointFilter(security);

       endpointFilter.afterPropertiesSet();

       endpointFilter.setAuthenticationEntryPoint(authenticationEntryPoint());

       security.addTokenEndpointAuthenticationFilter(endpointFilter);

       security.authenticationEntryPoint(authenticationEntryPoint())

               .tokenKeyAccess("isAuthenticated()")

               .checkTokenAccess("permitAll()");

   }

   @Bean

   public AuthenticationEntryPoint authenticationEntryPoint() {

       return (request, response, e) -> {

           response.setStatus(HttpStatus.HTTP_OK);

           response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE);

           response.setHeader("Access-Control-Allow-Origin", "*");

           response.setHeader("Cache-Control", "no-cache");

           Result result = Result.custom(ResultCode.CLIENT_AUTHENTICATION_FAILED);

           response.getWriter().print(JSONUtil.toJsonStr(result));

           response.getWriter().flush();

       };

   }

   ......

}


三. 异常处理分析

其实你搜一下有关Spring Security OAuth2如何自定义异常处理,网上会很多差不多解决方案提供参考,但是照搬过来试用,一点效果没有?!咋回事么?其实不能武断的说人家方案不行,最多的可能是Spring Security OAuth2版本不一致,本篇项目使用的是2.3.4版本,目前截止写这篇文章最新一版的是2020.5.28发布的2.5.0版本,后续项目会升级,如果有差异我会修改本篇文章,总之给大家提供一个解决思路,可行不可行我是不希望大家不能在我里浪费时间。


好了正文开始了~ Spring Security OAuth2认证服务器异常目前我知道的有3类:


用户名或密码错误

账户状态异常

客户端认证异常

有知道其他的欢迎留言补充~,以下就这3类异常逐一分析


在异常处理之前先看下UserDetailsServiceImpl#loadUserByUsername方法抛出的异常信息,如下图:


微信图片_20230709224513.png


1. 用户名或密码错误

异常分析

org.springframework.security.oauth2.common.exceptions.InvalidGrantException: 用户名或密码错误

1微信图片_20230709224524.png



通过异常堆栈信息定位到最终抛出异常的方法是ResourceOwnerPasswordTokenGranter#getOAuth2Authentication,异常类型是InvalidGrantException,其实到这个异常类型中间经过几道转换UsernameNotFoundException->BadCredentialsException->InvalidGrantException


处理方法

添加全局异常处理器捕获(定位标识:AuthExceptionHandler)



/**

* 用户名和密码异常

*

* @param e

* @return

*/

@ExceptionHandler(InvalidGrantException.class)

public Result handleInvalidGrantException(InvalidGrantException e) {

   return Result.error(ResultCode.USERNAME_OR_PASSWORD_ERROR);

}


结果验证

验证成功,已按照自定义异常格式返回

微信图片_20230709224527.png



2. 账户状态异常

异常分析

首先我们需要把数据库youlai的表sys_user的字段status设置为0,表示不可用状态,然后输入正确的用户名和密码,看看跑出来的原生异常信息,可惜的是这个异常没有打印堆栈信息,不过没关系,我们断点调试下,最终定位到ProviderManager#authenticate方法抛出的异常,异常类型是InternalAuthenticationServiceException。

微信图片_20230709224546.png



处理方法

添加全局异常处理器捕获(定位标识:AuthExceptionHandler)


/**

* 账户异常(禁用、锁定、过期)

*

* @param e

* @return

*/

@ExceptionHandler({InternalAuthenticationServiceException.class})

public Result handleInternalAuthenticationServiceException(InternalAuthenticationServiceException e) {

   return Result.error(e.getMessage());

}


结果验证

验证成功,已按照自定义异常格式返回

微信图片_20230709224550.png



3. 客户端认证异常

异常分析

之前两种异常方式都可以通过全局异常处理器捕获,且@RestControllerAdvice只能捕获Controller的异常。


客户端认证的异常则是发生在过滤器filter上,此时还没进入DispatcherServlet请求处理流程,便无法通过全局异常处理器捕获。


先看下客户端认证异常出现的位置,首先把客户端ID改成错的。


微信图片_20230709224610.png


然后执行“登录”操作,返回错误信息如下:


{"error":"invalid_client","error_description":"Bad client credentials"}

1

一眼望去,这显然不是我们想要的格式。


那怎么做才能捕获这个异常转换成自定义数据格式返回呢?显然全局异常处理器无法实现,那必须转换下思路了。


首先客户端的认证是交由ClientCredentialsTokenEndpointFilter来完成的,其中有后置添加失败处理方法,最后把异常交给OAuth2AuthenticationEntryPoint这个所谓认证入口处理。


微信图片_20230709224647.png


认证入口OAuth2AuthenticationEntryPoint#commence方法中转给父类AbstractOAuth2SecurityExceptionHandler#doHandle方法。


微信图片_20230709224650.png


最后异常定格在AbstractOAuth2SecurityExceptionHandler#doHandle方法上,如下图:

微信图片_20230709224708.png微信图片_20230709224710.png



其中this.enhanceResponse是调用OAuth2AuthenticationEntryPoint#enhanceResponse方法得到响应结果数据。


处理方法

上面我们得知客户端的认证失败异常是过滤器ClientCredentialsTokenEndpointFilter转交给OAuth2AuthenticationEntryPoint得到响应结果的,既然这样我们就可以重写ClientCredentialsTokenEndpointFilter然后使用自定义的AuthenticationEntryPoint替换原生的OAuth2AuthenticationEntryPoint,在自定义AuthenticationEntryPoint处理得到我们想要的异常数据。


自定义AuthenticationEntryPoint设置异常响应数据格式


微信图片_20230709224725.png


重写ClientCredentialsTokenEndpointFilter替换AuthenticationEntryPoint

微信图片_20230709224728.png



认证服务器配置添加自定义过滤器

微信图片_20230709224743.png



结果验证

验证成功,已按照自定义异常格式返回


微信图片_20230709224745.png


四. 总结

至此,认证服务器的自定义异常处理已全部处理完毕,资源服务器异常处理说明在这篇文章 Spring Cloud实战 | 第六篇:Spring Cloud Gateway+Spring Security OAuth2+JWT实现微服务统一认证授权,这就宣告 youlai-mall 的统一认证授权模块基本达到完善的一个标准, 后面继续回到业务功能的开发,所以觉得对你有帮助的给个关注(持续更新)或者给个star,灰常感谢! 最重要的如果你真的对这个项目有兴趣想一起开发学习的像文章开始说的那样请联系我哈~


相关文章
|
8月前
|
监控 Java API
Spring Boot 3.2 结合 Spring Cloud 微服务架构实操指南 现代分布式应用系统构建实战教程
Spring Boot 3.2 + Spring Cloud 2023.0 微服务架构实践摘要 本文基于Spring Boot 3.2.5和Spring Cloud 2023.0.1最新稳定版本,演示现代微服务架构的构建过程。主要内容包括: 技术栈选择:采用Spring Cloud Netflix Eureka 4.1.0作为服务注册中心,Resilience4j 2.1.0替代Hystrix实现熔断机制,配合OpenFeign和Gateway等组件。 核心实操步骤: 搭建Eureka注册中心服务 构建商品
1208 3
|
6月前
|
负载均衡 Java API
《深入理解Spring》Spring Cloud 构建分布式系统的微服务全家桶
Spring Cloud为微服务架构提供一站式解决方案,涵盖服务注册、配置管理、负载均衡、熔断限流等核心功能,助力开发者构建高可用、易扩展的分布式系统,并持续向云原生演进。
|
9月前
|
Java Linux 网络安全
Linux云端服务器上部署Spring Boot应用的教程。
此流程涉及Linux命令行操作、系统服务管理及网络安全知识,需要管理员权限以进行配置和服务管理。务必在一个测试环境中验证所有步骤,确保一切配置正确无误后,再将应用部署到生产环境中。也可以使用如Ansible、Chef等配置管理工具来自动化部署过程,提升效率和可靠性。
827 13
|
负载均衡 Dubbo Java
Spring Cloud Alibaba与Spring Cloud区别和联系?
Spring Cloud Alibaba与Spring Cloud区别和联系?
|
9月前
|
Java Spring 容器
SpringBoot自动配置的原理是什么?
Spring Boot自动配置核心在于@EnableAutoConfiguration注解,它通过@Import导入配置选择器,加载META-INF/spring.factories中定义的自动配置类。这些类根据@Conditional系列注解判断是否生效。但Spring Boot 3.0后已弃用spring.factories,改用新格式的.imports文件进行配置。
1271 0
|
10月前
|
人工智能 Java 测试技术
Spring Boot 集成 JUnit 单元测试
本文介绍了在Spring Boot中使用JUnit 5进行单元测试的常用方法与技巧,包括添加依赖、编写测试类、使用@SpringBootTest参数、自动装配测试模块(如JSON、MVC、WebFlux、JDBC等),以及@MockBean和@SpyBean的应用。内容实用,适合Java开发者参考学习。
1071 0
|
6月前
|
JavaScript Java Maven
【SpringBoot(二)】带你认识Yaml配置文件类型、SpringMVC的资源访问路径 和 静态资源配置的原理!
SpringBoot专栏第二章,从本章开始正式进入SpringBoot的WEB阶段开发,本章先带你认识yaml配置文件和资源的路径配置原理,以方便在后面的文章中打下基础
535 4
|
6月前
|
Java 测试技术 数据库连接
【SpringBoot(四)】还不懂文件上传?JUnit使用?本文带你了解SpringBoot的文件上传、异常处理、组件注入等知识!并且带你领悟JUnit单元测试的使用!
Spring专栏第四章,本文带你上手 SpringBoot 的文件上传、异常处理、组件注入等功能 并且为你演示Junit5的基础上手体验
1053 3
|
前端开发 Java 数据库
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——Thymeleaf 介绍
本课介绍Spring Boot集成Thymeleaf模板引擎。Thymeleaf是一款现代服务器端Java模板引擎,支持Web和独立环境,可实现自然模板开发,便于团队协作。与传统JSP不同,Thymeleaf模板可以直接在浏览器中打开,方便前端人员查看静态原型。通过在HTML标签中添加扩展属性(如`th:text`),Thymeleaf能够在服务运行时动态替换内容,展示数据库中的数据,同时兼容静态页面展示,为开发带来灵活性和便利性。
504 0

热门文章

最新文章