{"id":319059,"date":"2021-03-04T21:00:09","date_gmt":"2021-03-04T21:00:09","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=319059"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=319059","title":{"rendered":"REST API \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Spring Security \u0438 JWT"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u0420\u0430\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u0437\u0434\u043d\u043e \u043a\u0430\u0436\u0434\u044b\u0439 Java-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0441\u0442\u043e\u043b\u043a\u043d\u0435\u0442\u0441\u044f \u0441 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0449\u0438\u0449\u0435\u043d\u043d\u043e\u0435 REST API \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0445\u043e\u0447\u0443 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0441\u0432\u043e\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<h2>1. \u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 REST?<\/h2>\n<p>REST (\u043e\u0442 \u0430\u043d\u0433\u043b. Representational State Transfer \u2014 \u00ab\u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u00bb) \u2013 \u044d\u0442\u043e \u043e\u0431\u0449\u0438\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\/\u0441\u0430\u0439\u0442\u0430 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c \u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e\u043c \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 HTTP.<\/p>\n<p>\u0414\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043d\u0438\u0436\u0435 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043e\u0431\u0449\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f0c\/9d3\/ebb\/f0c9d3ebb45cc58d0810415748a2c4b7.png\" width=\"974\" height=\"647\"><figcaption><\/figcaption><\/figure>\n<p>\u0412\u0441\u0451 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c \u0441\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043a 4 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c (4 \u2014 \u044d\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0439 \u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u044b\u0439 \u043c\u0438\u043d\u0438\u043c\u0443\u043c, \u0432 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0442\u0438\u043f\u043e\u0432 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435):<\/p>\n<ol>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 (\u043e\u0431\u044b\u0447\u043d\u043e \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 JSON, \u0438\u043b\u0438 XML);<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440;<\/p>\n<\/li>\n<li>\n<p>\u041c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435;<\/p>\n<\/li>\n<li>\n<p>\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435<\/p>\n<\/li>\n<\/ol>\n<p>\u0411\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0447\u0435\u0441\u0442\u044c \u0432 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430\u0445, \u0441\u0442\u0430\u0442\u0435\u0439 \u043e REST \u043c\u043d\u043e\u0433\u043e.<\/p>\n<h2>2. \u0417\u0430\u0434\u0430\u0447\u0430<\/h2>\n<p>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c \u0437\u0430\u0449\u0438\u0449\u0435\u043d\u043d\u043e\u0435 REST \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0441 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435\u0439 \u043b\u043e\u0433\u0438\u043d\u0430 \u0438 \u043f\u0430\u0440\u043e\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c, \u043f\u0440\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0439 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u043a\u0435\u043d. \u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u0435\u043d\u0430 \u043f\u043e \u0442\u043e\u043a\u0435\u043d\u0443.<br \/> \u0421\u0445\u0435\u043c\u0430 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d31\/d3c\/95c\/d31d3c95c8eeb2fb1d86cfc8bac6a250.png\" width=\"974\" height=\"691\"><figcaption><\/figcaption><\/figure>\n<h2>3. \u0422\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438<\/h2>\n<p>\u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a Spring Boot \u0438 Spring Web, \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f:<\/p>\n<ol>\n<li>\n<p>Java 8+;<\/p>\n<\/li>\n<li>\n<p>Apache Maven<\/p>\n<\/li>\n<\/ol>\n<p>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u0441\u0438\u043b\u0430\u043c\u0438 Spring Security \u0438 JsonWebToken (JWT).<br \/> \u0414\u043b\u044f \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e Lombok.<\/p>\n<h2>4. \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/h2>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435. \u0421\u043e\u0437\u0434\u0430\u0435\u043c Spring Boot \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0435 REST API \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439.<\/p>\n<h2>4.1 \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 Web-\u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/h2>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c Maven-\u043f\u0440\u043e\u0435\u043a\u0442 SpringBootSecurityRest. \u041f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u0435\u0441\u043b\u0438 \u0432\u044b \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0435 \u0447\u0435\u0440\u0435\u0437 Intellij IDEA, \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 Spring Boot DevTools, Lombok \u0438 Spring Web, \u0438\u043d\u0430\u0447\u0435 \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0432 pom-\u0444\u0430\u0439\u043b\u0435.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b7d\/d22\/77c\/b7dd2277c082ef91f00422dbe3f37d55.png\" width=\"474\" height=\"249\"><figcaption><\/figcaption><\/figure>\n<h2>4.2 \u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f pom-xml<\/h2>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0440\u0430\u0437\u0432\u0435\u0440\u0442\u044b\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u0435\u043a\u0442\u0430 pom-\u0444\u0430\u0439\u043b \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<ol>\n<li>\n<p>\u0414\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u043d parent-\u0441\u0435\u0433\u043c\u0435\u043d\u0442 \u0441 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u043d\u044b\u043c spring-boot-starter-parent;<\/p>\n<\/li>\n<li>\n<p>\u0418 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u044b \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 spring-boot-starter-web, spring-boot-devtools \u0438 Lombok.<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"java\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt; &lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"          xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 https:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;     &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;     &lt;parent&gt;         &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;         &lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt;         &lt;version&gt;2.3.5.RELEASE&lt;\/version&gt;         &lt;relativePath\/&gt; &lt;!-- lookup parent from repository --&gt;     &lt;\/parent&gt;     &lt;groupId&gt;com&lt;\/groupId&gt;     &lt;artifactId&gt;springbootsecurityrest&lt;\/artifactId&gt;     &lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;     &lt;name&gt;springbootsecurityrest&lt;\/name&gt;     &lt;description&gt;Demo project for Spring Boot&lt;\/description&gt;     &lt;properties&gt;         &lt;java.version&gt;15&lt;\/java.version&gt;     &lt;\/properties&gt;     &lt;dependencies&gt;         &lt;dependency&gt;             &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;             &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;         &lt;\/dependency&gt;          &lt;dependency&gt;             &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;             &lt;artifactId&gt;spring-boot-devtools&lt;\/artifactId&gt;             &lt;scope&gt;runtime&lt;\/scope&gt;             &lt;optional&gt;true&lt;\/optional&gt;         &lt;\/dependency&gt;         &lt;dependency&gt;             &lt;groupId&gt;org.projectlombok&lt;\/groupId&gt;             &lt;artifactId&gt;lombok&lt;\/artifactId&gt;             &lt;optional&gt;true&lt;\/optional&gt;         &lt;\/dependency&gt;         &lt;!--Test--&gt;         &lt;dependency&gt;             &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;             &lt;artifactId&gt;spring-boot-starter-test&lt;\/artifactId&gt;             &lt;scope&gt;test&lt;\/scope&gt;             &lt;exclusions&gt;                 &lt;exclusion&gt;                     &lt;groupId&gt;org.junit.vintage&lt;\/groupId&gt;                     &lt;artifactId&gt;junit-vintage-engine&lt;\/artifactId&gt;                 &lt;\/exclusion&gt;             &lt;\/exclusions&gt;         &lt;\/dependency&gt;     &lt;\/dependencies&gt;      &lt;build&gt;         &lt;plugins&gt;             &lt;plugin&gt;                 &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;                 &lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt;                 &lt;configuration&gt;                     &lt;excludes&gt;                         &lt;exclude&gt;                             &lt;groupId&gt;org.projectlombok&lt;\/groupId&gt;                             &lt;artifactId&gt;lombok&lt;\/artifactId&gt;                         &lt;\/exclude&gt;                     &lt;\/excludes&gt;                 &lt;\/configuration&gt;             &lt;\/plugin&gt;         &lt;\/plugins&gt;     &lt;\/build&gt;  &lt;\/project&gt;<\/code><\/pre>\n<h2>4.3 \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u0430 REST<\/h2>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u0438\u043c \u0432\u0441\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u043d\u0430 \u0441\u043b\u043e\u0438, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0432 \u043f\u0430\u043f\u043a\u0435 com.springbootsecurityrest \u0447\u0435\u0442\u044b\u0440\u0435 \u043d\u043e\u0432\u044b\u0435 \u043f\u0430\u043f\u043a\u0438:<\/p>\n<ul>\n<li>\n<p>model \u2013 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f POJO-\u043a\u043b\u0430\u0441\u0441\u043e\u0432;<\/p>\n<\/li>\n<li>\n<p>repository \u2013 \u0432 \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441 \u0411\u0414, \u043d\u043e \u0442.\u043a. \u0443 \u043d\u0430\u0441 \u0435\u0435 \u043d\u0435\u0442, \u0442\u043e \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439;<\/p>\n<\/li>\n<li>\n<p>service \u2013 \u0441\u043b\u043e\u0439 \u0441\u0435\u0440\u0432\u0438\u0441\u0430, \u043f\u0440\u043e\u0441\u043b\u043e\u0439\u043a\u0430 \u043c\u0435\u0436\u0434\u0443 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0435\u0440\u043e\u043c \u0438 \u0441\u043b\u043e\u0435\u043c \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0440\u0435\u0441\u0443\u0440\u0441\u0430, \u0438\u0445 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f (\u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e);<\/p>\n<\/li>\n<li>\n<p>rest \u2013 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432 \u0441\u0435\u0431\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b.<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u043f\u0430\u043f\u043a\u0435 model \u0441\u043e\u0437\u0434\u0430\u0435\u043c POJO \u043a\u043b\u0430\u0441\u0441 User.<\/p>\n<pre><code class=\"java\">import lombok.AllArgsConstructor; import lombok.Data;  @Data @AllArgsConstructor public class User {     private String login;     private String password;     private String firstname;     private String lastname;     private Integer age; }<\/code><\/pre>\n<p>\u0412 \u043f\u0430\u043f\u043a\u0435 repository \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u043a\u043b\u0430\u0441\u0441 UserRepository c \u0434\u0432\u0443\u043c\u044f \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438:<\/p>\n<ol>\n<li>\n<p>getByLogin \u2013 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e \u043b\u043e\u0433\u0438\u043d\u0443;<\/p>\n<\/li>\n<li>\n<p>getAll \u2013 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439. \u0427\u0442\u043e\u0431\u044b Spring \u0441\u043e\u0437\u0434\u0430\u043b \u0431\u0438\u043d \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 \u044d\u0442\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430, \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0435\u043c\u0443 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e @Repository.<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"java\">@Repository public class UserRepository {        private List&lt;User&gt; users;      public UserRepository() {         this.users = List.of(                 new User(\"anton\", \"1234\", \"\u0410\u043d\u0442\u043e\u043d\", \"\u0418\u0432\u0430\u043d\u043e\u0432\", 20),                 new User(\"ivan\", \"12345\", \"\u0421\u0435\u0440\u0433\u0435\u0439\", \"\u041f\u0435\u0442\u0440\u043e\u0432\", 21));     }      public User getByLogin(String login) {         return this.users.stream()                 .filter(user -&gt; login.equals(user.getLogin()))                 .findFirst()                 .orElse(null);     }      public List&lt;User&gt; getAll() {         return this.users;     }<\/code><\/pre>\n<p>\u0412 \u043f\u0430\u043f\u043a\u0435 service \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441 UserService. \u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441\u0443 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e @Service \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0438\u043d\u044a\u0435\u043a\u0446\u0438\u044e \u0431\u0438\u043d\u0430 UserRepository. \u0412 \u043a\u043b\u0430\u0441\u0441 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043c\u0435\u0442\u043e\u0434 getAll, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0432\u0441\u0435\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 getByLogin \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e \u043b\u043e\u0433\u0438\u043d\u0443. <\/p>\n<pre><code class=\"java\">@Service public class UserService {      private UserRepository repository;      public UserService(UserRepository repository) {         this.repository = repository;     }      public List&lt;User&gt; getAll() {         return this.repository.getAll();     }      public User getByLogin(String login) {         return this.repository.getByLogin(login);     } }<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 UserController \u0432 \u043f\u0430\u043f\u043a\u0435 rest, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u043c\u0443 \u0438\u043d\u044a\u0435\u043a\u0446\u0438\u044e UserService \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043e\u0434\u0438\u043d \u043c\u0435\u0442\u043e\u0434 getAll. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 @GetMapping \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0430\u0434\u0440\u0435\u0441 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u0438 \u0442\u0438\u043f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"java\">@RestController public class UserController {      private UserService service;      public UserController(UserService service) {         this.service = service;     }      @GetMapping(path = \"\/users\", produces = MediaType.APPLICATION_JSON_VALUE)     public @ResponseBody List&lt;User&gt; getAll() {         return this.service.getAll();     } }<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e \u043e\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0430\u0434\u0440\u0435\u0441 http:\/\/localhost:8080\/users, \u0435\u0441\u043b\u0438 \u0432\u044b \u0432\u0441\u0435 \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u0432\u0435\u0440\u043d\u043e, \u0442\u043e \u0443\u0432\u0438\u0434\u0438\u0442\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7cf\/5b6\/c94\/7cf5b6c940042997ec294d45bc2272d2.png\" width=\"516\" height=\"477\"><figcaption><\/figcaption><\/figure>\n<h2>5. Spring Security<\/h2>\n<p>\u041f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u043e\u0435 REST API \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u0438 \u043f\u043e\u043a\u0430 \u043e\u043d\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u043e \u0434\u043b\u044f \u0432\u0441\u0435\u0445. \u0414\u0432\u0438\u0433\u0430\u0435\u043c\u0441\u044f \u0434\u0430\u043b\u044c\u0448\u0435, \u0442\u0435\u043f\u0435\u0440\u044c \u0435\u0433\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u0449\u0438\u0442\u0438\u0442\u044c, \u0430 \u0434\u043e\u0441\u0442\u0443\u043f \u043e\u0442\u043a\u0440\u044b\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f Spring Security \u0438 JWT.<\/p>\n<p>Spring Security \u044d\u0442\u043e Java\/JavaEE framework, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0441\u0438\u0441\u0442\u0435\u043c \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0434\u0440\u0443\u0433\u0438\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u044f \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043a\u043e\u0440\u043f\u043e\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0445 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Spring Framework.<\/p>\n<p>JSON Web Token (JWT) \u2014 \u044d\u0442\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442 (RFC 7519) \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u0434\u043e\u0441\u0442\u0443\u043f\u0430, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 JSON. \u041a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u043e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0432 \u043a\u043b\u0438\u0435\u043d\u0442-\u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445. \u0422\u043e\u043a\u0435\u043d\u044b \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c, \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0441\u0435\u043a\u0440\u0435\u0442\u043d\u044b\u043c \u043a\u043b\u044e\u0447\u043e\u043c \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0439 \u0442\u043e\u043a\u0435\u043d \u0434\u043b\u044f \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u044f \u0441\u0432\u043e\u0435\u0439 \u043b\u0438\u0447\u043d\u043e\u0441\u0442\u0438.<\/p>\n<h2>5.1 \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438<\/h2>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u043e\u0432\u044b\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u0432 pom-\u0444\u0430\u0439\u043b.<\/p>\n<pre><code class=\"java\">&lt;!--Security--&gt; &lt;dependency&gt;     &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;     &lt;artifactId&gt;spring-boot-starter-security&lt;\/artifactId&gt; &lt;\/dependency&gt; &lt;dependency&gt;     &lt;groupId&gt;io.jsonwebtoken&lt;\/groupId&gt;     &lt;artifactId&gt;jjwt&lt;\/artifactId&gt;     &lt;version&gt;0.9.1&lt;\/version&gt; &lt;\/dependency&gt; &lt;dependency&gt;     &lt;groupId&gt;jakarta.xml.bind&lt;\/groupId&gt;     &lt;artifactId&gt;jakarta.xml.bind-api&lt;\/artifactId&gt;     &lt;version&gt;2.3.3&lt;\/version&gt; &lt;\/dependency&gt;<\/code><\/pre>\n<h2>5.2 \u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0438 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u0430<\/h2>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u0430, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0430\u043f\u043a\u0443 security \u0438 \u0432 \u043d\u0435\u0439 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441 JwtTokenRepository \u0441 \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0435\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 CsrfTokenRepository (\u0438\u0437 \u043f\u0430\u043a\u0435\u0442\u0430 org.springframework.security.web.csrf).<\/p>\n<p>\u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0440\u0438 \u043c\u0435\u0442\u043e\u0434\u0430:<\/p>\n<ol>\n<li>\n<p>\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u0430 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 generateToken;<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u0430 \u2013 saveToken;<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0442\u043e\u043a\u0435\u043d\u0430 \u2013 loadToken.<\/p>\n<\/li>\n<\/ol>\n<p>\u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0442\u043e\u043a\u0435\u043d \u0441\u0438\u043b\u0430\u043c\u0438 Jwt, \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u0435\u0442\u043e\u0434\u0430.<\/p>\n<pre><code class=\"java\">@Repository public class JwtTokenRepository implements CsrfTokenRepository {      @Getter     private String secret;      public JwtTokenRepository() {         this.secret = \"springrest\";     }      @Override     public CsrfToken generateToken(HttpServletRequest httpServletRequest) {         String id = UUID.randomUUID().toString().replace(\"-\", \"\");         Date now = new Date();         Date exp = Date.from(LocalDateTime.now().plusMinutes(30)                 .atZone(ZoneId.systemDefault()).toInstant());          String token = \"\";         try {             token = Jwts.builder()                     .setId(id)                     .setIssuedAt(now)                     .setNotBefore(now)                     .setExpiration(exp)                     .signWith(SignatureAlgorithm.HS256, secret)                     .compact();         } catch (JwtException e) {             e.printStackTrace();             \/\/ignore         }         return new DefaultCsrfToken(\"x-csrf-token\", \"_csrf\", token);     }      @Override     public void saveToken(CsrfToken csrfToken, HttpServletRequest request, HttpServletResponse response) {     }      @Override     public CsrfToken loadToken(HttpServletRequest request) {         return null;     } }<\/code><\/pre>\n<p>\u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 secret \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043b\u044e\u0447\u043e\u043c, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u043c \u0434\u043b\u044f \u0440\u0430\u0441\u0448\u0438\u0444\u0440\u043e\u0432\u043a\u0438 \u0442\u043e\u043a\u0435\u043d\u0430, \u043e\u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u043c \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0442\u043e\u043a\u0435\u043d\u043e\u0432, \u043d\u043e \u043b\u0443\u0447\u0448\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0435\u0433\u043e \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c ip-\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u043b\u0438 \u0435\u0433\u043e \u043b\u043e\u0433\u0438\u043d. \u0414\u0430\u0442\u0430 exp \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0430\u0442\u043e\u0439 \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u0430, \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u0442\u0435\u043a\u0443\u0449\u0430\u044f \u0434\u0430\u0442\u0430 \u043f\u043b\u044e\u0441 30 \u043c\u0438\u043d\u0443\u0442. \u0422\u0430\u043a\u043e\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u043a\u0430\u043a \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0436\u0438\u0437\u043d\u0438 \u0442\u043e\u043a\u0435\u043d\u0430 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u0432 application.properties.<\/p>\n<p>\u0422\u043e\u043a\u0435\u043d \u0431\u0443\u0434\u0435\u0442 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u043e\u0432\u044b\u0439 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u0441 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u044b\u043c \u0446\u0438\u043a\u043b\u043e\u043c \u0432 30 \u043c\u0438\u043d\u0443\u0442. \u041f\u043e\u0441\u043b\u0435 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0430 \u0444\u0440\u043e\u043d\u0442\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043a\u0435\u043d \u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0441 \u043d\u043e\u0432\u044b\u043c. \u041e\u043d \u0441\u0442\u0430\u043d\u0435\u0442 \u043d\u0435\u0432\u0430\u043b\u0438\u0434\u043d\u044b\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u043c\u0435\u0436\u0434\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c\u0438 \u043f\u0440\u043e\u0439\u0434\u0435\u0442 \u0431\u043e\u043b\u0435\u0435 30 \u043c\u0438\u043d\u0443\u0442. <\/p>\n<pre><code class=\"java\">    @Override     public void saveToken(CsrfToken csrfToken, HttpServletRequest request, HttpServletResponse response) {         if (Objects.nonNull(csrfToken)) {             if (!response.getHeaderNames().contains(ACCESS_CONTROL_EXPOSE_HEADERS))                 response.addHeader(ACCESS_CONTROL_EXPOSE_HEADERS, csrfToken.getHeaderName());              if (response.getHeaderNames().contains(csrfToken.getHeaderName()))                 response.setHeader(csrfToken.getHeaderName(), csrfToken.getToken());             else                 response.addHeader(csrfToken.getHeaderName(), csrfToken.getToken());         }     }      @Override     public CsrfToken loadToken(HttpServletRequest request) {         return (CsrfToken) request.getAttribute(CsrfToken.class.getName());     }<\/code><\/pre>\n<p>\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0442\u043e\u043a\u0435\u043d\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0432 response (\u043e\u0442\u0432\u0435\u0442 \u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430) \u0432 \u0440\u0430\u0437\u0434\u0435\u043b headers \u0438 \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0444\u0440\u043e\u043d\u0442\u0430 \u0443\u043a\u0430\u0437\u0430\u0432 \u0438\u043c\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u0432 Access-Control-Expose-Headers.<\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043a \u043a\u043b\u0430\u0441\u0441\u0443 \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043c\u0435\u0442\u043e\u0434 \u043f\u043e \u043e\u0447\u0438\u0441\u0442\u043a\u0435 \u0442\u043e\u043a\u0435\u043d\u0430 \u0438\u0437 response, \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u043f\u0440\u0438 \u043e\u0448\u0438\u0431\u043a\u0435 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<pre><code class=\"java\">    public void clearToken(HttpServletResponse response) {         if (response.getHeaderNames().contains(\"x-csrf-token\"))             response.setHeader(\"x-csrf-token\", \"\");     }<\/code><\/pre>\n<h2>5.3 \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u0444\u0438\u043b\u044c\u0442\u0440\u0430 \u0434\u043b\u044f SpringSecurity<\/h2>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 JwtCsrfFilter, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 OncePerRequestFilter (\u043f\u0430\u043a\u0435\u0442 org.springframework.web.filter). \u041a\u043b\u0430\u0441\u0441 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0442\u043e\u043a\u0435\u043d\u0430 \u0438 \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e. \u0415\u0441\u043b\u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u043a \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 (\u043f\u0443\u0442\u044c \/auth\/login), \u0442\u043e \u043b\u043e\u0433\u0438\u043a\u0430 \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0438 \u0437\u0430\u043f\u0440\u043e\u0441 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0430\u043b\u0435\u0435 \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<pre><code class=\"java\">public class JwtCsrfFilter extends OncePerRequestFilter {      private final CsrfTokenRepository tokenRepository;      private final HandlerExceptionResolver resolver;      public JwtCsrfFilter(CsrfTokenRepository tokenRepository, HandlerExceptionResolver resolver) {         this.tokenRepository = tokenRepository;         this.resolver = resolver;     }      @Override     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)             throws ServletException, IOException {         request.setAttribute(HttpServletResponse.class.getName(), response);         CsrfToken csrfToken = this.tokenRepository.loadToken(request);         boolean missingToken = csrfToken == null;         if (missingToken) {             csrfToken = this.tokenRepository.generateToken(request);             this.tokenRepository.saveToken(csrfToken, request, response);         }          request.setAttribute(CsrfToken.class.getName(), csrfToken);         request.setAttribute(csrfToken.getParameterName(), csrfToken);         if (request.getServletPath().equals(\"\/auth\/login\")) {             try {                 filterChain.doFilter(request, response);             } catch (Exception e) {                 resolver.resolveException(request, response, null, new MissingCsrfTokenException(csrfToken.getToken()));             }         } else {             String actualToken = request.getHeader(csrfToken.getHeaderName());             if (actualToken == null) {                 actualToken = request.getParameter(csrfToken.getParameterName());             }             try {                 if (!StringUtils.isEmpty(actualToken)) {                     Jwts.parser()                             .setSigningKey(((JwtTokenRepository) tokenRepository).getSecret())                             .parseClaimsJws(actualToken);                          filterChain.doFilter(request, response);                 } else                     resolver.resolveException(request, response, null, new InvalidCsrfTokenException(csrfToken, actualToken));             } catch (JwtException e) {                 if (this.logger.isDebugEnabled()) {                     this.logger.debug(\"Invalid CSRF token found for \" + UrlUtils.buildFullRequestUrl(request));                 }                  if (missingToken) {                     resolver.resolveException(request, response, null, new MissingCsrfTokenException(actualToken));                 } else {                     resolver.resolveException(request, response, null, new InvalidCsrfTokenException(csrfToken, actualToken));                 }             }         }     } }<\/code><\/pre>\n<h2>5.4 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f<\/h2>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e \u043b\u043e\u0433\u0438\u043d\u0443, \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u043c \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a \u0441\u0435\u0440\u0432\u0438\u0441\u0443 UserService  \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 UserDetailsService \u0438\u0437 \u043f\u0430\u043a\u0435\u0442\u0430 org.springframework.security.core.userdetails. \u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u043c\u0435\u0442\u043e\u0434, \u0432\u044b\u043d\u043e\u0441\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u043d\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438.<\/p>\n<pre><code class=\"java\">@Service public class UserService implements UserDetailsService {      private UserRepository repository;      public UserService(UserRepository repository) {         this.repository = repository;     }      public List&lt;User&gt; getAll() {         return this.repository.getAll();     }      public User getByLogin(String login) {         return this.repository.getByLogin(login);     }      @Override     public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException {         User u = getByLogin(login);         if (Objects.isNull(u)) {             throw new UsernameNotFoundException(String.format(\"User %s is not found\", login));         }         return new org.springframework.security.core.userdetails.User(u.getLogin(), u.getPassword(), true, true, true, true, new HashSet&lt;&gt;());     } }<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043a\u043b\u0430\u0441\u0441 \u0441 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 UserDetails \u0438\u043b\u0438 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0443\u0436\u0435 \u0433\u043e\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u0438\u0437 \u043f\u0430\u043a\u0435\u0442\u0430 org.springframework.security.core.userdetails. \u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 GrantedAuthority, \u044d\u0442\u043e \u0440\u043e\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0443 \u043d\u0430\u0441 \u0438\u0445 \u043d\u0435\u0442, \u043e\u0441\u0442\u0430\u0432\u0438\u043c \u0435\u0433\u043e \u043f\u0443\u0441\u0442\u044b\u043c. \u0415\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e \u043b\u043e\u0433\u0438\u043d\u0443 \u043d\u0435 \u043d\u0430\u0448\u043b\u0438, \u0442\u043e \u0431\u0440\u043e\u0441\u0430\u0435\u043c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 UsernameNotFoundException.<\/p>\n<h2>5.5 \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 <\/h2>\n<p>\u041f\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0443 \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u043e\u0439 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e \u0434\u0430\u043d\u043d\u044b\u0435 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 AuthController \u0441 \u043c\u0435\u0442\u043e\u0434\u043e\u043c getAuthUser. \u041a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \/auth\/login, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0443 Security \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043b\u043e\u0433\u0438\u043d\u0430 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043f\u043e \u043d\u0435\u043c\u0443 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u0437 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 UserService \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0438\u0445 \u043d\u0430 \u0444\u0440\u043e\u043d\u0442.<\/p>\n<pre><code class=\"java\">@RestController @RequestMapping(\"\/auth\") public class AuthController {      private UserService service;      public AuthController(UserService service) {         this.service = service;     }      @PostMapping(path = \"\/login\", produces = MediaType.APPLICATION_JSON_VALUE)     public @ResponseBody com.springbootsecurityrest.model.User getAuthUser() {         Authentication auth = SecurityContextHolder.getContext().getAuthentication();         if (auth == null) {             return null;         }         Object principal = auth.getPrincipal();         User user = (principal instanceof User) ? (User) principal : null;         return Objects.nonNull(user) ? this.service.getByLogin(user.getUsername()) : null;     }  }<\/code><\/pre>\n<h2>5.6 \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a<\/h2>\n<p>\u0427\u0442\u043e \u0431\u044b \u0432\u0438\u0434\u0435\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u043b\u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0442\u043e\u043a\u0435\u043d\u0430, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043e\u0448\u0438\u0431\u043e\u043a. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 GlobalExceptionHandler \u0432 \u043a\u043e\u0440\u043d\u0435 com.springbootsecurityrest, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\u043c \u043a\u043b\u0430\u0441\u0441\u0430 ResponseEntityExceptionHandler \u0441 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043c\u0435\u0442\u043e\u0434\u0430 handleAuthenticationException.<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u0441\u0442\u0430\u0442\u0443\u0441 \u043e\u0442\u0432\u0435\u0442\u0430 401 (UNAUTHORIZED) \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 ErrorInfo.<\/p>\n<pre><code class=\"java\">@RestControllerAdvice public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {      private JwtTokenRepository tokenRepository;      public GlobalExceptionHandler(JwtTokenRepository tokenRepository) {         this.tokenRepository = tokenRepository;     }      @ExceptionHandler({AuthenticationException.class, MissingCsrfTokenException.class, InvalidCsrfTokenException.class, SessionAuthenticationException.class})     public ErrorInfo handleAuthenticationException(RuntimeException ex, HttpServletRequest request, HttpServletResponse response){         this.tokenRepository.clearToken(response);         response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);         return new ErrorInfo(UrlUtils.buildFullRequestUrl(request), \"error.authorization\");     }      @Getter public class ErrorInfo {         private final String url;         private final String info;          ErrorInfo(String url, String info) {             this.url = url;             this.info = info;         }     } }<\/code><\/pre>\n<h2>5.7 \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 Spring Security.<\/h2>\n<p>\u0412\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u043b\u0438 \u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b. \u0412 \u043f\u0430\u043f\u043a\u0435 com.springbootsecurityrest \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b SpringSecurityConfig, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 WebSecurityConfigurerAdapter \u043f\u0430\u043a\u0435\u0442\u0430 org.springframework.security.config.annotation.web.configuration. \u041f\u043e\u043c\u0435\u0447\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u0434\u0432\u0443\u043c\u044f \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f\u043c\u0438: Configuration \u0438 EnableWebSecurity.<\/p>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043c\u0435\u0442\u043e\u0434 configure(AuthenticationManagerBuilder auth), \u0432 \u043a\u043b\u0430\u0441\u0441 AuthenticationManagerBuilder \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0441\u0435\u0440\u0432\u0438\u0441 UserService, \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e \u0431\u044b Spring Security \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u043e\u0433 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u0437 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e \u043b\u043e\u0433\u0438\u043d\u0443.<\/p>\n<pre><code class=\"java\">@Configuration @EnableWebSecurity public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {      @Autowired     private UserService service;      @Autowired     private JwtTokenRepository jwtTokenRepository;      @Autowired     @Qualifier(\"handlerExceptionResolver\")     private HandlerExceptionResolver resolver;      @Bean     public PasswordEncoder devPasswordEncoder() {         return NoOpPasswordEncoder.getInstance();     }       @Override     protected void configure(HttpSecurity http) throws Exception {           }      @Override     protected void configure(AuthenticationManagerBuilder auth) throws Exception {         auth.userDetailsService(this.service);     }  }<\/code><\/pre>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043c\u0435\u0442\u043e\u0434 configure(HttpSecurity http):<\/p>\n<pre><code class=\"java\">    @Override     protected void configure(HttpSecurity http) throws Exception {         http                 .sessionManagement()                     .sessionCreationPolicy(SessionCreationPolicy.NEVER)                 .and()                     .addFilterAt(new JwtCsrfFilter(jwtTokenRepository, resolver), CsrfFilter.class)                     .csrf().ignoringAntMatchers(\"\/**\")                 .and()                     .authorizeRequests()                     .antMatchers(\"\/auth\/login\")                     .authenticated()                 .and()                     .httpBasic()                     .authenticationEntryPoint(((request, response, e) -&gt; resolver.resolveException(request, response, null, e)));     }<\/code><\/pre>\n<p>\u0420\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043c\u0435\u0442\u043e\u0434 \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u0435\u0435:<\/p>\n<ol>\n<li>\n<p>sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER) &#8212; \u043e\u0442\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e \u0441\u0435\u0441\u0441\u0438\u0438;<\/p>\n<\/li>\n<li>\n<p>addFilterAt(new JwtCsrfFilter(jwtTokenRepository, resolver), CsrfFilter.class).csrf().ignoringAntMatchers(&#171;\/**&#187;) &#8212; \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u043d\u0430\u043c\u0438 \u0444\u0438\u043b\u044c\u0442\u0440 JwtCsrfFilter \u0432 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0433\u043e \u0444\u0438\u043b\u044c\u0442\u0440\u0430, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0438\u0433\u043d\u043e\u0440\u0438\u0440\u0443\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0433\u043e;  <\/p>\n<\/li>\n<li>\n<p>.authorizeRequests().antMatchers(&#171;\/auth\/login&#187;).authenticated() \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \/auth\/login \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0441\u0438\u043b\u0430\u043c\u0438 security. \u0427\u0442\u043e \u0431\u044b \u043d\u0435 \u0431\u044b\u043b\u043e \u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 (\u043f\u043e \u0442\u043e\u043a\u0435\u043d\u0443 \u0438 \u0431\u0430\u0437\u043e\u0432\u043e\u0439), \u0437\u0430\u043f\u0440\u043e\u0441 \u0431\u044b\u043b \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d \u0432 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u043a\u043b\u0430\u0441\u0441\u0443 JwtCsrfFilter; <\/p>\n<\/li>\n<li>\n<p>.httpBasic().authenticationEntryPoint(((request, response, e) -&gt; resolver.resolveException(request, response, null, e))) &#8212; \u043e\u0448\u0438\u0431\u043a\u0438 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 GlobalExceptionHandler<\/p>\n<\/li>\n<\/ol>\n<h2>6. \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430<\/h2>\n<p>\u0414\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e Postman. \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0431\u044d\u043a\u0435\u043d\u0434 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 http:\/\/localhost:8080\/users \u0441 \u0442\u0438\u043f\u043e\u043c GET.<\/p>\n<p>\u0422\u043e\u043a\u0435\u043d\u0430 \u043d\u0435\u0442, \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u043d\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u0430, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0441 401 \u0441\u0442\u0430\u0442\u0443\u0441\u043e\u043c.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/97a\/552\/249\/97a552249674e05ed7a0d831a8a19d61.png\" width=\"1116\" height=\"393\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0441 \u043d\u0435\u0432\u0435\u0440\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 http:\/\/localhost:8080\/auth\/login \u0441 \u0442\u0438\u043f\u043e\u043c POST, \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430, \u0442\u043e\u043a\u0435\u043d \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u043b\u0430\u0441\u044c \u043e\u0448\u0438\u0431\u043a\u0430 \u0441 401 \u0441\u0442\u0430\u0442\u0443\u0441\u043e\u043c.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9f1\/8ea\/273\/9f18ea2735eb6ff71c93ae8c9248e8ae.png\" width=\"1122\" height=\"480\"><figcaption><\/figcaption><\/figure>\n<p>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0443\u0435\u043c\u0441\u044f \u0441 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430, \u043f\u043e\u043b\u0443\u0447\u0435\u043d \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0438 \u0442\u043e\u043a\u0435\u043d.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/5b7\/b5c\/4e4\/5b7b5c4e41a4f17246cbef6208876e30.png\" width=\"933\" height=\"530\"><figcaption><\/figcaption><\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e9c\/640\/e4e\/e9c640e4e4bb8bbb3c6b49810cf992f4.png\" width=\"1217\" height=\"231\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 http:\/\/localhost:8080\/users \u0441 \u0442\u0438\u043f\u043e\u043c GET, \u043d\u043e \u0441 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u043c \u0442\u043e\u043a\u0435\u043d\u043e\u043c \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0448\u0430\u0433\u0435. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0442\u043e\u043a\u0435\u043d.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/337\/7e0\/aab\/3377e0aab1abea553b1b66f3973fab28.png\" width=\"940\" height=\"606\"><figcaption><\/figcaption><\/figure>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 \u043e\u0434\u0438\u043d \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 REST \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441 Spring Security \u0438 JWT. \u041d\u0430\u0434\u0435\u044e\u0441\u044c \u0434\u0430\u043d\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u043c\u0443 \u0442\u043e \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u043c.<\/p>\n<p><a href=\"https:\/\/github.com\/angelrum\/SpringBootSecurityRest\" rel=\"noopener noreferrer nofollow\"><strong>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0432\u044b\u043b\u043e\u0436\u0435\u043d \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043d\u0430 github<\/strong><\/a><\/p>\n<\/div>\n<p> \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\/545610\/\"> https:\/\/habr.com\/ru\/post\/545610\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u0420\u0430\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u0437\u0434\u043d\u043e \u043a\u0430\u0436\u0434\u044b\u0439 Java-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0441\u0442\u043e\u043b\u043a\u043d\u0435\u0442\u0441\u044f \u0441 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0449\u0438\u0449\u0435\u043d\u043d\u043e\u0435 REST API \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0445\u043e\u0447\u0443 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0441\u0432\u043e\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<h2>1. \u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 REST?<\/h2>\n<p>REST (\u043e\u0442 \u0430\u043d\u0433\u043b. Representational State Transfer \u2014 \u00ab\u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u00bb) \u2013 \u044d\u0442\u043e \u043e\u0431\u0449\u0438\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\/\u0441\u0430\u0439\u0442\u0430 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c \u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e\u043c \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 HTTP.<\/p>\n<p>\u0414\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043d\u0438\u0436\u0435 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043e\u0431\u0449\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0412\u0441\u0451 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c \u0441\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043a 4 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c (4 \u2014 \u044d\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0439 \u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u044b\u0439 \u043c\u0438\u043d\u0438\u043c\u0443\u043c, \u0432 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0442\u0438\u043f\u043e\u0432 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435):<\/p>\n<ol>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 (\u043e\u0431\u044b\u0447\u043d\u043e \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 JSON, \u0438\u043b\u0438 XML);<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440;<\/p>\n<\/li>\n<li>\n<p>\u041c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435;<\/p>\n<\/li>\n<li>\n<p>\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435<\/p>\n<\/li>\n<\/ol>\n<p>\u0411\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0447\u0435\u0441\u0442\u044c \u0432 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430\u0445, \u0441\u0442\u0430\u0442\u0435\u0439 \u043e REST \u043c\u043d\u043e\u0433\u043e.<\/p>\n<h2>2. \u0417\u0430\u0434\u0430\u0447\u0430<\/h2>\n<p>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c \u0437\u0430\u0449\u0438\u0449\u0435\u043d\u043d\u043e\u0435 REST \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0441 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435\u0439 \u043b\u043e\u0433\u0438\u043d\u0430 \u0438 \u043f\u0430\u0440\u043e\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c, \u043f\u0440\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0439 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u043a\u0435\u043d. \u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u0435\u043d\u0430 \u043f\u043e \u0442\u043e\u043a\u0435\u043d\u0443.<br \/> \u0421\u0445\u0435\u043c\u0430 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h2>3. \u0422\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438<\/h2>\n<p>\u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a Spring Boot \u0438 Spring Web, \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f:<\/p>\n<ol>\n<li>\n<p>Java 8+;<\/p>\n<\/li>\n<li>\n<p>Apache Maven<\/p>\n<\/li>\n<\/ol>\n<p>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u0441\u0438\u043b\u0430\u043c\u0438 Spring Security \u0438 JsonWebToken (JWT).<br \/> \u0414\u043b\u044f \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e Lombok.<\/p>\n<h2>4. \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/h2>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435. \u0421\u043e\u0437\u0434\u0430\u0435\u043c Spring Boot \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0435 REST API \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439.<\/p>\n<h2>4.1 \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 Web-\u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/h2>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c Maven-\u043f\u0440\u043e\u0435\u043a\u0442 SpringBootSecurityRest. \u041f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u0435\u0441\u043b\u0438 \u0432\u044b \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0435 \u0447\u0435\u0440\u0435\u0437 Intellij IDEA, \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 Spring Boot DevTools, Lombok \u0438 Spring Web, \u0438\u043d\u0430\u0447\u0435 \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0432 pom-\u0444\u0430\u0439\u043b\u0435.<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<h2>4.2 \u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f pom-xml<\/h2>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0440\u0430\u0437\u0432\u0435\u0440\u0442\u044b\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u0435\u043a\u0442\u0430 pom-\u0444\u0430\u0439\u043b \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<ol>\n<li>\n<p>\u0414\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u043d parent-\u0441\u0435\u0433\u043c\u0435\u043d\u0442 \u0441 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u043d\u044b\u043c spring-boot-starter-parent;<\/p>\n<\/li>\n<li>\n<p>\u0418 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u044b \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 spring-boot-starter-web, spring-boot-devtools \u0438 Lombok.<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"java\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt; &lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"          xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 https:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;     &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;     &lt;parent&gt;         &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;         &lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt;         &lt;version&gt;2.3.5.RELEASE&lt;\/version&gt;         &lt;relativePath\/&gt; &lt;!-- lookup parent from repository --&gt;     &lt;\/parent&gt;     &lt;groupId&gt;com&lt;\/groupId&gt;     &lt;artifactId&gt;springbootsecurityrest&lt;\/artifactId&gt;     &lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;     &lt;name&gt;springbootsecurityrest&lt;\/name&gt;     &lt;description&gt;Demo project for Spring Boot&lt;\/description&gt;     &lt;properties&gt;         &lt;java.version&gt;15&lt;\/java.version&gt;     &lt;\/properties&gt;     &lt;dependencies&gt;         &lt;dependency&gt;             &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;             &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;         &lt;\/dependency&gt;          &lt;dependency&gt;             &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;             &lt;artifactId&gt;spring-boot-devtools&lt;\/artifactId&gt;             &lt;scope&gt;runtime&lt;\/scope&gt;             &lt;optional&gt;true&lt;\/optional&gt;         &lt;\/dependency&gt;         &lt;dependency&gt;             &lt;groupId&gt;org.projectlombok&lt;\/groupId&gt;             &lt;artifactId&gt;lombok&lt;\/artifactId&gt;             &lt;optional&gt;true&lt;\/optional&gt;         &lt;\/dependency&gt;         &lt;!--Test--&gt;         &lt;dependency&gt;             &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;             &lt;artifactId&gt;spring-boot-starter-test&lt;\/artifactId&gt;             &lt;scope&gt;test&lt;\/scope&gt;             &lt;exclusions&gt;                 &lt;exclusion&gt;                     &lt;groupId&gt;org.junit.vintage&lt;\/groupId&gt;                     &lt;artifactId&gt;junit-vintage-engine&lt;\/artifactId&gt;                 &lt;\/exclusion&gt;             &lt;\/exclusions&gt;         &lt;\/dependency&gt;     &lt;\/dependencies&gt;      &lt;build&gt;         &lt;plugins&gt;             &lt;plugin&gt;                 &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;                 &lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt;                 &lt;configuration&gt;                     &lt;excludes&gt;                         &lt;exclude&gt;                             &lt;groupId&gt;org.projectlombok&lt;\/groupId&gt;                             &lt;artifactId&gt;lombok&lt;\/artifactId&gt;                         &lt;\/exclude&gt;                     &lt;\/excludes&gt;                 &lt;\/configuration&gt;             &lt;\/plugin&gt;         &lt;\/plugins&gt;     &lt;\/build&gt;  &lt;\/project&gt;<\/code><\/pre>\n<h2>4.3 \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u0430 REST<\/h2>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u0438\u043c \u0432\u0441\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u043d\u0430 \u0441\u043b\u043e\u0438, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0432 \u043f\u0430\u043f\u043a\u0435 com.springbootsecurityrest \u0447\u0435\u0442\u044b\u0440\u0435 \u043d\u043e\u0432\u044b\u0435 \u043f\u0430\u043f\u043a\u0438:<\/p>\n<ul>\n<li>\n<p>model \u2013 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f POJO-\u043a\u043b\u0430\u0441\u0441\u043e\u0432;<\/p>\n<\/li>\n<li>\n<p>repository \u2013 \u0432 \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441 \u0411\u0414, \u043d\u043e \u0442.\u043a. \u0443 \u043d\u0430\u0441 \u0435\u0435 \u043d\u0435\u0442, \u0442\u043e \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439;<\/p>\n<\/li>\n<li>\n<p>service \u2013 \u0441\u043b\u043e\u0439 \u0441\u0435\u0440\u0432\u0438\u0441\u0430, \u043f\u0440\u043e\u0441\u043b\u043e\u0439\u043a\u0430 \u043c\u0435\u0436\u0434\u0443 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0435\u0440\u043e\u043c \u0438 \u0441\u043b\u043e\u0435\u043c \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0440\u0435\u0441\u0443\u0440\u0441\u0430, \u0438\u0445 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f (\u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e);<\/p>\n<\/li>\n<li>\n<p>rest \u2013 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432 \u0441\u0435\u0431\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b.<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u043f\u0430\u043f\u043a\u0435 model \u0441\u043e\u0437\u0434\u0430\u0435\u043c POJO \u043a\u043b\u0430\u0441\u0441 User.<\/p>\n<pre><code class=\"java\">import lombok.AllArgsConstructor; import lombok.Data;  @Data @AllArgsConstructor public class User {     private String login;     private String password;     private String firstname;     private String lastname;     private Integer age; }<\/code><\/pre>\n<p>\u0412 \u043f\u0430\u043f\u043a\u0435 repository \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u043a\u043b\u0430\u0441\u0441 UserRepository c \u0434\u0432\u0443\u043c\u044f \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438:<\/p>\n<ol>\n<li>\n<p>getByLogin \u2013 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e \u043b\u043e\u0433\u0438\u043d\u0443;<\/p>\n<\/li>\n<li>\n<p>getAll \u2013 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439. \u0427\u0442\u043e\u0431\u044b Spring \u0441\u043e\u0437\u0434\u0430\u043b \u0431\u0438\u043d \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 \u044d\u0442\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430, \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0435\u043c\u0443 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e @Repository.<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"java\">@Repository public class UserRepository {        private List&lt;User&gt; users;      public UserRepository() {         this.users = List.of(                 new User(\"anton\", \"1234\", \"\u0410\u043d\u0442\u043e\u043d\", \"\u0418\u0432\u0430\u043d\u043e\u0432\", 20),                 new User(\"ivan\", \"12345\", \"\u0421\u0435\u0440\u0433\u0435\u0439\", \"\u041f\u0435\u0442\u0440\u043e\u0432\", 21));     }      public User getByLogin(String login) {         return this.users.stream()                 .filter(user -&gt; login.equals(user.getLogin()))                 .findFirst()                 .orElse(null);     }      public List&lt;User&gt; getAll() {         return this.users;     }<\/code><\/pre>\n<p>\u0412 \u043f\u0430\u043f\u043a\u0435 service \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441 UserService. \u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441\u0443 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e @Service \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0438\u043d\u044a\u0435\u043a\u0446\u0438\u044e \u0431\u0438\u043d\u0430 UserRepository. \u0412 \u043a\u043b\u0430\u0441\u0441 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043c\u0435\u0442\u043e\u0434 getAll, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0432\u0441\u0435\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 getByLogin \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e \u043b\u043e\u0433\u0438\u043d\u0443. <\/p>\n<pre><code class=\"java\">@Service public class UserService {      private UserRepository repository;      public UserService(UserRepository repository) {         this.repository = repository;     }      public List&lt;User&gt; getAll() {         return this.repository.getAll();     }      public User getByLogin(String login) {         return this.repository.getByLogin(login);     } }<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 UserController \u0432 \u043f\u0430\u043f\u043a\u0435 rest, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u043c\u0443 \u0438\u043d\u044a\u0435\u043a\u0446\u0438\u044e UserService \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043e\u0434\u0438\u043d \u043c\u0435\u0442\u043e\u0434 getAll. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 @GetMapping \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0430\u0434\u0440\u0435\u0441 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u0438 \u0442\u0438\u043f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"java\">@RestController public class UserController {      private UserService service;      public UserController(UserService service) {         this.service = service;     }      @GetMapping(path = \"\/users\", produces = MediaType.APPLICATION_JSON_VALUE)     public @ResponseBody List&lt;User&gt; getAll() {         return this.service.getAll();     } }<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e \u043e\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0430\u0434\u0440\u0435\u0441 http:\/\/localhost:8080\/users, \u0435\u0441\u043b\u0438 \u0432\u044b \u0432\u0441\u0435 \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u0432\u0435\u0440\u043d\u043e, \u0442\u043e \u0443\u0432\u0438\u0434\u0438\u0442\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<h2>5. Spring Security<\/h2>\n<p>\u041f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u043e\u0435 REST API \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u0438 \u043f\u043e\u043a\u0430 \u043e\u043d\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u043e \u0434\u043b\u044f \u0432\u0441\u0435\u0445. \u0414\u0432\u0438\u0433\u0430\u0435\u043c\u0441\u044f \u0434\u0430\u043b\u044c\u0448\u0435, \u0442\u0435\u043f\u0435\u0440\u044c \u0435\u0433\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u0449\u0438\u0442\u0438\u0442\u044c, \u0430 \u0434\u043e\u0441\u0442\u0443\u043f \u043e\u0442\u043a\u0440\u044b\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f Spring Security \u0438 JWT.<\/p>\n<p>Spring Security \u044d\u0442\u043e Java\/JavaEE framework, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0441\u0438\u0441\u0442\u0435\u043c \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0434\u0440\u0443\u0433\u0438\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u044f \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043a\u043e\u0440\u043f\u043e\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0445 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Spring Framework.<\/p>\n<p>JSON Web Token (JWT) \u2014 \u044d\u0442\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442 (RFC 7519) \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u0434\u043e\u0441\u0442\u0443\u043f\u0430, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 JSON. \u041a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u043e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0432 \u043a\u043b\u0438\u0435\u043d\u0442-\u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445. \u0422\u043e\u043a\u0435\u043d\u044b \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c, \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0441\u0435\u043a\u0440\u0435\u0442\u043d\u044b\u043c \u043a\u043b\u044e\u0447\u043e\u043c \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0439 \u0442\u043e\u043a\u0435\u043d \u0434\u043b\u044f \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u044f \u0441\u0432\u043e\u0435\u0439 \u043b\u0438\u0447\u043d\u043e\u0441\u0442\u0438.<\/p>\n<h2>5.1 \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438<\/h2>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u043e\u0432\u044b\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u0432 pom-\u0444\u0430\u0439\u043b.<\/p>\n<pre><code class=\"java\">&lt;!--Security--&gt; &lt;dependency&gt;     &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;     &lt;artifactId&gt;spring-boot-starter-security&lt;\/artifactId&gt; &lt;\/dependency&gt; &lt;dependency&gt;     &lt;groupId&gt;io.jsonwebtoken&lt;\/groupId&gt;     &lt;artifactId&gt;jjwt&lt;\/artifactId&gt;     &lt;version&gt;0.9.1&lt;\/version&gt; &lt;\/dependency&gt; &lt;dependency&gt;     &lt;groupId&gt;jakarta.xml.bind&lt;\/groupId&gt;     &lt;artifactId&gt;jakarta.xml.bind-api&lt;\/artifactId&gt;     &lt;version&gt;2.3.3&lt;\/version&gt; &lt;\/dependency&gt;<\/code><\/pre>\n<h2>5.2 \u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0438 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u0430<\/h2>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u0430, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0430\u043f\u043a\u0443 security \u0438 \u0432 \u043d\u0435\u0439 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441 JwtTokenRepository \u0441 \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0435\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 CsrfTokenRepository (\u0438\u0437 \u043f\u0430\u043a\u0435\u0442\u0430 org.springframework.security.web.csrf).<\/p>\n<p>\u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0440\u0438 \u043c\u0435\u0442\u043e\u0434\u0430:<\/p>\n<ol>\n<li>\n<p>\u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u0430 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 generateToken;<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u0430 \u2013 saveToken;<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0442\u043e\u043a\u0435\u043d\u0430 \u2013 loadToken.<\/p>\n<\/li>\n<\/ol>\n<p>\u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0442\u043e\u043a\u0435\u043d \u0441\u0438\u043b\u0430\u043c\u0438 Jwt, \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u0435\u0442\u043e\u0434\u0430.<\/p>\n<pre><code class=\"java\">@Repository public class JwtTokenRepository implements CsrfTokenRepository {      @Getter     private String secret;      public JwtTokenRepository() {         this.secret = \"springrest\";     }      @Override     public CsrfToken generateToken(HttpServletRequest httpServletRequest) {         String id = UUID.randomUUID().toString().replace(\"-\", \"\");         Date now = new Date();         Date exp = Date.from(LocalDateTime.now().plusMinutes(30)                 .atZone(ZoneId.systemDefault()).toInstant());          String token = \"\";         try {             token = Jwts.builder()                     .setId(id)                     .setIssuedAt(now)                     .setNotBefore(now)  <\/code><\/pre>\n<\/p>\n<\/div>\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-319059","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/319059","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=319059"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/319059\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=319059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=319059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=319059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}