Spring Cloud:Security OAuth2 自定义异常响应
对于客户端开发或者网站开发而言,调用接口返回有统一的响应体,可以针对性的设计界面,代码结构更加清晰,层次也更加分明。
默认异常响应
在使用 Spring Security Oauth2 登录和鉴权失败时,默认返回的异常信息如下:
{"error": "unauthorized","error_description": "Full authentication is required to access this resource" }
这与我们返回的信息格式不一致。如果需要修改这种返回的格式,需要重写相关异常处理类。这里我统一的是资源服务器(网关)的响应格式。
自定义异常响应
无效 token 异常类重写
新增 AuthExceptionEntryPoint.java
@Component public class AuthExceptionEntryPoint implements AuthenticationEntryPoint {@Overridepublic void commence(HttpServletRequest request, HttpServletResponse response,AuthenticationException authException) throws ServletException {Map<String, Object> map = new HashMap<String, Object>();Throwable cause = authException.getCause();response.setStatus(HttpStatus.OK.value());response.setHeader("Content-Type", "application/json;charset=UTF-8");try {if(cause instanceof InvalidTokenException) {response.getWriter().write(ResultJsonUtil.build(ResponseCodeConstant.REQUEST_FAILED,ResponseStatusCodeConstant.OAUTH_TOKEN_FAILURE,ResponseMessageConstant.OAUTH_TOKEN_ILLEGAL));}else{response.getWriter().write(ResultJsonUtil.build(ResponseCodeConstant.REQUEST_FAILED,ResponseStatusCodeConstant.OAUTH_TOKEN_MISSING,ResponseMessageConstant.OAUTH_TOKEN_MISSING));}} catch (IOException e) {e.printStackTrace();}} }
权限不足异常类重写
新增 CustomAccessDeniedHandler.java
@Component("customAccessDeniedHandler") public class CustomAccessDeniedHandler implements AccessDeniedHandler {@Overridepublic void handle(HttpServletRequest request, HttpServletResponse response,AccessDeniedException accessDeniedException)throws IOException, ServletException {response.setStatus(HttpStatus.OK.value());response.setHeader("Content-Type", "application/json;charset=UTF-8");try {response.getWriter().write(ResultJsonUtil.build(ResponseCodeConstant.REQUEST_FAILED,ResponseStatusCodeConstant.OAUTH_TOKEN_DENIED,ResponseMessageConstant.OAUTH_TOKEN_DENIED));} catch (IOException e) {e.printStackTrace();}} }
资源配置类中设置异常处理类
修改资源配置类 ResourceServerConfiguration.java
@Override public void configure(ResourceServerSecurityConfigurer resources) {resources.tokenExtractor(customTokenExtractor); resources.authenticationEntryPoint(authExceptionEntryPoint).accessDeniedHandler(customAccessDeniedHandler); }
自定义响应测试
示例代码:https://github.com/BNDong/spring-cloud-examples/tree/master/spring-cloud-zuul/cloud-zuul
转载于:https://www.cnblogs.com/bndong/p/10275430.html
Spring Cloud:Security OAuth2 自定义异常响应相关推荐
- gateway oauth2 对称加密_深入理解Spring Cloud Security OAuth2及JWT
因项目需要,需要和三方的oauth2服务器进行集成.网上关于spring cloud security oauth2的相关资料,一般都是讲如何配置,而能把这块原理讲透彻的比较少,这边自己做一下总结和整 ...
- 玩转Spring Cloud Security OAuth2身份认证扩展——电话号码+验证码认证
在程序的认证过程中,除了常规的用户名和密码方式(可以参考深入理解Spring Cloud Security OAuth2身份认证),也经常会出现电话号码+密码的方式:电话号码+验证码的方式:或者第三方 ...
- 【java_wxid项目】【第七章】【Spring Cloud Security Oauth2集成】
主项目链接:https://gitee.com/java_wxid/java_wxid 项目架构及博文总结: 点击:[使用Spring Boot快速构建应用] 点击:[使用Spring Cloud O ...
- Spring cloud gateway的自定义异常响应
由于gateway默认的返回码和返回信息不能满足下游微服务的自定义的异常响应码和描述,则需要通过网关透传下游微服务的异常的码值或者至少异常描述信息. spring cloud gateway给我们留了 ...
- Spring Cloud Security OAuth2结合网关
文章目录 一.需求分析 二.服务搭建 2.1 注册中心 Eureka 搭建 2.2 网关 Zuul 搭建 2.2.1 转发明文token给微服务 2.2.2 微服务处理 token 2.2.3 Spr ...
- Spring Cloud Security:Oauth2实现单点登录
摘要 Spring Cloud Security 为构建安全的SpringBoot应用提供了一系列解决方案,结合Oauth2可以实现单点登录功能,本文将对其单点登录用法进行详细介绍. 单点登录简介 单 ...
- Spring Cloud Security:Oauth2结合JWT使用
摘要 Spring Cloud Security 为构建安全的SpringBoot应用提供了一系列解决方案,结合Oauth2还可以实现更多功能,比如使用JWT令牌存储信息,刷新令牌功能,本文将对其结合 ...
- Spring Cloud Security:Oauth2使用入门
摘要 Spring Cloud Security 为构建安全的SpringBoot应用提供了一系列解决方案,结合Oauth2可以实现单点登录.令牌中继.令牌交换等功能,本文将对其结合Oauth2入门使 ...
- java B2B2C springmvc mybatis电子商务平台源码-Spring Cloud Security
一.SpringCloud Security简介 Spring Cloud Security提供了一组原语,用于构建安全的应用程序和服务,而且操作简便.可以在外部(或集中)进行大量 ...
- 微服务集成cas_Spring Cloud(四) Spring Cloud Security集成CAS (单点登录)对微服务认证...
一.前言 由于leader要求在搭好的spring cloud 框架中加入对微服务的认证包括单点登录认证,来确保系统的安全,所以研究了Spring Cloud Security这个组件.在前面搭好的d ...
最新文章
- 一次使用 Eclipse Memory Analyzer 分析 Tomcat 内存溢出
- 【Leafletjs】4.L.Map 中文API
- XCTF WEB get_post
- 最近做了一个安装包的安装流程图
- JavaScript——执行环境、变量对象、作用域链
- 设计模式学习之单件模式singleton
- wiki文档_Wiki成为文档的5个理由
- c语言编程继承例子,C语言模拟实现C++的继承与多态示例
- dlut-KFQ概率上机2
- Markdown Cookbook by Eric
- python中文版免费下载-Python IDLE汉化版下载
- 自己把握系统安全,Windows自启动程序的十大藏身之所
- python菜鸟教程python基础教程
- fgo日服服务器维护,FGO国服和日服哪个福利更好 圣晶石维护补偿对比
- [BTS] Unable to create the transform
- 手机对计算机的远程控制软件,教你用手机远程控制电脑,轻松实现远程操作!...
- django接入facebook登陆设置
- 电子产品检验-检验中心
- Java+Springmvc+velement实现高校学科竞赛项目系统+Lw
- 查询student表中姓童的学生情况
热门文章
- 支持10秒自毁的新芯片
- 嵌入式开发之cmos---前端采集aptina cmos
- Android源代码编译——编译
- [原创].关于SD卡的隐藏分区的认识过程及结果
- java中springmvc_java框架之springmvc
- Linux RTC驱动模型分析之rtc-sysfs.c
- Android UI学习之Dialog
- 北航计算机组成重修,北航计算机组成原理123希望大伙把作业上传.pdf
- c语言各定义长度,关于C语言各个数据类型长度
- Linux内核线程kernel thread详解--Linux进程的管理与调度(十)