java获取京东token_关于java:Spring-Security-实战干货OAuth2登录获取Token的核心逻辑

1. 前言

在上一篇Spring Security 实战干货:OAuth2受权回调的外围认证流程中,咱们讲了当第三方批准受权后会调用redirectUri发送回执给咱们的服务器。咱们的服务器拿到一个两头授信凭据会再次进行认证,目标是为了获取Token。而这个逻辑由OAuth2LoginAuthenticationProvider负责,通过上一文的剖析后咱们发现获取Token的具体逻辑由OAuth2AuthorizationCodeAuthenticationProvider来实现,明天就把它的流程搞清楚,来看看Spring Security OAuth2 认证受权获取Token的具体步骤。

留神:本Spring Security干货系列教程的OAuth2相干局部是在Spring Security 5.x版本的。

2. OAuth2AuthorizationCodeAuthenticationProvider

该类是AuthenticationProvider针对OAuth 2.0中Authorization Code Grant模式的实现。对于AuthenticationProvider有必要简略强调一下,它曾经屡次在Spring Security干货系列中呈现,非常重要!肯定要去看看相干的剖析和应用,它是你依据业务扩大认证形式渠道的重要入口。

2.1 OAuth2AccessTokenResponseClient

在该实现中蕴含了一个OAuth2AccessTokenResponseClient成员变量,它形象了通过tokenUri端点从认证服务器获取Token的细节。你能够依据OAuth 2.0罕用的四种模式来进行实现它, 以达到依据不同的策略来获取Token的能力。

在Spring Security 5中OAuth 2.0登录的配置中默认应用DefaultAuthorizationCodeTokenResponseClient。如果你想应用自定义实现的话能够通过HttpSecurity来配置:

@Override

protected void configure(HttpSecurity http) throws Exception {

http.oauth2Login()

.tokenEndpoint()

// 注入自定义的 OAuth2AccessTokenResponseClient

.accessTokenResponseClient(authorizationCodeTokenResponseClient);

// 其它省略

}

接下来咱们看看DefaultAuthorizationCodeTokenResponseClient实现的获取Token的逻辑:

@Override

public OAuth2AccessTokenResponse getTokenResponse(OAuth2AuthorizationCodeGrantRequest authorizationCodeGrantRequest) {

Assert.notNull(authorizationCodeGrantRequest, "authorizationCodeGrantRequest cannot be null");

// 1. 封装调用tokenUri所须要的申请参数RequestEntity

RequestEntity> request = this.requestEntityConverter.convert(authorizationCodeGrantRequest);

ResponseEntity response;

try {

// 2. 通过RestTemplate 发动申请获取 OAuth2AccessTokenResponse

response = this.restOperations.exchange(request, OAuth2AccessTokenResponse.class);

} catch (RestClientException ex) {

OAuth2Error oauth2Error = new OAuth2Error(INVALID_TOKEN_RESPONSE_ERROR_CODE,

"An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response: " + ex.getMessage(), null);

throw new OAuth2AuthorizationException(oauth2Error, ex);

}

// 3. 解析 ResponseEntity 组织返回值 OAuth2AccessTokenResponse

OAuth2AccessTokenResponse tokenResponse = response.getBody();

if (CollectionUtils.isEmpty(tokenResponse.getAccessToken().getScopes())) {

// originally requested by the client in the Token Request

tokenResponse = OAuth2AccessTokenResponse.withResponse(tokenResponse)

.scopes(authorizationCodeGrantRequest.getClientRegistration().getScopes())

.build();

}

return tokenResponse;

}

这里的形式跟我另一个开源我的项目Payment Spring Boot的申请形式殊途同归,都是三个步骤:

组织参数RequestEntity。

RestOperations发动申请。

解析ResponseEntity组织返回值。

如果有些的OAuth 2.0认证服务器获取Token的形式比拟非凡你能够自行实现OAuth2AccessTokenResponseClient。

3. 总结

OAuth2AccessTokenResponseClient是OAuth2AuthorizationCodeAuthenticationProvider的外围要点。搞清楚它的作用和机制就能够了。这里咱们总结一下OAuth2AuthorizationCodeAuthenticationProvider的认证过程:

检测未授信OAuth2AuthorizationCodeAuthenticationToken的状态是否非法。

通过OAuth2AccessTokenResponseClient申请OAuth 2.0认证服务器获取Token等信息。

组装认证过的授信OAuth2AuthorizationCodeAuthenticationToken返回。

到此OAuth 2.0的登录流程就搞清楚了,读者可通过系列文章进行学习批评。我是:码农小胖哥,多多关注,获取实用的编程干货。

关注公众号:Felordcn 获取更多资讯

集体博客:https://felord.cn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值