Spring Security OAuth2 认证服务器自定义异常处理
创始人
2024-11-04 03:32:31
0

目录

    • 前言
    • WebResponseExceptionTranslator
    • 自定义异常处理
      • 1、自定义我们响应实体类
      • 2、定义响应结果枚举类
      • 3、自定义异常转换类
      • 4、配置自定义异常转换器
      • 5、测试

前言

Spring Security OAuth2 认证失败的格式如下

{     "error": "unsupported_grant_type",     "error_description": "Unsupported grant type: refresh_token1" } 

这个返回是很不友好的,特别是在前后端分离的时候,前端一般是根据我们的返回码进行处理,所以我们还得自定义我们的异常处理

WebResponseExceptionTranslator

在 AuthorizationServerEndpointsConfigurer端点配置类有一个 WebResponseExceptionTranslator异常转换器。

WebResponseExceptionTranslator只有一个translate方法,很明显,这个方法就是用来转换异常的

public interface WebResponseExceptionTranslator { 	 	ResponseEntity translate(Exception e) throws Exception;  } 

它就是专门用于处理异常转换的,我们要自定义异常很简单,创建一个类来实现WebResponseExceptionTranslator接口,然后进行配置即

自定义异常处理

1、自定义我们响应实体类

定义一个我们需要格式的响应实体类,这个实体类我们以json的格式返回

/**  * 统一的返回实体  */ @Data @NoArgsConstructor public class MyResponseResult  {      /**      * 响应码      */     private String code;      /**      * 响应结果消息      */     private String msg;      /**      * 响应数据      */     private T data;      protected MyResponseResult(String code, String msg, T data) {         this.code = code;         this.msg = msg;         this.data = data;     }      public static  MyResponseResult failed(String resultCode, String resultMsg) {         return new MyResponseResult(resultCode, resultMsg, null);     }      public static  MyResponseResult failed(String message) {         return new MyResponseResult(ResultCode.FAILED.getCode(), message, null);     }      public static  MyResponseResult failed(T data) {         return new MyResponseResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), data);     }  } 

2、定义响应结果枚举类

/**  * 响应结果枚举  */ @Getter @AllArgsConstructor public enum ResultCode {      SUCCESS("200", "操作成功"),     FAILED("500", "操作失败"),     AUTH_FAIL("10001", "认证失败"),     INVALID_TOKEN("10002", "token无效"),     NONSUPPORT_GRANT_TYPE("10003", "授权类型不支持"),     ;      private final String code;      private final String msg;  } 

3、自定义异常转换类

/**  * 自定义异常转换  */ @Slf4j public class MyExtendOAuth2ResponseExceptionTranslator implements WebResponseExceptionTranslator {      public static final int FAIL_CODE = 500;      @Override     public ResponseEntity translate(Exception e) throws Exception {         log.error("认证服务器认证异常:{}", e.getMessage());         //对异常进行转换         if (e instanceof UnsupportedGrantTypeException){             return ResponseEntity.status(FAIL_CODE).contentType(MediaType.APPLICATION_JSON)                     .body(MyResponseResult.failed(ResultCode.NONSUPPORT_GRANT_TYPE.getCode(), ResultCode.NONSUPPORT_GRANT_TYPE.getMsg()));         }         if (e instanceof InvalidTokenException) {             return ResponseEntity.status(FAIL_CODE).contentType(MediaType.APPLICATION_JSON)                     .body(MyResponseResult.failed(ResultCode.INVALID_TOKEN.getCode(),ResultCode.INVALID_TOKEN.getMsg()));         }         return ResponseEntity.status(FAIL_CODE).contentType(MediaType.APPLICATION_JSON)                 .body(MyResponseResult.failed(ResultCode.AUTH_FAIL.getCode(),ResultCode.AUTH_FAIL.getMsg()));     }  } 

4、配置自定义异常转换器

在AuthorizationServerConfig配置文件的configure(AuthorizationServerEndpointsConfigurer endpoints)方法加下下面这行配置即可

//指定异常转换器         endpoints.exceptionTranslator(new MyExtendOAuth2ResponseExceptionTranslator()); 

5、测试

使用一个不存在的grant_type请求/oauth/token,已经返回我们自定义的异常响应了

{     "code": "10003",     "msg": "授权类型不支持",     "data": null } 

相关内容

热门资讯

技巧辅助挂!pokermast... 技巧辅助挂!pokermaster修改器,丹东约战麻将辅助器,演示教程(有挂细节)1、点击下载安装,...
现场直击!wepokerplu... 现场直击!wepokerplus万能挂,丰城双剑新版最强高分攻略,操作教程(有挂方针)1.丰城双剑新...
插件辅助挂!wepoker有辅... 插件辅助挂!wepoker有辅助器吗,乐平包王攻略,学习教程(有挂方略)1、首先打开乐平包王攻略辅助...
据玩家消息!拱趴大菠萝辅助神器... 据玩家消息!拱趴大菠萝辅助神器,多乐跑得快辅助器,机巧教程(证实有挂)1、在拱趴大菠萝辅助神器插件功...
此事备受玩家关注!来玩app破... 此事备受玩家关注!来玩app破解版,h5能反杀吗,绝活教程(有挂详细)1、打开软件启动之后找到中间准...
值得注意的是!aapoker破... 值得注意的是!aapoker破解侠是真的吗,蜀山四川游戏修改工具,经验教程(有挂助手)1、金币登录送...
第三方辅助!wepoker脚本... 第三方辅助!wepoker脚本,广东星悦有外开挂辅助器吗,法门教程(有挂分析)广东星悦有外开挂辅助器...
此事引发广泛关注!德州透视脚本... 此事引发广泛关注!德州透视脚本,崇阳斗棋辅助脚本视频,诀窍教程(的确有挂)暗藏猫腻,小编详细说明崇阳...
黑科技辅助挂!wepoker买... 黑科技辅助挂!wepoker买脚本靠谱吗,情怀七喜游戏辅助,法门教程(有挂方法)1、每一步都需要思考...
方法辅助挂!aapoker怎么... 方法辅助挂!aapoker怎么设置提高好牌几率,蘑菇云辅助使用视频,绝活儿教程(讲解有挂)1、完成蘑...