Spring Reactive OAuth2: iarratais ar thócan client_credentials trí sheachfhreastalaí
Le client_credentials i Spring Security 6.5, ceangail WebClient an tócain tríd an soláthraí leis an mbainisteoir authorized-client a úsáidtear i ndáiríre.
Is féidir le hacmhainn WebClient a théann trí sheachfhreastalaí teacht anuas fós roimh a glao API mura n-úsáideann fáil an tócain nascóir eile. Le haghaidh sreabhadh imoibríoch client_credentials Spring Security 6.5, ceangail na trí phíosa:
HttpClient route = HttpClient.create()
.proxy(p -> p.type(ProxyProvider.Proxy.HTTP)
.host("proxy.example").port(8080));
WebClient tokenHttp = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(route))
.build();
var tokens = new WebClientReactiveClientCredentialsTokenResponseClient();
tokens.setWebClient(tokenHttp);
var provider = new ClientCredentialsReactiveOAuth2AuthorizedClientProvider();
provider.setAccessTokenResponseClient(tokens);
var manager = new AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager(
registrations, authorizedClients);
manager.setAuthorizedClientProvider(provider);Sannann tokens.setWebClient an cliant HTTP tócain; sannann provider.setAccessTokenResponseClient don soláthraí deontais é; suiteálann manager.setAuthorizedClientProvider an soláthraí sin. Úsáid an bainisteoir seo sa scagaire OAuth iarbhír nó i sreabhadh oibre authorized-client.
Is cineálacha Reactor Netty iad HttpClient agus ProxyProvider. Is cineálacha Spring WebFlux iad WebClient agus a nascóir. Tabhair an ReactiveClientRegistrationRepository atá ann mar registrations agus an ReactiveOAuth2AuthorizedClientService mar authorizedClients.
Láimhseálann an soláthraí seo client_credentials; coinnigh slabhra soláthraithe oiriúnach má theastaíonn deontais eile. Níl cuntas seachfhreastalaí sa sampla; bhainfeadh sé sin leis an iompar, ní le OAuth client ID/secret. Le haghaidh críochphointe tócain phoiblí a cheadaítear trí idirghabhálaí poiblí, roghnaigh óstach agus port ón liosta seachfhreastalaí HTTP.
Cumraigh nascóir na hacmhainne cosanta freisin nuair a theastaíonn an mharcshlí ó na glaonna API sin. Is torthaí ar leith iad rath an tócain agus rath an API.