色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Spring Cloud OAuth2中/oauth/token的返回內容格式

瀏覽:23日期:2023-06-28 10:51:02
目錄背景實現原理代碼實現相關類關鍵切面攔截器背景

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

訪問/oauth/token示例如下:

Spring Cloud OAuth2中/oauth/token的返回內容格式

原始返回值的格式如下:

Spring Cloud OAuth2中/oauth/token的返回內容格式

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

Spring Cloud OAuth2中/oauth/token的返回內容格式

實現原理

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

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

方法上。

代碼實現相關類

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); }}關鍵切面攔截器

在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. 統一返回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 注解不能修改返回格式,否則授權碼模式可以統一改 * 因為本項目的 sso-demo/ss-sso 里面使用了 @EnableOAuth2Sso 注解,所以這里就不修改授權碼模式的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 {/** * 授權碼模式 */ String AUTHORIZATION_CODE = 'authorization_code';}

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

標簽: Spring
相關文章:
主站蜘蛛池模板: 国产乱码精品一区二区三区卡 | 日韩第一视频 | 国产蜜汁tv福利在线 | 日本一级高清不卡视频在线 | 91国偷自产一区二区三区 | 欧美一级免费观看 | 亚洲怡红院在线 | 精品手机在线视频 | 欧美另类亚洲一区二区 | 久草手机在线视频 | 亚洲第一激情 | 怡红院成人永久免费看 | 亚洲成人中文 | 日韩欧美亚洲中字幕在线播放 | 高清一区在线 | 国产第一草草影院 | 久久成人精品免费播放 | 国产一二三区在线 | 国产精品久久久精品视频 | 欧美日韩在线视频不卡一区二区三区 | 精品国产三级a∨在线观看 精品国产三级a在线观看 | 99精品视频在线观看re | 美国毛片基地a级e片 | 特级a做爰全过程片 | 欧美成人精品大片免费流量 | 国产老妇k | 亚洲精品视频专区 | 真实国产精品视频国产网 | 亚洲系列 | 国产精品18久久久久久久久久 | 国产精品久久久久三级 | 免费看成人 | 中文字幕无线精品乱码一区 | 国产一级做a爰片久久毛片 国产一级做a爰片久久毛片99 | 亚洲国产日韩精品 | 欧美激情一区二区三区高清视频 | 91久久99热青草国产 | 成人aaa| 免费看一级视频 | 深爱五月开心网亚洲综合 | 成人国产精品一级毛片了 |