电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

Spring Cloud OAuth2中/oauth/token的返回內(nèi)容格式

瀏覽:45日期:2023-06-28 10:51:02
目錄背景實現(xiàn)原理代碼實現(xiàn)相關(guān)類關(guān)鍵切面攔截器背景

在前后端分離的項目中,一般后端返回給前端的格式是一個固定的json格式。在這個前提下,Spring Cloud OAuth2 生成access token的請求/oauth/token的返回內(nèi)容就需要自定義。

訪問/oauth/token示例如下:

Spring Cloud OAuth2中/oauth/token的返回內(nèi)容格式

原始返回值的格式如下:

Spring Cloud OAuth2中/oauth/token的返回內(nèi)容格式

我們希望使用我們自己固定的json格式,如下:

Spring Cloud OAuth2中/oauth/token的返回內(nèi)容格式

實現(xiàn)原理

原理就是通過切面編程實現(xiàn)對/oauth/token端點請求的結(jié)果進(jìn)行攔截封裝處理,由于/oauth/token是Spring Cloud OAuth2的內(nèi)部端點,因此需要對相關(guān)的Spring源碼進(jìn)行分析。最終定位到

org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.postAccessToken()

方法上。

代碼實現(xiàn)相關(guān)類

CodeEnum.java

package com.wongoing.common.model;/** * @description: 代碼枚舉 * @author: zheng * @date: Created in 2021/1/26 11:18 * @version: 0.0.1 * @modified By: */public enum CodeEnum { SUCCESS(0), ERROR(1); private Integer code; CodeEnum(Integer code) {this.code = code; } public Integer getCode() {return this.code; }}

Result.java

package com.wongoing.common.model;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import java.io.Serializable;/** * @description: Rest API 接口方法返回類型定義 * @author: zheng * @date: Created in 2021/1/26 13:25 * @version: 0.0.1 * @modified By: */@Data@NoArgsConstructor@AllArgsConstructorpublic class Result<T> implements Serializable { private T data; private Integer code; private String msg; public static <T> Result<T> of(T data, Integer code, String msg) {return new Result<>(data, code, msg); } public static <T> Result<T> succeed(String msg) {return of(null, CodeEnum.SUCCESS.getCode(), msg); } public static <T> Result<T> succeed(T model, String msg) {return of(model, CodeEnum.SUCCESS.getCode(), msg); } public static <T> Result<T> succeed(T model) {return of(model, CodeEnum.SUCCESS.getCode(), ''); } public static <T> Result<T> failed(String msg) {return of(null, CodeEnum.ERROR.getCode(), msg); } public static <T> Result<T> failed(T model, String msg) {return of(model, CodeEnum.ERROR.getCode(), msg); }}關(guān)鍵切面攔截器

在uaa項目中定義OauthTokenAspect.java

