問題描述
我想為我的 OAuth2 提供者增強令牌請求.我需要向 POST 請求添加一個附加參數.我不明白在哪里掛鉤到 Spring Boot 框架來完成這個.
I want to enhance the token request for my OAuth2 provider. I need to add an additional parameter to the POST request. I don't understand where to hook into the Spring Boot framework to accomplish this.
Spring Boot 框架提供了一個用于自定義 OAuth2RestTemplate 的鉤子,如自定義用戶信息 RestTemplate".我已經實現了以下定制器,它被實例化并按預期調用.不幸的是,發出令牌請求時似乎沒有調用我的提供程序.
The Spring Boot framework provides a hook for customizing the OAuth2RestTemplate as described in "Customizing the User Info RestTemplate". I have implemented the following customizer, which gets instantiated and called as expected. Unfortunately, my provider does not seem to get called when the token request is made.
public class AadUserInfoRestTemplateCustomizer implements UserInfoRestTemplateCustomizer {
@Override
public void customize(OAuth2RestTemplate oAuth2RestTemplate) {
oAuth2RestTemplate.setAuthenticator(new AadOauth2RequestAuthenticator());
// Attempt 1: Use my own token provider, but it never gets called...
oAuth2RestTemplate.setAccessTokenProvider(new AadAccessTokenProvider());
// Even better, if only OAuth2RestTemplate provided a getter for AccessTokenProvider, I could add interceptors and or enhancers
// Can't do this :( AuthorizationCodeAccessTokenProvider provider = oAuth2RestTemplate.getAccessTokenProvider();
}
}
問題:
如何設置自定義 AccessTokeProvder,或者更好的是,獲取對默認值的引用并使用攔截器或增強器掛鉤到請求中?
How does set a custom AccessTokeProvder, or even better, get a reference to the default one and hook into the request with an interceptor or enhancer?
代碼示例
在下面的分叉中,請查看/simple 模塊.將您的 AAD 租戶信息添加到/simple/src/main/resources/application.yml 文件中:
In the fork below, please see the /simple module. Add your AAD tenant info into the /simple/src/main/resources/application.yml file:
https://github.com/bmillerbma/tut-spring-boot-oauth2/tree/aad
注意事項:
這個提交似乎是框架使這成為可能,但是如何利用這一功能呢?
This commit to the framework seems to make this possible, but how does one leverage this functionality?
這個問題似乎是相關的.不知何故,這個家伙添加了一個自定義提供程序.但是在哪里?
This question seems to be related. Somehow the fella added a custom provider. But where?
推薦答案
我遇到了同樣的問題并使用了這個解決方法,但因此我堅持使用 spring boot 1.3.8
I came across with the same issue and used this workaround but because of this I stuck with spring boot 1.3.8
所以我開始深入挖掘,然后我終于找到了一個更簡單的方法.只需在 userAuthorizationUri
之后添加資源參數即可.
So I started to dig deeper and then I finally found an easier method. Just add a resource parameter after the userAuthorizationUri
.
security:
oauth2:
client:
...
userAuthorizationUri: https://login.microsoftonline.com/<<tenantId>>/oauth2/authorize?resource=https://graph.windows.net
...
這篇關于如何自定義 Spring Boot AccessTokenProvider?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!