最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2021年07月30日 16:18:45   作者:CodingPioneer  
Spring Cloud OAuth2 生成access token的請(qǐng)求/oauth/token的返回內(nèi)容就需要自定義,本文就詳細(xì)介紹一下,感興趣的可以了解一下

背景

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

訪問/oauth/token示例如下:

在這里插入圖片描述

原始返回值的格式如下:

在這里插入圖片描述

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

在這里插入圖片描述

實(shí)現(xiàn)原理

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

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

方法上。

代碼實(shí)現(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
@AllArgsConstructor
public 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項(xiàng)目中定義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
@Aspect
public 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)一改
                 * 因?yàn)楸卷?xià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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java+Mysql學(xué)生管理系統(tǒng)源碼

    Java+Mysql學(xué)生管理系統(tǒng)源碼

    這篇文章主要為大家詳細(xì)介紹了Java+Mysql學(xué)生管理系統(tǒng)源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Jsoup解析HTML實(shí)例及文檔方法詳解

    Jsoup解析HTML實(shí)例及文檔方法詳解

    這篇文章主要介紹了Jsoup如何解析一個(gè)HTML文檔、從文件加載文檔、從URL加載Document等方法,對(duì)Jsoup常用方法做了詳細(xì)講解,最近提供了一個(gè)示例供大家參考 使用DOM方法來遍歷一個(gè)文檔 從元素抽取屬性,文本和HTML 獲取所有鏈接
    2013-11-11
  • 淺談java Properties類的使用基礎(chǔ)

    淺談java Properties類的使用基礎(chǔ)

    下面小編就為大家分享一篇淺談java Properties類的使用基礎(chǔ),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • java處理圖片背景顏色的方法

    java處理圖片背景顏色的方法

    這篇文章主要為大家詳細(xì)介紹了java處理圖片背景顏色的方法,藍(lán)底寸照批量轉(zhuǎn)換為白底,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • SpringBoot配置多數(shù)據(jù)源的四種方式分享

    SpringBoot配置多數(shù)據(jù)源的四種方式分享

    在日常開發(fā)中我們都是以單個(gè)數(shù)據(jù)庫進(jìn)行開發(fā),在小型項(xiàng)目中是完全能夠滿足需求的,但是,當(dāng)我們牽扯到大型項(xiàng)目的時(shí)候,單個(gè)數(shù)據(jù)庫就難以承受用戶的CRUD操作,那么此時(shí),我們就需要使用多個(gè)數(shù)據(jù)源進(jìn)行讀寫分離的操作,本文就給大家介紹SpringBoot配置多數(shù)據(jù)源的方式
    2023-07-07
  • 關(guān)于MVC與SpringMVC的介紹、區(qū)別、執(zhí)行流程

    關(guān)于MVC與SpringMVC的介紹、區(qū)別、執(zhí)行流程

    這篇文章主要介紹了關(guān)于MVC與SpringMVC的介紹、區(qū)別、執(zhí)行流程,MVC框架的主要目標(biāo)是將應(yīng)用程序的業(yè)務(wù)邏輯(Model)與用戶界面(View)分離開來,從而提高應(yīng)用程序的可維護(hù)性和可擴(kuò)展性,需要的朋友可以參考下
    2023-05-05
  • JAVA函數(shù)的定義、使用方法實(shí)例分析

    JAVA函數(shù)的定義、使用方法實(shí)例分析

    這篇文章主要介紹了JAVA函數(shù)的定義、使用方法,結(jié)合實(shí)例形式分析了JAVA函數(shù)的基本概念、功能、原理、定義、使用方法與操作注意事項(xiàng),需要的朋友可以參考下
    2020-04-04
  • SpringBoot使用redis生成訂單號(hào)的實(shí)現(xiàn)示例

    SpringBoot使用redis生成訂單號(hào)的實(shí)現(xiàn)示例

    在電商系統(tǒng)中,生成唯一訂單號(hào)是常見需求,本文介紹如何利用SpringBoot和Redis實(shí)現(xiàn)訂單號(hào)的生成,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-09-09
  • AsyncConfigurerSupport自定義異步線程池處理異常

    AsyncConfigurerSupport自定義異步線程池處理異常

    這篇文章主要為大家介紹了AsyncConfigurerSupport自定義異步線程池處理異常詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • MyBatis-Plus中靜態(tài)工具Db的多種用法及實(shí)例分析

    MyBatis-Plus中靜態(tài)工具Db的多種用法及實(shí)例分析

    本文將詳細(xì)講解MyBatis-Plus中靜態(tài)工具Db的各種用法,并結(jié)合具體案例進(jìn)行演示和說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03

最新評(píng)論

淮安市| 定兴县| 夏河县| 镇雄县| 修水县| 上饶市| 长春市| 河源市| 军事| 上蔡县| 东乌| 东兰县| 石柱| 五河县| 聂拉木县| 肇庆市| 石渠县| 博野县| 静乐县| 巴林右旗| 霞浦县| 郯城县| 禹州市| 迁安市| 明星| 兴海县| 武定县| 西华县| 姜堰市| 元江| 密云县| 安新县| 广河县| 巴南区| 德兴市| 乐亭县| 瓮安县| 泸溪县| 图们市| 杭锦后旗| 彰化县|