package com.wongoing.oauth2.filter;import com.wongoing.common.constant.SecurityConstants;import com.wongoing.common.context.TenantContextHolder;import com.wongoing.common.model.Result;import lombok.extern.slf4j.Slf4j;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.security.authentication.InsufficientAuthenticationException;import org.springframework.security.core.Authentication;import org.springframework.security.oauth2.common.OAuth2AccessToken;import org.springframework.security.oauth2.common.util.OAuth2Utils;import org.springframework.security.oauth2.provider.OAuth2Authentication;import org.springframework.stereotype.Component;import java.security.Principal;import java.util.Map;/** * @description: oauth-token攔截器 * 1. 賦值租戶 * 2. 統(tǒng)一返回token格式 * * @author: zheng * @date: Created in 2021/7/12 16:25 * @version: 0.0.1 * @modified By: */@Slf4j@Component@Aspectpublic class OauthTokenAspect { @Around('execution(* org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.postAccessToken(..))') public Object handleControllerMethod(ProceedingJoinPoint joinPoint) throws Throwable {try { Object[] args = joinPoint.getArgs(); Principal principal = (Principal) args[0]; if (!(principal instanceof Authentication)) {throw new InsufficientAuthenticationException('There is no client authentication. Try adding an appropriate authentication filter.'); } String clientId = this.getClientId(principal); Map<String, String> parameters = (Map<String, String>) args[1]; String grantType = parameters.get(OAuth2Utils.GRANT_TYPE); //保存租戶id TenantContextHolder.setTenant(clientId); Object proceed = joinPoint.proceed(); if (SecurityConstants.AUTHORIZATION_CODE.equals(grantType)) {/** * 如果使用 @EnableOAuth2Sso 注解不能修改返回格式,否則授權(quán)碼模式可以統(tǒng)一改 * 因為本項目的 sso-demo/ss-sso 里面使用了 @EnableOAuth2Sso 注解,所以這里就不修改授權(quán)碼模式的token返回值了 */return proceed; } else {ResponseEntity<OAuth2AccessToken> responseEntity = (ResponseEntity<OAuth2AccessToken>) proceed;OAuth2AccessToken body = responseEntity.getBody();return ResponseEntity.status(HttpStatus.OK).body(Result.succeed(body)); }} finally { TenantContextHolder.clear();} } private String getClientId(Principal principal) {Authentication client = (Authentication) principal;if (!client.isAuthenticated()) { throw new InsufficientAuthenticationException('The client is not authenticated.');}String clientId = client.getName();if (client instanceof OAuth2Authentication) { clientId = ((OAuth2Authentication) client).getOAuth2Request().getClientId();}return clientId; }}

其中的常量值:

public abstract class OAuth2Utils {public static final String GRANT_TYPE = 'grant_type';}

public interface SecurityConstants {/** * 授權(quán)碼模式 */ String AUTHORIZATION_CODE = 'authorization_code';}

到此這篇關(guān)于Spring Cloud OAuth2中/oauth/token的返回內(nèi)容格式的文章就介紹到這了,更多相關(guān)Spring Cloud OAuth2返回內(nèi)容格式內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 高压包-点火器-高压发生器-点火变压器-江苏天网 | 斗式提升机_链式斗提机_带式斗提机厂家无锡市鸿诚输送机械有限公司 | 壹作文_中小学生优秀满分作文大全 | 济南网站策划设计_自适应网站制作_H5企业网站搭建_济南外贸网站制作公司_锐尚 | 手持式线材张力计-套帽式风量罩-深圳市欧亚精密仪器有限公司 | 细石混凝土泵_厂家_价格-烟台九达机械有限公司 | 防爆鼓风机-全风-宏丰鼓风机-上海梁瑾机电设备有限公司 | 煤机配件厂家_刮板机配件_链轮轴组_河南双志机械设备有限公司 | 在线PH计-氧化锆分析仪-在线浊度仪-在线溶氧仪- 无锡朝达 | 威客电竞(vk·game)·电子竞技赛事官网 | 康明斯发电机,上柴柴油发电机,玉柴柴油发电机组_海南重康电力官网 | 无纺布包装机|径向缠绕包装机|缠绕膜打包机-上海晏陵智能设备有限公司 | 真空乳化机-灌装封尾机-首页-温州精灌| 时代北利离心机,实验室离心机,医用离心机,低速离心机DT5-2,美国SKC采样泵-上海京工实业有限公司 工业电炉,台车式电炉_厂家-淄博申华工业电炉有限公司 | 包塑丝_高铁绑丝_地暖绑丝_涂塑丝_塑料皮铁丝_河北创筹金属丝网制品有限公司 | 旋振筛|圆形摇摆筛|直线振动筛|滚筒筛|压榨机|河南天众机械设备有限公司 | 塑料脸盆批发,塑料盆生产厂家,临沂塑料广告盆,临沂家用塑料盆-临沂市永顺塑业 | 重庆私家花园设计-别墅花园-庭院-景观设计-重庆彩木园林建设有限公司 | 电采暖锅炉_超低温空气源热泵_空气源热水器-鑫鲁禹电锅炉空气能热泵厂家 | 天品互联-北京APP开发公司-小程序开发制作-软件开发 | 12cr1mov无缝钢管切割-15crmog无缝钢管切割-40cr无缝钢管切割-42crmo无缝钢管切割-Q345B无缝钢管切割-45#无缝钢管切割 - 聊城宽达钢管有限公司 | 新中天检测有限公司青岛分公司-山东|菏泽|济南|潍坊|泰安防雷检测验收 | 智能化的检漏仪_气密性测试仪_流量测试仪_流阻阻力测试仪_呼吸管快速检漏仪_连接器防水测试仪_车载镜头测试仪_奥图自动化科技 | 电杆荷载挠度测试仪-电杆荷载位移-管桩测试仪-北京绿野创能机电设备有限公司 | ERP企业管理系统永久免费版_在线ERP系统_OA办公_云版软件官网 | LZ-373测厚仪-华瑞VOC气体检测仪-个人有毒气体检测仪-厂家-深圳市深博瑞仪器仪表有限公司 | 蔬菜配送公司|蔬菜配送中心|食材配送|饭堂配送|食堂配送-首宏公司 | 台湾Apex减速机_APEX行星减速机_台湾精锐减速机厂家代理【现货】-杭州摩森机电 | 压缩空气检测_气体_水质找上海京工-服务专业、价格合理 | CXB船用变压器-JCZ系列制动器-HH101船用铜质开关-上海永上船舶电器厂 | 聚氨酯复合板保温板厂家_廊坊华宇创新科技有限公司 | 山楂片_雪花_迷你山楂片_山楂条饼厂家-青州市丰源食品厂 | 高低温万能试验机-复合材料万能试验机-馥勒仪器 | 除湿机|工业除湿机|抽湿器|大型地下室车间仓库吊顶防爆除湿机|抽湿烘干房|新风除湿机|调温/降温除湿机|恒温恒湿机|加湿机-杭州川田电器有限公司 | 中国产业发展研究网 - 提供行业研究报告 可行性研究报告 投资咨询 市场调研服务 | 液压升降平台_剪叉式液压/导轨式升降机_传菜机定做「宁波日腾升降机厂家」 | 衬四氟_衬氟储罐_四氟储罐-无锡市氟瑞特防腐科技有限公司 | 一体化净水器_一体化净水设备_一体化水处理设备-江苏旭浩鑫环保科技有限公司 | 托利多电子平台秤-高精度接线盒-托利多高精度电子秤|百科 | 医院专用门厂家报价-医用病房门尺寸大全-抗菌木门品牌推荐 | 设计圈 - 让设计更有价值!|