{"id":346140,"date":"2023-03-02T09:01:19","date_gmt":"2023-03-02T09:01:19","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=346140"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=346140","title":{"rendered":"<span>Spring Cloud OpenFeign \u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f (OAuth2). \u0415\u0449\u0451 \u043f\u0440\u043e\u0449\u0435<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u043d\u0435\u0439 \u043d\u0430\u0437\u0430\u0434 \u0431\u044b\u043b\u0430 \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043c\u0435\u0436\u0434\u0443 \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c\u0438 \u0441 \u0442\u043e\u043a\u0435\u043d\u043e\u043c. \u0423 \u043d\u0430\u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f Keycloak \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 SSO. \u0412 \u0441\u0438\u043b\u0443 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u0441\u0451 \u043d\u0430 \u0431\u0430\u0437\u043e\u0432\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 (\u043d\u0438\u0447\u0435\u0433\u043e \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e),\u0442\u043e \u0438\u043c\u0435\u0435\u0442 \u0441\u043c\u044b\u0441\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Feign Client (OpenFeign) \u0434\u043b\u044f Spring Boot.<\/p>\n<p>\u041d\u0430\u0447\u0430\u043b \u043a\u043e\u043f\u0430\u0442\u044c\u0441\u044f \u0432 \u0432\u043e\u043f\u0440\u043e\u0441\u0435 \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f OAuth \u0434\u043b\u044f Feign \u0438 \u0447\u0435\u0440\u0435\u0437 1-2 \u0434\u043d\u044f \u043d\u0430\u0442\u043a\u043d\u0443\u043b\u0441\u044f \u043d\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043d\u043d\u0443\u044e <a href=\"https:\/\/habr.com\/ru\/post\/717610\/\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0430\u0442\u044c\u044e<\/a> \u043e\u0442 <a class=\"mention\" href=\"\/users\/kinski\">@Kinski<\/a>, \u043d\u043e, \u043a\u0430\u043a \u0438 \u0432\u0435\u0437\u0434\u0435, \u0432 \u043d\u0435\u0439 \u0438 \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445 \u0435\u0441\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0438 \u0434\u0430\u0436\u0435 \u0432 \u0442\u043e\u0439 \u0436\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438. \u041e\u043f\u0438\u0448\u0443 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f \u0441\u0447\u0451\u043b \u0432\u0435\u0440\u043d\u044b\u043c\u0438 \u0438 \u043f\u0440\u043e\u0441\u0442\u044b\u043c\u0438 \u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435\u043c.<\/p>\n<p>\u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 <u>client.registration<\/u> \u0443 \u043d\u0430\u0441 <strong>keycloak<\/strong>.<br \/>Spring Boot 2.7.6<br \/>Spring Cloud 2021.0.5<\/p>\n<h2>1. \u0412\u043a\u043b\u044e\u0447\u0430\u0435\u043c OAuth2 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 Feign \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432<\/h2>\n<p>\u041f\u0435\u0440\u0432\u0430\u044f \u043d\u0435\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u043f\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 &#8212; \u044d\u0442\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a\u0430\u043a \u0440\u0430\u0437 \u044d\u0442\u043e\u0433\u043e OAuth \u0434\u043b\u044f Feign. \u0412\u043c\u0435\u0441\u0442\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043a \u0434\u043b\u044f Spring<\/p>\n<pre><code class=\"yaml\">spring:   cloud:     openfeign:       oauth2:         enabled: true         clientRegistrationId: keycloak<\/code><\/pre>\n<p>\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0432\u043a\u043b\u044e\u0447\u0430\u0442.<\/p>\n<ol start=\"2\">\n<li>\n<p>\u043d\u0430\u0434\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"yaml\">feign:   oauth2:     enabled: true     clientRegistrationId: keycloak<\/code><\/pre>\n<p>\u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043f\u0430\u043a\u0435\u0442\u044b:<br \/>&#8212; <u>spring-boot-starter-security<\/u><br \/>&#8212; <u>spring-boot-starter-oauth2-client<\/u><br \/>&#8212; <u>spring-cloud-starter-openfeign<\/u>.<\/p>\n<ol start=\"3\">\n<li>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0441\u0430\u043c OAuth2Client security<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"java\">public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {     http         \/\/ \u0432\u0430\u0448\u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 +         .oauth2Client();     return http.build(); }<\/code><\/pre>\n<ol start=\"4\">\n<li>\n<p>\u043d\u0443 \u0438 \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0434\u043b\u044f OAuth2 \u043a\u043b\u0438\u0435\u043d\u0442\u0430<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"yaml\">spring:   security:     oauth2:       client:         registration:           keycloak:             client-id: id \u0432\u0430\u0448\u0435\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430             client-secret: \u0432\u0430\u0448 \u0441\u0435\u043a\u0440\u0435\u0442             authorization-grant-type: client_credentials             provider: keycloak         provider:           keycloak:             token-uri: \u0430\u0434\u0440\u0435\u0441 \u0434\u043e \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430 \u0442\u043e\u043a\u0435\u043d\u0430<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0432\u0441\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0447\u0435\u0440\u0435\u0437 Feign Client \u0431\u0443\u0434\u0443\u0442 \u043e\u0431\u043e\u0433\u043e\u0449\u0430\u0442\u044c\u0441\u044f OAuth \u0442\u043e\u043a\u0435\u043d\u043e\u043c.<\/p>\n<h2>2. \u0412\u043a\u043b\u044e\u0447\u0430\u0435\u043c OAuth2 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043d\u0443\u0436\u043d\u044b\u0445 Feign \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432<\/h2>\n<p>\u041f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c \u0438\u0437 1\u0433\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0430 \u043f\u0443\u043d\u043a\u0442\u044b 3 \u0438 4. \u0414\u0430\u043b\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u0434\u043b\u044f Feign<\/p>\n<pre><code class=\"java\">public class OAuthFeignConfig {     private final OAuth2ClientProperties oAuth2ClientProperties;      public OAuthFeignConfig(OAuth2ClientProperties oAuth2ClientProperties) {         this.oAuth2ClientProperties = oAuth2ClientProperties;     }      @Bean     @ConditionalOnBean({OAuth2AuthorizedClientService.class, ClientRegistrationRepository.class})     @ConditionalOnMissingBean     public OAuth2AuthorizedClientManager feignOAuth2AuthorizedClientManager(ClientRegistrationRepository clientRegistrationRepository,             OAuth2AuthorizedClientService oAuth2AuthorizedClientService) {         return new AuthorizedClientServiceOAuth2AuthorizedClientManager(clientRegistrationRepository, oAuth2AuthorizedClientService);     }      @Bean     @ConditionalOnBean(OAuth2AuthorizedClientManager.class)     public OAuth2AccessTokenInterceptor defaultOAuth2AccessTokenInterceptor(OAuth2AuthorizedClientManager oAuth2AuthorizedClientManager) {         return new OAuth2AccessTokenInterceptor(oAuth2ClientProperties.getRegistration().keySet().iterator().next(), oAuth2AuthorizedClientManager);     } }<\/code><\/pre>\n<p>\u0424\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043a\u043e\u0434 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0431\u0438\u043d\u043e\u0432 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u043e\u0432 \u0438 \u043f\u043e\u043f\u0440\u0430\u0432\u043b\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043c\u043e\u043c\u0435\u043d\u0442\u0435 \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u043d\u0438\u044f \u0438\u043c\u0435\u043d\u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0439 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u0438\u0437 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043a.<br \/>\u0412 \u0440\u0430\u0441\u043a\u043e\u043f\u043a\u0430\u0445 \u043f\u043e\u043c\u043e\u0433\u043b\u0438 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438:<\/p>\n<ul>\n<li>\n<p><code>FeignAutoConfiguration<\/code>\u00a0from\u00a0<code>org.springframework.cloud.openfeign<\/code><\/p>\n<\/li>\n<li>\n<p><code>OAuth2AccessTokenInterceptor<\/code>\u00a0from\u00a0<code>org.springframework.cloud.openfeign.security<\/code><\/p>\n<\/li>\n<\/ul>\n<p>\u0412 19\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b <code>oAuth2ClientProperties.getRegistration().keySet().iterator().next()<\/code>  \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0439 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 (\u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0445\u0430\u0440\u0434\u043a\u043e\u0434\u0438\u0442\u044c. \u0423 \u043d\u0430\u0441 \u044d\u0442\u043e keycloak).<br \/>\u0442\u0435\u043f\u0435\u0440\u044c \u0432 \u043b\u044e\u0431\u043e\u043c \u043d\u0430\u0448\u0435\u043c FeignClient \u0434\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e:<\/p>\n<pre><code class=\"java\">@FeignClient(value = \"client-name\", url = \"your_url\", configuration = OAuthFeignConfig.class)<\/code><\/pre>\n<p>PS: \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u0435\u0440\u0432\u0430\u044f,\u0441\u0438\u043b\u044c\u043d\u043e \u043d\u0435 \u0431\u0435\u0439\u0442\u0435. \u0420\u0435\u0448\u0438\u043b \u043e\u0444\u043e\u0440\u043c\u0438\u0442\u044c \u043f\u043e\u0441\u043b\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u043e\u0442 <a class=\"mention\" href=\"\/users\/kinski\">@Kinski<\/a>\u0441 \u0431\u043e\u043b\u0435\u0435 \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u044b\u043c \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435\u043c \u0438 \u0440\u0430\u0441\u043a\u0440\u044b\u0442\u0438\u0435\u043c \u043d\u044e\u0430\u043d\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0440\u0430\u0445 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430 \u043d\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u043d\u0438\u0433\u0434\u0435.<br \/>\u0415\u0441\u043b\u0438 \u043a\u043e\u043c\u0443-\u0442\u043e \u0441\u0442\u0430\u0442\u044c\u044f \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442 \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 &#8212; \u0431\u0443\u0434\u0443 \u0440\u0430\u0434!<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/719934\/\"> https:\/\/habr.com\/ru\/post\/719934\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u043d\u0435\u0439 \u043d\u0430\u0437\u0430\u0434 \u0431\u044b\u043b\u0430 \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043c\u0435\u0436\u0434\u0443 \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c\u0438 \u0441 \u0442\u043e\u043a\u0435\u043d\u043e\u043c. \u0423 \u043d\u0430\u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f Keycloak \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 SSO. \u0412 \u0441\u0438\u043b\u0443 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u0441\u0451 \u043d\u0430 \u0431\u0430\u0437\u043e\u0432\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 (\u043d\u0438\u0447\u0435\u0433\u043e \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e),\u0442\u043e \u0438\u043c\u0435\u0435\u0442 \u0441\u043c\u044b\u0441\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Feign Client (OpenFeign) \u0434\u043b\u044f Spring Boot.<\/p>\n<p>\u041d\u0430\u0447\u0430\u043b \u043a\u043e\u043f\u0430\u0442\u044c\u0441\u044f \u0432 \u0432\u043e\u043f\u0440\u043e\u0441\u0435 \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f OAuth \u0434\u043b\u044f Feign \u0438 \u0447\u0435\u0440\u0435\u0437 1-2 \u0434\u043d\u044f \u043d\u0430\u0442\u043a\u043d\u0443\u043b\u0441\u044f \u043d\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043d\u043d\u0443\u044e <a href=\"https:\/\/habr.com\/ru\/post\/717610\/\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0430\u0442\u044c\u044e<\/a> \u043e\u0442 <a class=\"mention\" href=\"\/users\/kinski\">@Kinski<\/a>, \u043d\u043e, \u043a\u0430\u043a \u0438 \u0432\u0435\u0437\u0434\u0435, \u0432 \u043d\u0435\u0439 \u0438 \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445 \u0435\u0441\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0438 \u0434\u0430\u0436\u0435 \u0432 \u0442\u043e\u0439 \u0436\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438. \u041e\u043f\u0438\u0448\u0443 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f \u0441\u0447\u0451\u043b \u0432\u0435\u0440\u043d\u044b\u043c\u0438 \u0438 \u043f\u0440\u043e\u0441\u0442\u044b\u043c\u0438 \u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435\u043c.<\/p>\n<p>\u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 <u>client.registration<\/u> \u0443 \u043d\u0430\u0441 <strong>keycloak<\/strong>.<br \/>Spring Boot 2.7.6<br \/>Spring Cloud 2021.0.5<\/p>\n<h2>1. \u0412\u043a\u043b\u044e\u0447\u0430\u0435\u043c OAuth2 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 Feign \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432<\/h2>\n<p>\u041f\u0435\u0440\u0432\u0430\u044f \u043d\u0435\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u043f\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 &#8212; \u044d\u0442\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a\u0430\u043a \u0440\u0430\u0437 \u044d\u0442\u043e\u0433\u043e OAuth \u0434\u043b\u044f Feign. \u0412\u043c\u0435\u0441\u0442\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043a \u0434\u043b\u044f Spring<\/p>\n<pre><code class=\"yaml\">spring:   cloud:     openfeign:       oauth2:         enabled: true         clientRegistrationId: keycloak<\/code><\/pre>\n<p>\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0432\u043a\u043b\u044e\u0447\u0430\u0442.<\/p>\n<ol start=\"2\">\n<li>\n<p>\u043d\u0430\u0434\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"yaml\">feign:   oauth2:     enabled: true     clientRegistrationId: keycloak<\/code><\/pre>\n<p>\u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043f\u0430\u043a\u0435\u0442\u044b:<br \/>&#8212; <u>spring-boot-starter-security<\/u><br \/>&#8212; <u>spring-boot-starter-oauth2-client<\/u><br \/>&#8212; <u>spring-cloud-starter-openfeign<\/u>.<\/p>\n<ol start=\"3\">\n<li>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0441\u0430\u043c OAuth2Client security<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"java\">public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {     http         \/\/ \u0432\u0430\u0448\u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 +         .oauth2Client();     return http.build(); }<\/code><\/pre>\n<ol start=\"4\">\n<li>\n<p>\u043d\u0443 \u0438 \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0434\u043b\u044f OAuth2 \u043a\u043b\u0438\u0435\u043d\u0442\u0430<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"yaml\">spring:   security:     oauth2:       client:         registration:           keycloak:             client-id: id \u0432\u0430\u0448\u0435\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430             client-secret: \u0432\u0430\u0448 \u0441\u0435\u043a\u0440\u0435\u0442             authorization-grant-type: client_credentials             provider: keycloak         provider:           keycloak:             token-uri: \u0430\u0434\u0440\u0435\u0441 \u0434\u043e \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430 \u0442\u043e\u043a\u0435\u043d\u0430<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0432\u0441\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0447\u0435\u0440\u0435\u0437 Feign Client \u0431\u0443\u0434\u0443\u0442 \u043e\u0431\u043e\u0433\u043e\u0449\u0430\u0442\u044c\u0441\u044f OAuth \u0442\u043e\u043a\u0435\u043d\u043e\u043c.<\/p>\n<h2>2. \u0412\u043a\u043b\u044e\u0447\u0430\u0435\u043c OAuth2 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043d\u0443\u0436\u043d\u044b\u0445 Feign \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432<\/h2>\n<p>\u041f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c \u0438\u0437 1\u0433\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0430 \u043f\u0443\u043d\u043a\u0442\u044b 3 \u0438 4. \u0414\u0430\u043b\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u0434\u043b\u044f Feign<\/p>\n<pre><code class=\"java\">public class OAuthFeignConfig {     private final OAuth2ClientProperties oAuth2ClientProperties;      public OAuthFeignConfig(OAuth2ClientProperties oAuth2ClientProperties) {         this.oAuth2ClientProperties = oAuth2ClientProperties;     }      @Bean     @ConditionalOnBean({OAuth2AuthorizedClientService.class, ClientRegistrationRepository.class})     @ConditionalOnMissingBean     public OAuth2AuthorizedClientManager feignOAuth2AuthorizedClientManager(ClientRegistrationRepository clientRegistrationRepository,             OAuth2AuthorizedClientService oAuth2AuthorizedClientService) {         return new AuthorizedClientServiceOAuth2AuthorizedClientManager(clientRegistrationRepository, oAuth2AuthorizedClientService);     }      @Bean     @ConditionalOnBean(OAuth2AuthorizedClientManager.class)     public OAuth2AccessTokenInterceptor defaultOAuth2AccessTokenInterceptor(OAuth2AuthorizedClientManager oAuth2AuthorizedClientManager) {         return new OAuth2AccessTokenInterceptor(oAuth2ClientProperties.getRegistration().keySet().iterator().next(), oAuth2AuthorizedClientManager);     } }<\/code><\/pre>\n<p>\u0424\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043a\u043e\u0434 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0431\u0438\u043d\u043e\u0432 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u043e\u0432 \u0438 \u043f\u043e\u043f\u0440\u0430\u0432\u043b\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043c\u043e\u043c\u0435\u043d\u0442\u0435 \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u043d\u0438\u044f \u0438\u043c\u0435\u043d\u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0439 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u0438\u0437 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043a.<br \/>\u0412 \u0440\u0430\u0441\u043a\u043e\u043f\u043a\u0430\u0445 \u043f\u043e\u043c\u043e\u0433\u043b\u0438 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438:<\/p>\n<ul>\n<li>\n<p><code>FeignAutoConfiguration<\/code>\u00a0from\u00a0<code>org.springframework.cloud.openfeign<\/code><\/p>\n<\/li>\n<li>\n<p><code>OAuth2AccessTokenInterceptor<\/code>\u00a0from\u00a0<code>org.springframework.cloud.openfeign.security<\/code><\/p>\n<\/li>\n<\/ul>\n<p>\u0412 19\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b <code>oAuth2ClientProperties.getRegistration().keySet().iterator().next()<\/code>  \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0439 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 (\u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0445\u0430\u0440\u0434\u043a\u043e\u0434\u0438\u0442\u044c. \u0423 \u043d\u0430\u0441 \u044d\u0442\u043e keycloak).<br \/>\u0442\u0435\u043f\u0435\u0440\u044c \u0432 \u043b\u044e\u0431\u043e\u043c \u043d\u0430\u0448\u0435\u043c FeignClient \u0434\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e:<\/p>\n<pre><code class=\"java\">@FeignClient(value = \"client-name\", url = \"your_url\", configuration = OAuthFeignConfig.class)<\/code><\/pre>\n<p>PS: \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u0435\u0440\u0432\u0430\u044f,\u0441\u0438\u043b\u044c\u043d\u043e \u043d\u0435 \u0431\u0435\u0439\u0442\u0435. \u0420\u0435\u0448\u0438\u043b \u043e\u0444\u043e\u0440\u043c\u0438\u0442\u044c \u043f\u043e\u0441\u043b\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u043e\u0442 <a class=\"mention\" href=\"\/users\/kinski\">@Kinski<\/a>\u0441 \u0431\u043e\u043b\u0435\u0435 \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u044b\u043c \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435\u043c \u0438 \u0440\u0430\u0441\u043a\u0440\u044b\u0442\u0438\u0435\u043c \u043d\u044e\u0430\u043d\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0440\u0430\u0445 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430 \u043d\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u043d\u0438\u0433\u0434\u0435.<br \/>\u0415\u0441\u043b\u0438 \u043a\u043e\u043c\u0443-\u0442\u043e \u0441\u0442\u0430\u0442\u044c\u044f \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442 \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 &#8212; \u0431\u0443\u0434\u0443 \u0440\u0430\u0434!<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/719934\/\"> https:\/\/habr.com\/ru\/post\/719934\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-346140","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/346140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=346140"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/346140\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=346140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=346140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=346140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}