{"id":330680,"date":"2022-03-15T15:00:55","date_gmt":"2022-03-15T15:00:55","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=330680"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2022-03-15T15:00:55","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=330680","title":{"rendered":"<span>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 Basic Authentication \u0441 RestTemplate \u0432 Spring<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/549\/691\/14c\/54969114cdfa1c4efe2fc8ea3440a892.png\" width=\"780\" height=\"439\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/549\/691\/14c\/54969114cdfa1c4efe2fc8ea3440a892.png\"\/><figcaption><\/figcaption><\/figure>\n<h3>1. \u041e\u0431\u0437\u043e\u0440<\/h3>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Spring&#8217;\u043e\u0432\u044b\u0439 <code>RestTemplate<\/code> \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 RESTful-\u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c\u0438, \u0437\u0430\u0449\u0438\u0449\u0435\u043d\u043d\u044b\u043c\u0438 Basic Authentication.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 <code>RestTemplate<\/code> \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 Basic Authenticatio<code>n<\/code> \u0432\u0441\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0431\u0443\u0434\u0443\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0443\u0447\u0435\u0442\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438. \u0414\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043a\u043e\u0434\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432 HTTP-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a <code>Authorization<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code>Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==<\/code><\/pre>\n<h3>2. \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 RestTemplate<\/h3>\n<p>\u0414\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f <code>RestTemplate<\/code> \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 Spring, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u0431\u044a\u044f\u0432\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a \u0431\u0438\u043d. \u041d\u043e Basic Authentication \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0440\u0443\u0447\u043d\u043e\u0439 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>FactoryBean<\/code>:<\/p>\n<pre><code class=\"java\">@Component public class RestTemplateFactory   implements FactoryBean&lt;RestTemplate>, InitializingBean {       private RestTemplate restTemplate;      public RestTemplate getObject() {         return restTemplate;     }     public Class&lt;RestTemplate> getObjectType() {         return RestTemplate.class;     }     public boolean isSingleton() {         return true;     }      public void afterPropertiesSet() {         HttpHost host = new HttpHost(\"localhost\", 8082, \"http\");         restTemplate = new RestTemplate(           new HttpComponentsClientHttpRequestFactoryBasicAuth(host));     } }<\/code><\/pre>\n<p>\u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b host \u0438 port \u043e\u0431\u044b\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u043e\u0442 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f: \u0443 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043e\u0434\u0438\u043d \u043d\u0430\u0431\u043e\u0440 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0433\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0430 \u0434\u0440\u0443\u0433\u043e\u0439 \u0434\u043b\u044f \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0430. \u042d\u0442\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 <a href=\"https:\/\/www.baeldung.com\/project-configuration-with-spring\"><u>\u0444\u0430\u0439\u043b\u044b \u0441\u0432\u043e\u0439\u0441\u0442\u0432<\/u><\/a>.<\/p>\n<h3>3. \u0420\u0443\u0447\u043d\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 HTTP-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u043e\u043c Authorization<\/h3>\n<p>\u0417\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a <code>Authorization<\/code> \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0440\u0443\u0447\u043d\u0443\u044e:<\/p>\n<pre><code class=\"java\">HttpHeaders createHeaders(String username, String password){    return new HttpHeaders() {{          String auth = username + \":\" + password;          byte[] encodedAuth = Base64.encodeBase64(              auth.getBytes(Charset.forName(\"US-ASCII\")) );          String authHeader = \"Basic \" + new String( encodedAuth );          set( \"Authorization\", authHeader );       }}; }<\/code><\/pre>\n<p>\u041e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u043e\u0441\u0442\u043e:<\/p>\n<pre><code class=\"java\">restTemplate.exchange  (uri, HttpMethod.POST, new HttpEntity&lt;T>(createHeaders(username, password)), clazz);<\/code><\/pre>\n<h3>4. \u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 HTTP-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u043e\u043c Authorization\u00a0<\/h3>\n<p>\u0412 Spring 3.0 \u0438 3.1, \u0430 \u0442\u0435\u043f\u0435\u0440\u044c \u0438 \u0432 4.x \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u0445\u043e\u0440\u043e\u0448\u0430\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a Apache HTTP:<\/p>\n<ul>\n<li>\n<p>\u0412 Spring 3.0 <code>CommonsClientHttpRequestFactory<\/code> \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u043d \u0441 \u043d\u044b\u043d\u0435 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0438\u043c HttpClient 3.x.<\/p>\n<\/li>\n<li>\n<p>\u0412 Spring 3.1 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e HttpClient 4.x \u0447\u0435\u0440\u0435\u0437 <code>HttpComponentsClientHttpRequestFactory<\/code> (JIRA SPR-6180).<\/p>\n<\/li>\n<li>\n<p>\u0412 Spring 4.0 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u0438 \u0447\u0435\u0440\u0435\u0437 <code>HttpComponentsAsyncClientHttpRequestFactory<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u0447\u043d\u0435\u043c \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0443 \u0441 HttpClient 4 \u0438 Spring 4.<\/p>\n<p>\u0414\u043b\u044f RestTemplate \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0444\u0430\u0431\u0440\u0438\u043a\u0430 HTTP-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0449\u0430\u044f Basic Authentication. \u041e\u0434\u043d\u0430\u043a\u043e \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 <code>HttpComponentsClientHttpRequestFactory<\/code> \u043d\u0435\u043f\u0440\u043e\u0441\u0442\u043e, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 <code>RestTemplate<\/code> \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 <code>HttpContext<\/code> \u2014 \u0432\u0430\u0436\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u044f. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u043e\u0434\u043a\u043b\u0430\u0441\u0441 <code>HttpComponentsClientHttpRequestFactory<\/code> \u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>createHttpContext<\/code>:<\/p>\n<pre><code class=\"java\">public class HttpComponentsClientHttpRequestFactoryBasicAuth    extends HttpComponentsClientHttpRequestFactory {      HttpHost host;      public HttpComponentsClientHttpRequestFactoryBasicAuth(HttpHost host) {         super();         this.host = host;     }      protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {         return createHttpContext();     }          private HttpContext createHttpContext() {         AuthCache authCache = new BasicAuthCache();          BasicScheme basicAuth = new BasicScheme();         authCache.put(host, basicAuth);          BasicHttpContext localcontext = new BasicHttpContext();         localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);         return localcontext;     } }<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 <code>HttpContext<\/code> \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 Basic Authentication. \u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e, \u0443\u043f\u0440\u0435\u0436\u0434\u0430\u044e\u0449\u0430\u044f Basic Authentication \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e HttpClient 4.x \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043e\u0431\u0440\u0435\u043c\u0435\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430. \u0418\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e\u0431 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043a\u044d\u0448\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u043d\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u044d\u0442\u043e\u0442 \u043a\u044d\u0448 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0441\u043b\u043e\u0436\u043d\u043e \u0438 \u043d\u0435 \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u043e.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c <code>BasicAuthorizationInterceptor<\/code> \u0432 RestTemplate:<\/p>\n<pre><code class=\"java\">restTemplate.getInterceptors().add(   new BasicAuthorizationInterceptor(\"username\", \"password\"));<\/code><\/pre>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441:<\/p>\n<pre><code class=\"java\">restTemplate.exchange(   \"http:\/\/localhost:8082\/spring-security-rest-basic-auth\/api\/foos\/1\",    HttpMethod.GET, null, Foo.class);<\/code><\/pre>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c \u0441\u0430\u043c\u043e\u0433\u043e REST-\u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0447\u0438\u0442\u0430\u0439\u0442\u0435 \u0432 <a href=\"https:\/\/www.baeldung.com\/basic-and-digest-authentication-for-a-rest-api-with-spring-security\"><u>\u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/u><\/a>.<\/p>\n<h3>5. \u0417\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 Maven<\/h3>\n<p>\u041d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 Maven \u0434\u043b\u044f \u0441\u0430\u043c\u043e\u0433\u043e RestTemplate \u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 HttpClient:<\/p>\n<pre><code class=\"xml\">&lt;dependency>    &lt;groupId>org.springframework&lt;\/groupId>    &lt;artifactId>spring-webmvc&lt;\/artifactId>    &lt;version>5.0.6.RELEASE&lt;\/version> &lt;\/dependency>  &lt;dependency>    &lt;groupId>org.apache.httpcomponents&lt;\/groupId>    &lt;artifactId>httpclient&lt;\/artifactId>    &lt;version>4.5.3&lt;\/version> &lt;\/dependency><\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u0440\u0435\u0448\u0438\u043c \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c HTTP-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a Authorization \u0432\u0440\u0443\u0447\u043d\u0443\u044e, \u0442\u043e \u043d\u0430\u043c \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"xml\">&lt;dependency>    &lt;groupId>commons-codec&lt;\/groupId>    &lt;artifactId>commons-codec&lt;\/artifactId>    &lt;version>1.10&lt;\/version> &lt;\/dependency><\/code><\/pre>\n<h3>6. \u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0411\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043f\u043e <code>RestTemplate<\/code> \u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438, \u0432\u0441\u0435 \u0435\u0449\u0435 \u043d\u0435 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0438\u0435 \u0440\u0435\u043b\u0438\u0437\u044b HttpClient 4.x, \u0434\u0430\u0436\u0435 \u043d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u0432\u0435\u0442\u043a\u0430 3.x \u0443\u0441\u0442\u0430\u0440\u0435\u043b\u0430 \u0438 Spring&#8217;\u043e\u043c \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u043d\u0438\u043b\u0438 \u044d\u0442\u043e\u0442 \u043f\u0440\u043e\u0431\u0435\u043b, \u043e\u043f\u0438\u0441\u0430\u0432, \u043a\u0430\u043a \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c Basic Authentication \u0434\u043b\u044f RestTemplate, \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0437\u0430\u0449\u0438\u0449\u0435\u043d\u043d\u043e\u043c\u0443 REST API.<\/p>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u0441 RESTful-\u0441\u0435\u0440\u0432\u0438\u0441\u043e\u043c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u0439\u0442\u0438 \u043d\u0430 <a href=\"https:\/\/github.com\/eugenp\/tutorials\/tree\/master\/spring-security-modules\/spring-security-web-rest-basic-auth\"><u>Github<\/u><\/a>.<\/p>\n<hr\/>\n<p>\u0412\u0441\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043f\u0440\u043e\u0445\u043e\u0434\u044f\u0442 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0439 \u043f\u0443\u0442\u044c \u0432 \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u0438. \u041f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u0432\u0441\u0435\u0445 \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0445 \u043d\u0430 demo-\u0437\u0430\u043d\u044f\u0442\u0438\u0435 \u00ab\u041f\u043e\u0441\u043b\u0430\u043d\u0438\u0435 \u043f\u0440\u043e \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438\u0306 \u0441\u0430\u043c\u043e\u043c\u0443 \u0441\u0435\u0431\u0435 \u0432 \u043f\u0440\u043e\u0448\u043b\u043e\u0435\u00bb, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043f\u0440\u0435\u043f\u043e\u0434\u0430\u0432\u0430\u0442\u0435\u043b\u044c OTUS \u0412\u0438\u0442\u0430\u043b\u0438\u0439 \u041a\u0443\u0446\u0435\u043d\u043a\u043e \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0435\u0442, \u043a\u0430\u043a \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043e\u0448\u0438\u0431\u043e\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0441\u0438\u043b\u044c\u043d\u043e \u0443\u0441\u043b\u043e\u0436\u043d\u0438\u0442\u044c \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f <a href=\"https:\/\/otus.pw\/WMNg\/\">\u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435.<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/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\/company\/otus\/blog\/655739\/\"> https:\/\/habr.com\/ru\/company\/otus\/blog\/655739\/<\/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_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h3>1. \u041e\u0431\u0437\u043e\u0440<\/h3>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Spring&#8217;\u043e\u0432\u044b\u0439 <code>RestTemplate<\/code> \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 RESTful-\u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c\u0438, \u0437\u0430\u0449\u0438\u0449\u0435\u043d\u043d\u044b\u043c\u0438 Basic Authentication.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 <code>RestTemplate<\/code> \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 Basic Authenticatio<code>n<\/code> \u0432\u0441\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0431\u0443\u0434\u0443\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0443\u0447\u0435\u0442\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438. \u0414\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043a\u043e\u0434\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432 HTTP-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a <code>Authorization<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code>Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==<\/code><\/pre>\n<h3>2. \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 RestTemplate<\/h3>\n<p>\u0414\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f <code>RestTemplate<\/code> \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 Spring, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u0431\u044a\u044f\u0432\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a \u0431\u0438\u043d. \u041d\u043e Basic Authentication \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0440\u0443\u0447\u043d\u043e\u0439 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>FactoryBean<\/code>:<\/p>\n<pre><code class=\"java\">@Component public class RestTemplateFactory   implements FactoryBean&lt;RestTemplate>, InitializingBean {       private RestTemplate restTemplate;      public RestTemplate getObject() {         return restTemplate;     }     public Class&lt;RestTemplate> getObjectType() {         return RestTemplate.class;     }     public boolean isSingleton() {         return true;     }      public void afterPropertiesSet() {         HttpHost host = new HttpHost(\"localhost\", 8082, \"http\");         restTemplate = new RestTemplate(           new HttpComponentsClientHttpRequestFactoryBasicAuth(host));     } }<\/code><\/pre>\n<p>\u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b host \u0438 port \u043e\u0431\u044b\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u043e\u0442 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f: \u0443 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043e\u0434\u0438\u043d \u043d\u0430\u0431\u043e\u0440 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0433\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0430 \u0434\u0440\u0443\u0433\u043e\u0439 \u0434\u043b\u044f \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0430. \u042d\u0442\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 <a href=\"https:\/\/www.baeldung.com\/project-configuration-with-spring\"><u>\u0444\u0430\u0439\u043b\u044b \u0441\u0432\u043e\u0439\u0441\u0442\u0432<\/u><\/a>.<\/p>\n<h3>3. \u0420\u0443\u0447\u043d\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 HTTP-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u043e\u043c Authorization<\/h3>\n<p>\u0417\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a <code>Authorization<\/code> \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0440\u0443\u0447\u043d\u0443\u044e:<\/p>\n<pre><code class=\"java\">HttpHeaders createHeaders(String username, String password){    return new HttpHeaders() {{          String auth = username + \":\" + password;          byte[] encodedAuth = Base64.encodeBase64(              auth.getBytes(Charset.forName(\"US-ASCII\")) );          String authHeader = \"Basic \" + new String( encodedAuth );          set( \"Authorization\", authHeader );       }}; }<\/code><\/pre>\n<p>\u041e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u043e\u0441\u0442\u043e:<\/p>\n<pre><code class=\"java\">restTemplate.exchange  (uri, HttpMethod.POST, new HttpEntity&lt;T>(createHeaders(username, password)), clazz);<\/code><\/pre>\n<h3>4. \u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 HTTP-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u043e\u043c Authorization\u00a0<\/h3>\n<p>\u0412 Spring 3.0 \u0438 3.1, \u0430 \u0442\u0435\u043f\u0435\u0440\u044c \u0438 \u0432 4.x \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u0445\u043e\u0440\u043e\u0448\u0430\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a Apache HTTP:<\/p>\n<ul>\n<li>\n<p>\u0412 Spring 3.0 <code>CommonsClientHttpRequestFactory<\/code> \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u043d \u0441 \u043d\u044b\u043d\u0435 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0438\u043c HttpClient 3.x.<\/p>\n<\/li>\n<li>\n<p>\u0412 Spring 3.1 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e HttpClient 4.x \u0447\u0435\u0440\u0435\u0437 <code>HttpComponentsClientHttpRequestFactory<\/code> (JIRA SPR-6180).<\/p>\n<\/li>\n<li>\n<p>\u0412 Spring 4.0 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u0438 \u0447\u0435\u0440\u0435\u0437 <code>HttpComponentsAsyncClientHttpRequestFactory<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u0447\u043d\u0435\u043c \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0443 \u0441 HttpClient 4 \u0438 Spring 4.<\/p>\n<p>\u0414\u043b\u044f RestTemplate \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0444\u0430\u0431\u0440\u0438\u043a\u0430 HTTP-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0449\u0430\u044f Basic Authentication. \u041e\u0434\u043d\u0430\u043a\u043e \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 <code>HttpComponentsClientHttpRequestFactory<\/code> \u043d\u0435\u043f\u0440\u043e\u0441\u0442\u043e, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 <code>RestTemplate<\/code> \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 <code>HttpContext<\/code> \u2014 \u0432\u0430\u0436\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u044f. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u043e\u0434\u043a\u043b\u0430\u0441\u0441 <code>HttpComponentsClientHttpRequestFactory<\/code> \u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>createHttpContext<\/code>:<\/p>\n<pre><code class=\"java\">public class HttpComponentsClientHttpRequestFactoryBasicAuth    extends HttpComponentsClientHttpRequestFactory {      HttpHost host;      public HttpComponentsClientHttpRequestFactoryBasicAuth(HttpHost host) {         super();         this.host = host;     }      protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {         return createHttpContext();     }          private HttpContext createHttpContext() {         AuthCache authCache = new BasicAuthCache();          BasicScheme basicAuth = new BasicScheme();         authCache.put(host, basicAuth);          BasicHttpContext localcontext = new BasicHttpContext();         localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);         return localcontext;     } }<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 <code>HttpContext<\/code> \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 Basic Authentication. \u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e, \u0443\u043f\u0440\u0435\u0436\u0434\u0430\u044e\u0449\u0430\u044f Basic Authentication \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e HttpClient 4.x \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043e\u0431\u0440\u0435\u043c\u0435\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430. \u0418\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e\u0431 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043a\u044d\u0448\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u043d\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u044d\u0442\u043e\u0442 \u043a\u044d\u0448 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0441\u043b\u043e\u0436\u043d\u043e \u0438 \u043d\u0435 \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u043e.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c <code>BasicAuthorizationInterceptor<\/code> \u0432 RestTemplate:<\/p>\n<pre><code class=\"java\">restTemplate.getInterceptors().add(   new BasicAuthorizationInterceptor(\"username\", \"password\"));<\/code><\/pre>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441:<\/p>\n<pre><code class=\"java\">restTemplate.exchange(   \"http:\/\/localhost:8082\/spring-security-rest-basic-auth\/api\/foos\/1\",    HttpMethod.GET, null, Foo.class);<\/code><\/pre>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c \u0441\u0430\u043c\u043e\u0433\u043e REST-\u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0447\u0438\u0442\u0430\u0439\u0442\u0435 \u0432 <a href=\"https:\/\/www.baeldung.com\/basic-and-digest-authentication-for-a-rest-api-with-spring-security\"><u>\u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/u><\/a>.<\/p>\n<h3>5. \u0417\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 Maven<\/h3>\n<p>\u041d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 Maven \u0434\u043b\u044f \u0441\u0430\u043c\u043e\u0433\u043e RestTemplate \u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 HttpClient:<\/p>\n<pre><code class=\"xml\">&lt;dependency>    &lt;groupId>org.springframework&lt;\/groupId>    &lt;artifactId>spring-webmvc&lt;\/artifactId>    &lt;version>5.0.6.RELEASE&lt;\/version> &lt;\/dependency>  &lt;dependency>    &lt;groupId>org.apache.httpcomponents&lt;\/groupId>    &lt;artifactId>httpclient&lt;\/artifactId>    &lt;version>4.5.3&lt;\/version> &lt;\/dependency><\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u0440\u0435\u0448\u0438\u043c \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c HTTP-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a Authorization \u0432\u0440\u0443\u0447\u043d\u0443\u044e, \u0442\u043e \u043d\u0430\u043c \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"xml\">&lt;dependency>    &lt;groupId>commons-codec&lt;\/groupId>    &lt;artifactId>commons-codec&lt;\/artifactId>    &lt;version>1.10&lt;\/version> &lt;\/dependency><\/code><\/pre>\n<h3>6. \u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0411\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043f\u043e <code>RestTemplate<\/code> \u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438, \u0432\u0441\u0435 \u0435\u0449\u0435 \u043d\u0435 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0438\u0435 \u0440\u0435\u043b\u0438\u0437\u044b HttpClient 4.x, \u0434\u0430\u0436\u0435 \u043d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u0432\u0435\u0442\u043a\u0430 3.x \u0443\u0441\u0442\u0430\u0440\u0435\u043b\u0430 \u0438 Spring&#8217;\u043e\u043c \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u043d\u0438\u043b\u0438 \u044d\u0442\u043e\u0442 \u043f\u0440\u043e\u0431\u0435\u043b, \u043e\u043f\u0438\u0441\u0430\u0432, \u043a\u0430\u043a \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c Basic Authentication \u0434\u043b\u044f RestTemplate, \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0437\u0430\u0449\u0438\u0449\u0435\u043d\u043d\u043e\u043c\u0443 REST API.<\/p>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u0441 RESTful-\u0441\u0435\u0440\u0432\u0438\u0441\u043e\u043c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u0439\u0442\u0438 \u043d\u0430 <a href=\"https:\/\/github.com\/eugenp\/tutorials\/tree\/master\/spring-security-modules\/spring-security-web-rest-basic-auth\"><u>Github<\/u><\/a>.<\/p>\n<hr\/>\n<p>\u0412\u0441\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043f\u0440\u043e\u0445\u043e\u0434\u044f\u0442 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0439 \u043f\u0443\u0442\u044c \u0432 \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u0438. \u041f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u0432\u0441\u0435\u0445 \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0445 \u043d\u0430 demo-\u0437\u0430\u043d\u044f\u0442\u0438\u0435 \u00ab\u041f\u043e\u0441\u043b\u0430\u043d\u0438\u0435 \u043f\u0440\u043e \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438\u0306 \u0441\u0430\u043c\u043e\u043c\u0443 \u0441\u0435\u0431\u0435 \u0432 \u043f\u0440\u043e\u0448\u043b\u043e\u0435\u00bb, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043f\u0440\u0435\u043f\u043e\u0434\u0430\u0432\u0430\u0442\u0435\u043b\u044c OTUS \u0412\u0438\u0442\u0430\u043b\u0438\u0439 \u041a\u0443\u0446\u0435\u043d\u043a\u043e \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0435\u0442, \u043a\u0430\u043a \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043e\u0448\u0438\u0431\u043e\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0441\u0438\u043b\u044c\u043d\u043e \u0443\u0441\u043b\u043e\u0436\u043d\u0438\u0442\u044c \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f <a href=\"https:\/\/otus.pw\/WMNg\/\">\u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435.<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/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\/company\/otus\/blog\/655739\/\"> https:\/\/habr.com\/ru\/company\/otus\/blog\/655739\/<\/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-330680","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/330680","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=330680"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/330680\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=330680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=330680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=330680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}