{"id":363118,"date":"2024-05-21T01:49:14","date_gmt":"2024-05-21T01:49:14","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=363118"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-05-21T01:49:14","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=363118","title":{"rendered":"<span>\u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u0435\u0440\u0432\u0438\u0441 \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 \u043f\u043e JWT \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432 SpringSecurity<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><strong>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u043e\u0435\u043a\u0442: <\/strong><a href=\"https:\/\/gitlab.com\/SilverRid\/SpringSecurityJWT\" rel=\"noopener noreferrer nofollow\"><strong>GitLab<\/strong><\/a><\/p>\n<p>\u041d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u0435\u0440\u0432\u0438\u0441 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0441 \u0432\u044b\u0434\u0430\u0447\u0435\u0439 JWToken. \u0414\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Java 17, Maven, SpringBoot 3.2.0, h2 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438. \u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0438 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432 SpringSecurity.<\/p>\n<h3>C\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u043c \u043f\u0440\u043e\u0435\u043a\u0442 https:\/\/start.spring.io\/<\/h3>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/665\/e3f\/470\/665e3f470d3e4da5abb264383ae98877.png\" alt=\"SpringInitializer\" title=\"SpringInitializer\" width=\"1481\" height=\"567\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/665\/e3f\/470\/665e3f470d3e4da5abb264383ae98877.png\"\/><\/p>\n<div><figcaption>SpringInitializer<\/figcaption><\/div>\n<\/figure>\n<p>\u041d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f:<\/p>\n<ul>\n<li>\n<p>Web<\/p>\n<\/li>\n<li>\n<p>Security<\/p>\n<\/li>\n<li>\n<p>JPA<\/p>\n<\/li>\n<li>\n<p>H2 Database<\/p>\n<\/li>\n<li>\n<p>Lombok<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u0430\u0441\u0442\u0440\u043e\u0438\u043c \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u043d\u0430\u0448\u0435\u0439 \u0411\u0414 \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0430\u043c\u044f\u0442\u0438, \u0442\u0430\u043a \u0436\u0435 \u0441\u0435\u0440\u0432\u0435\u0440 \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043d\u0430 \u043f\u043e\u0440\u0442\u0443 9090 <\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/96e\/d0c\/60f\/96ed0c60f1a109abf6676b692e092516.png\" alt=\"application.properties\" title=\"application.properties\" width=\"508\" height=\"275\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/96e\/d0c\/60f\/96ed0c60f1a109abf6676b692e092516.png\"\/><\/p>\n<div><figcaption>application.properties<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0411\u0414 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0430\u0440\u0443 \u0444\u0430\u0439\u043b\u043e\u0432 \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0446 data.sql \u0438 schema.sql.<\/p>\n<details class=\"spoiler\">\n<summary>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u043e\u0432 data &amp;&amp; schema<\/summary>\n<div class=\"spoiler__content\">\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/929\/91f\/2a0\/92991f2a0ce133983aaa219dde664f15.png\" alt=\"data.sql\" title=\"data.sql\" width=\"381\" height=\"497\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/929\/91f\/2a0\/92991f2a0ce133983aaa219dde664f15.png\"\/><\/p>\n<div><figcaption>data.sql<\/figcaption><\/div>\n<\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e5d\/c1a\/725\/e5dc1a7250d3efca1effac9c631ca276.png\" alt=\"schema.sql\" title=\"schema.sql\" width=\"1172\" height=\"204\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e5d\/c1a\/725\/e5dc1a7250d3efca1effac9c631ca276.png\"\/><\/p>\n<div><figcaption>schema.sql<\/figcaption><\/div>\n<\/figure>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>\u0414\u0430\u043b\u0435\u0435 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043e\u043f\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 User, Role<\/summary>\n<div class=\"spoiler__content\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/3ea\/f7f\/d79\/3eaf7fd792c302f9ab9850269c9b9de1.png\" alt=\"User\" title=\"User\" width=\"530\" height=\"377\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3ea\/f7f\/d79\/3eaf7fd792c302f9ab9850269c9b9de1.png\"\/><\/p>\n<div><figcaption>User<\/figcaption><\/div>\n<\/figure>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/873\/c2f\/7e8\/873c2f7e8d534ec43879fc3ef809405b.png\" alt=\"Role\" title=\"Role\" width=\"456\" height=\"228\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/873\/c2f\/7e8\/873c2f7e8d534ec43879fc3ef809405b.png\"\/><\/p>\n<div><figcaption>Role<\/figcaption><\/div>\n<\/figure>\n<\/div>\n<\/details>\n<h3>\u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/h3>\n<ul>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0438\u043b\u044c\u0442\u0440 \u0434\u043b\u044f \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438 \u0432\u044b\u0434\u0430\u0447\u0438 JWT<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0435\u0440\u0432\u0438\u0441 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 JWT<\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u0441\u0442\u0440\u043e\u0438\u043c SecurityFilterChain<\/p>\n<\/li>\n<\/ul>\n<p>\u0417\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u0438 \u0441 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u043e\u0439, \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c\u0443 \u043a\u043b\u0430\u0441\u0441\u0443 <strong>WebConfiguration<\/strong> \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 Security. \u0412 \u043d\u0435\u043c \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c bean <strong>SecurityFilterChain<\/strong>, \u0442\u0430\u043a \u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c bean <strong>PasswordEncoder<\/strong> \u0434\u043b\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u0440\u043e\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. <\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/4df\/3d6\/262\/4df3d62621623912d0c9fa4167d061c8.png\" alt=\"WebConfiguration.jav\" title=\"WebConfiguration.jav\" width=\"1011\" height=\"467\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4df\/3d6\/262\/4df3d62621623912d0c9fa4167d061c8.png\"\/><\/p>\n<div><figcaption>WebConfiguration.jav<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u043b\u044f \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u0440\u043e\u043b\u044f \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c BCrypt. <\/p>\n<p> \u0414\u0430\u043b\u0435\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c <strong>SecurityFilterChain<\/strong>:<\/p>\n<pre><code class=\"java\">http.headers(headers -> headers              .frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin))               .csrf(AbstractHttpConfigurer::disable)               .cors(AbstractHttpConfigurer::disable); <\/code><\/pre>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0440\u043e\u0447\u043a\u0435 \u043c\u044b \u043e\u0442\u043a\u043b\u044e\u0447\u0430\u0435\u043c CSRF &amp;&amp; CORS \u0442\u0430\u043a \u043a\u0430\u043a \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0444\u0443\u043d\u0446\u0438\u043e\u043d\u0430\u043b\u0430 \u043d\u0430\u043c \u043e\u043d\u0438 \u043d\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u044f\u0442\u0441\u044f. <\/p>\n<blockquote>\n<p>\u0412 \u043f\u0440\u043e\u0434\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c  CSRF &amp;&amp; CORS<\/p>\n<\/blockquote>\n<p>\u0422.\u043a h2-console \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 frame \u0442\u043e \u0434\u043b\u044f \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c <strong>header x-frame-options &#171;SAMEORIGIN&#187;<\/strong> \u0438\u043b\u0438 \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0435\u0433\u043e <strong>FrameOptionsConfig::disable<\/strong><\/p>\n<pre><code class=\"java\">headers.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable) headers.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin)<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u043c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043a\u043e\u043d\u0441\u043e\u043b\u0438 h2 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 \u0434\u043b\u044f \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/p>\n<pre><code class=\"java\">http.authorizeHttpRequests(authz -> authz.requestMatchers(\"\/h2-console\/**\")                            .permitAll()                            .anyRequest()                            .authenticated());<\/code><\/pre>\n<p><strong>\u0413\u043b\u0430\u0432\u043d\u043e\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0432 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/strong> <\/p>\n<pre><code class=\"java\">http.addFilterAt(initialAuthenticationFilter, BasicAuthenticationFilter.class);<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u0432\u043e\u0439 \u0444\u0438\u043b\u044c\u0442\u0440 \u0432 \u0446\u0435\u043f\u043e\u0447\u043a\u0443 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 <strong><em>initialAuthenticationFilter<\/em><\/strong><em> \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b inject&#8217;\u0438\u043c \u0432 \u043c\u0435\u0442\u043e\u0434\u0435  <br \/><\/em><strong><em>public SecurityFilterChain securityFilterChain(HttpSecurity http, InitialAuthenticationFilter initialAuthenticationFilter)<\/em><\/strong><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e78\/4fe\/764\/e784fe76455a33761c5049ad16633453.png\" alt=\"InitialAuthenticationFilter.java\" title=\"InitialAuthenticationFilter.java\" width=\"960\" height=\"688\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e78\/4fe\/764\/e784fe76455a33761c5049ad16633453.png\"\/><\/p>\n<div><figcaption>InitialAuthenticationFilter.java<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0442\u044c <strong>OncePerRequestFilter<\/strong> \u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0434\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 <\/p>\n<ul>\n<li>\n<p>doFilterInternal<\/p>\n<\/li>\n<li>\n<p>shouldNotFilter<\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u0444\u0438\u043b\u044c\u0442\u0440 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 <u>header<\/u> <strong>Authorization<\/strong>, \u0435\u0441\u043b\u0438 \u043e\u043d \u043f\u0443\u0441\u0442\u043e\u0439 \u0442\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043e \u043b\u0438 \u0432 \u0442\u0435\u043b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 JSON \u0441 \u0438\u043c\u0435\u043d\u0435\u043c \u0438 \u043f\u0430\u0440\u043e\u043b\u0435\u043c \u0434\u043b\u044f \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u0435\u0441\u043b\u0438 \u0432\u0441\u0435 \u043e\u043a \u0432\u044b\u0434\u0430\u0435\u0442 JWToken.<\/p>\n<p><strong>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441.<\/strong><\/p>\n<p>\u0414\u043b\u044f \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f JWToken \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0438 \u0437\u0430\u0438\u043d\u0436\u0435\u043a\u0442\u0438\u043c \u0434\u0432\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 <\/p>\n<p><strong><em>private final JwtService jwtService; <br \/>private final UsernamePasswordAuthenticationProvider authenticationProvider;<\/em><\/strong><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b43\/ccf\/cd6\/b43ccfcd6b1d8259346db2880ba85ed2.png\" alt=\"JwtService.java\" title=\"JwtService.java\" width=\"1252\" height=\"695\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b43\/ccf\/cd6\/b43ccfcd6b1d8259346db2880ba85ed2.png\"\/><\/p>\n<div><figcaption>JwtService.java<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 JWT \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/3ba\/3b2\/186\/3ba3b218692b53c301b53812d0a2050a.png\" alt=\"pom.xml\" title=\"pom.xml\" width=\"367\" height=\"352\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3ba\/3b2\/186\/3ba3b218692b53c301b53812d0a2050a.png\"\/><\/p>\n<div><figcaption>pom.xml<\/figcaption><\/div>\n<\/figure>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043b\u044e\u0447 \u043f\u043e\u0434\u043f\u0438\u0441\u0438 \u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0441\u0430\u043c JWT.<\/p>\n<p><em>Keys.hmacShaKeyFor(signingKey.getBytes(StandardCharsets.UTF_8)); <\/em><\/p>\n<p>\u0448\u0438\u0444\u0440\u0443\u0435\u043c \u043a\u043b\u044e\u0447 BASE64<\/p>\n<p>\u0412 \u043c\u0435\u0442\u043e\u0434\u0435 <em>generatedJwt \u0441\u0442\u0440\u043e\u0438\u043c JWT. <\/em><\/p>\n<p>\u0417\u0430\u0434\u0430\u0435\u043c \u043f\u043e\u043b\u044f \u0432 payload \u0438 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443, \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0434\u0430\u0442\u0443 \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044f \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0438 \u043a\u043b\u044e\u0447 \u043f\u043e\u0434\u043f\u0438\u0441\u0438, \u0442\u0435\u043b\u043e \u043d\u0430\u0448\u0435\u0433\u043e JWT \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u0442\u044c \u0438\u0437 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u043f\u043e\u043b\u0435\u0439:<\/p>\n<pre><code class=\"json\">payload {    \"role\" : \"\",   \"user_id\" : \"\",   \"username\" : \"\",   \"exp\" : \"\",   \"sub\" : \"\"}<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/dad\/ece\/6aa\/dadece6aa3aae56dae166bffec3f94b0.png\" alt=\"UsernamePasswordAuthenticationProvider.java\" title=\"UsernamePasswordAuthenticationProvider.java\" width=\"886\" height=\"409\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/dad\/ece\/6aa\/dadece6aa3aae56dae166bffec3f94b0.png\"\/><\/p>\n<div><figcaption>UsernamePasswordAuthenticationProvider.java<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c \u043f\u0430\u0440\u043e\u043b\u044f \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e.<\/p>\n<p>\u041a\u043b\u0430\u0441\u0441 <strong>UserDetailsService<\/strong> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0435\u0441\u043b\u0438 \u043e\u043d \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u0432 \u0431\u0430\u0437\u0435<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/82d\/83e\/4a9\/82d83e4a906e2c901857df809b918e72.png\" alt=\"UserService.java\" title=\"UserService.java\" width=\"1149\" height=\"260\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/82d\/83e\/4a9\/82d83e4a906e2c901857df809b918e72.png\"\/><\/p>\n<div><figcaption>UserService.java<\/figcaption><\/div>\n<\/figure>\n<p>\u0412\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u043a \u043a\u043b\u0430\u0441\u0441\u0443 <strong>InitialAuthenticationFilter<\/strong>.<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 <strong><em>doFilterInternal<\/em><\/strong><em> <\/em><\/p>\n<ul>\n<li>\n<p>\u0438\u0437 request \u043c\u044b \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u043c JSON \u0441 \u043b\u043e\u0433\u0438\u043d\u043e\u043c \u0438 \u043f\u0430\u0440\u043e\u043b\u0435\u043c <\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e\u0435\u0440\u044f\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f <br \/><em>Authentication authentication = new UsernamePasswordAuthentication(username, password); authentication = authenticationProvider.authenticate(authentication);<\/em><\/p>\n<\/li>\n<li>\n<p>\u0435\u0441\u043b\u0438 \u0432\u0441\u0435 \u043e\u043a \u0432\u044b\u0434\u0430\u0435\u043c JWT \u0432  response \u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a <em>Authorization: Bearer *****<\/em><br \/>String jwt = jwtS<em>ervice.generatedJwt(authentication); response.setHeader(&#171;Authorization&#187;, HeaderValues.BEARER + jwt);<\/em><\/p>\n<\/li>\n<\/ul>\n<p>\u041c\u0435\u0442\u043e\u0434 <strong><em>shouldNotFilter<\/em><\/strong><\/p>\n<ul>\n<li>\n<p>\u041f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0444\u0438\u043b\u044c\u0442\u0440 \u043a \u043e\u0442\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c\u0443\/\u044b\u043c uri <br \/>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435  \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u0441\u044f \u043a \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c \u043d\u0430 uri \/login<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430<\/strong> <\/p>\n<pre><code class=\"bash\">curl -v -d '{\"username\":\"admin\", \"password\":\"123\"}' \\ -H \"Content-Type: application\/json\" \\ -X POST http:\/\/localhost:9090\/login<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/cee\/5ea\/7e2\/cee5ea7e272b618a050da7a4cf5b4c24.png\" width=\"1381\" height=\"423\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/cee\/5ea\/7e2\/cee5ea7e272b618a050da7a4cf5b4c24.png\"\/><\/figure>\n<p>\u0412\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f <strong>header<\/strong> <\/p>\n<pre><code class=\"yaml\">Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJyb2xlIjpbIlJPTEVfQURNSU4iLCJST0xFX1VTRVIiXSwidXNlcl9pZCI6IjEiLCJ1c2VybmFtZSI6ImFkbWluIiwiZXhwIjoxNzAzMTU1NzAxLCJzdWIiOiJhZG1pbiJ9.aNHtaBa-7WDXO_MMl83MG9wxTO0MnMmEwdjgzSOrh0g<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c64\/6a2\/b4e\/c646a2b4ed4e471e9e5d65e09f6353af.png\" alt=\"\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 JWToken\" title=\"\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 JWToken\" width=\"1198\" height=\"731\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c64\/6a2\/b4e\/c646a2b4ed4e471e9e5d65e09f6353af.png\"\/><\/p>\n<div><figcaption>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 JWToken<\/figcaption><\/div>\n<\/figure>\n<h3>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0447\u0430\u0441\u0442\u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/h3>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 <strong>JwtAuthorizationFilter<\/strong><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b5b\/208\/26a\/b5b20826a8658cccc1177a478e0e5b98.png\" alt=\"JwtAuthorizationFilter.java\" title=\"JwtAuthorizationFilter.java\" width=\"1265\" height=\"773\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b5b\/208\/26a\/b5b20826a8658cccc1177a478e0e5b98.png\"\/><\/p>\n<div><figcaption>JwtAuthorizationFilter.java<\/figcaption><\/div>\n<\/figure>\n<p>\u042d\u0442\u0430\u043f\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0444\u0438\u043b\u044c\u0442\u0440\u0430: <\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u0430 \u043a\u043e \u0432\u0441\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c \u043a\u0440\u043e\u043c\u0435 &#171;\/login&#187;<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 header Authorization \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 <\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0432\u0430\u043b\u0438\u0434\u043d\u043e\u0441\u0442\u0438 JWT<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0440\u043e\u043b\u0438<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/p>\n<\/li>\n<\/ul>\n<p>\u0417\u0430 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u0430 \u043a\u043e \u0432\u0441\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c \u043a\u0440\u043e\u043c\u0435 &#171;\/login&#187; \u0443 \u043d\u0430\u0441 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <strong><em>shouldNotFilter<\/em><\/strong>(HttpServletRequest request).<\/p>\n<p>\u0414\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0432\u0430\u043b\u0438\u0434\u043d\u043e\u0441\u0442\u0438 JWT \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043a\u043b\u0430\u0441\u0441 <strong>JwtService <\/strong>\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b:<\/p>\n<pre><code class=\"java\">@Override public Claims getClaims(String jwt) { return Jwts.parserBuilder() .setSigningKey(generatedSecretKey()) .build() .parseClaimsJws(jwt) .getBody(); }  @Override public boolean isValidJwt(Jwt jwt) { Claims claims = Jwts.parserBuilder() .setSigningKey(generatedSecretKey()) .build() .parseClaimsJws(jwt.getToken()) .getBody();  Optional&lt;User> user = userRepository.findByUsername(String.valueOf(claims.get(ClaimField.USERNAME)));  return claims.getExpiration().after(new Date()) &amp;&amp; user.isPresent(); }<\/code><\/pre>\n<p>\u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432 <strong>isValidJwt<\/strong> \u043c\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c, \u0438\u0441\u0442\u0435\u043a\u043b\u0430 \u043b\u0438 \u0434\u0430\u0442\u0430 \u0432\u044b\u0434\u0430\u0447\u0438. \u0415\u0441\u043b\u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u044c JWT \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0431\u0440\u043e\u0448\u0435\u043d\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435.<\/p>\n<p>\u0412 <strong>getClaims <\/strong>\u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043c\u0430\u043f\u0443<strong>&lt;String, Object><\/strong> \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u044b \u0432 JWT \u043f\u0440\u0438 \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043f\u0440\u0430\u0432\u0430 \u0438\u0437 JWT \u0438 \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c authentication.<\/p>\n<pre><code class=\"java\"> List&lt;GrantedAuthority> authorities = (List&lt;GrantedAuthority>) roles.stream()                .map(role -> new SimpleGrantedAuthority(role.toString()))                .collect(Collectors.toList());  UsernamePasswordAuthenticationToken authentication =                new UsernamePasswordAuthentication(username, null, authorities); SecurityContextHolder.getContext().setAuthentication(authentication);<\/code><\/pre>\n<ul>\n<li>\n<p><strong>SecurityContextHolder<\/strong>, \u0432 \u043d\u0435\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u044f \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435(Principal) \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0435\u043c \u0432 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u0441 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c.<\/p>\n<\/li>\n<li>\n<p> <strong>SecurityContext<\/strong>, \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 Authentication \u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u0443\u044e \u0441 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.<\/p>\n<\/li>\n<li>\n<p><strong>Authentication<\/strong> \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.<\/p>\n<\/li>\n<li>\n<p><strong>GrantedAuthority<\/strong> \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u0442 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0432\u044b\u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0432 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0435 \u0432\u0441\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 ROLE_USER, ROLE_ADMIN.<\/p>\n<\/li>\n<\/ul>\n<p>\u0422.\u043a. SecurityContextHolder \u0434\u043e\u043b\u0436\u0435\u043d \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0438\u0437\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e\u0431 Authentication, \u0442\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0435 \u0432 SecurityContext. \u0422\u0435\u043f\u0435\u0440\u044c \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u044b \u0442\u0440\u0435\u0431\u0443\u044e\u0449\u0438\u0435 Authentication \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0443 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0442\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u043c \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u044b.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0447\u0442\u043e \u0431\u044b \u043d\u0430\u0448 \u0444\u0438\u043b\u044c\u0442\u0440 \u0437\u0430\u0440\u0430\u0431\u043e\u0442\u0430\u043b \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/282\/28d\/663\/28228d6636d6524af3c4bb219b1375b2.png\" alt=\"WebConfiguration.java\" title=\"WebConfiguration.java\" width=\"1259\" height=\"513\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/282\/28d\/663\/28228d6636d6524af3c4bb219b1375b2.png\"\/><\/p>\n<div><figcaption>WebConfiguration.java<\/figcaption><\/div>\n<\/figure>\n<pre><code class=\"java\">@Bean public SecurityFilterChain securityFilterChain(HttpSecurity http, InitialAuthenticationFilter initialAuthenticationFilter, JwtAuthorizationFilter jwtAuthorizationFilter) throws Exception {  http           .addFilterAt(initialAuthenticationFilter, BasicAuthenticationFilter.class)           .addFilterAt(jwtAuthorizationFilter, BasicAuthenticationFilter.class);  \/\/ *******************************     \/\/ Some code       \/\/ *******************************  return http.build(); } } <\/code><\/pre>\n<p>\u0414\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043e\u0442\u043d\u0440\u043e\u043b\u043b\u0435\u0440 <strong>UserController<\/strong><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c13\/a3a\/ecf\/c13a3aecff1ccfc2b28e17ccc595f05b.png\" alt=\"UserController.java\" title=\"UserController.java\" width=\"856\" height=\"601\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c13\/a3a\/ecf\/c13a3aecff1ccfc2b28e17ccc595f05b.png\"\/><\/p>\n<div><figcaption>UserController.java<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u043e\u0441\u0442\u0443\u043f \u043f\u043e \u043f\u0440\u0430\u0432\u0430\u043c \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 FilterChain, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438<strong><a class=\"mention\" href=\"\/users\/preauthorize.\">@PreAuthorize.<\/a><\/strong><\/p>\n<p>\u0427\u0442\u043e \u0431\u044b \u0434\u0430\u043d\u043d\u0430\u044f \u0430\u043d\u043e\u0442\u0430\u0446\u0438\u044f \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e <strong><a class=\"mention\" href=\"\/users\/EnableMethodSecurity\">@EnableMethodSecurity<\/a><\/strong><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/5aa\/9a5\/c12\/5aa9a5c1297a185d353542560248a240.png\" alt=\"SpringSecurityJwtApplication.java\" title=\"SpringSecurityJwtApplication.java\" width=\"913\" height=\"179\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/5aa\/9a5\/c12\/5aa9a5c1297a185d353542560248a240.png\"\/><\/p>\n<div><figcaption>SpringSecurityJwtApplication.java<\/figcaption><\/div>\n<\/figure>\n<h4>\u041f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c<\/h4>\n<ul>\n<li>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441 \u0431\u0435\u0437 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"bash\">curl -v http:\/\/localhost:9090\/user\/get\/myname <\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/318\/cd6\/8cd\/318cd68cd9c9219dbf3cc69423725f0b.png\" alt=\"\u0417\u0430\u043f\u0440\u043e\u0441 \u0431\u0435\u0437 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438\" title=\"\u0417\u0430\u043f\u0440\u043e\u0441 \u0431\u0435\u0437 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438\" width=\"595\" height=\"375\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/318\/cd6\/8cd\/318cd68cd9c9219dbf3cc69423725f0b.png\"\/><\/p>\n<div><figcaption>\u0417\u0430\u043f\u0440\u043e\u0441 \u0431\u0435\u0437 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043e\u0442\u0432\u0435\u0442 403 Forbidden<\/p>\n<ul>\n<li>\n<p>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0443\u0435\u043c\u0441\u044f \u043f\u043e\u0434 <strong>User<\/strong> <\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0442\u043e\u043a\u0435\u043d<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/f74\/9ee\/197\/f749ee19782f8cd0aa11ca4f0c06ffd1.png\" alt=\"\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0442\u043e\u043a\u0435\u043d\" title=\"\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0442\u043e\u043a\u0435\u043d\" width=\"879\" height=\"515\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f74\/9ee\/197\/f749ee19782f8cd0aa11ca4f0c06ffd1.png\"\/><\/p>\n<div><figcaption>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0442\u043e\u043a\u0435\u043d<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u043c\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c<br \/><a href=\"http:\/\/localhost:9090\/user\/get\/myname\" rel=\"noopener noreferrer nofollow\">http:\/\/localhost:9090\/user\/get\/myname<\/a><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/130\/10e\/fbc\/13010efbcb74fab09cdccb75cd915601.png\" alt=\"\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u043c\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c\" title=\"\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u043c\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c\" width=\"898\" height=\"416\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/130\/10e\/fbc\/13010efbcb74fab09cdccb75cd915601.png\"\/><\/p>\n<div><figcaption>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u043c\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0434\u0430\u043d\u043d\u044b\u043c \u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 <br \/><a href=\"http:\/\/localhost:9090\/user\/get\/all\" rel=\"noopener noreferrer nofollow\">http:\/\/localhost:9090\/user\/get\/all<\/a><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/13f\/fdb\/d75\/13ffdbd7557039e731f354e6107d7881.png\" alt=\"\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0434\u0430\u043d\u043d\u044b\u043c \u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c response 403 forbidden\" title=\"\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0434\u0430\u043d\u043d\u044b\u043c \u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c response 403 forbidden\" width=\"898\" height=\"416\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/13f\/fdb\/d75\/13ffdbd7557039e731f354e6107d7881.png\"\/><\/p>\n<div><figcaption>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0434\u0430\u043d\u043d\u044b\u043c \u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c response 403 forbidden<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c response 403 forbidden. \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442. <\/p>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 \u043a\u0430\u043a \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438, \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438, \u0432\u044b\u0434\u0430\u0447\u0438 JWT. <\/p>\n<p><strong>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u043e\u0435\u043a\u0442: <\/strong><a href=\"https:\/\/gitlab.com\/SilverRid\/SpringSecurityJWT\" rel=\"noopener noreferrer nofollow\"><strong>GitLab<\/strong><\/a><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/781066\/\"> https:\/\/habr.com\/ru\/articles\/781066\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><strong>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u043e\u0435\u043a\u0442: <\/strong><a href=\"https:\/\/gitlab.com\/SilverRid\/SpringSecurityJWT\" rel=\"noopener noreferrer nofollow\"><strong>GitLab<\/strong><\/a><\/p>\n<p>\u041d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u0435\u0440\u0432\u0438\u0441 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0441 \u0432\u044b\u0434\u0430\u0447\u0435\u0439 JWToken. \u0414\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Java 17, Maven, SpringBoot 3.2.0, h2 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438. \u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0438 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432 SpringSecurity.<\/p>\n<h3>C\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u043c \u043f\u0440\u043e\u0435\u043a\u0442 https:\/\/start.spring.io\/<\/h3>\n<figure class=\"full-width\">\n<div><figcaption>SpringInitializer<\/figcaption><\/div>\n<\/figure>\n<p>\u041d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f:<\/p>\n<ul>\n<li>\n<p>Web<\/p>\n<\/li>\n<li>\n<p>Security<\/p>\n<\/li>\n<li>\n<p>JPA<\/p>\n<\/li>\n<li>\n<p>H2 Database<\/p>\n<\/li>\n<li>\n<p>Lombok<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u0430\u0441\u0442\u0440\u043e\u0438\u043c \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u043d\u0430\u0448\u0435\u0439 \u0411\u0414 \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0430\u043c\u044f\u0442\u0438, \u0442\u0430\u043a \u0436\u0435 \u0441\u0435\u0440\u0432\u0435\u0440 \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043d\u0430 \u043f\u043e\u0440\u0442\u0443 9090 <\/p>\n<figure class=\"\">\n<div><figcaption>application.properties<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0411\u0414 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0430\u0440\u0443 \u0444\u0430\u0439\u043b\u043e\u0432 \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0446 data.sql \u0438 schema.sql.<\/p>\n<details class=\"spoiler\">\n<summary>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u043e\u0432 data &amp;&amp; schema<\/summary>\n<div class=\"spoiler__content\">\n<figure class=\"\">\n<div><figcaption>data.sql<\/figcaption><\/div>\n<\/figure>\n<figure class=\"full-width\">\n<div><figcaption>schema.sql<\/figcaption><\/div>\n<\/figure>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>\u0414\u0430\u043b\u0435\u0435 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043e\u043f\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 User, Role<\/summary>\n<div class=\"spoiler__content\">\n<figure class=\"full-width\">\n<div><figcaption>User<\/figcaption><\/div>\n<\/figure>\n<figure class=\"\">\n<div><figcaption>Role<\/figcaption><\/div>\n<\/figure>\n<\/div>\n<\/details>\n<h3>\u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/h3>\n<ul>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0438\u043b\u044c\u0442\u0440 \u0434\u043b\u044f \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438 \u0432\u044b\u0434\u0430\u0447\u0438 JWT<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0435\u0440\u0432\u0438\u0441 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 JWT<\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u0441\u0442\u0440\u043e\u0438\u043c SecurityFilterChain<\/p>\n<\/li>\n<\/ul>\n<p>\u0417\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u0438 \u0441 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u043e\u0439, \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c\u0443 \u043a\u043b\u0430\u0441\u0441\u0443 <strong>WebConfiguration<\/strong> \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 Security. \u0412 \u043d\u0435\u043c \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c bean <strong>SecurityFilterChain<\/strong>, \u0442\u0430\u043a \u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c bean <strong>PasswordEncoder<\/strong> \u0434\u043b\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u0440\u043e\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. <\/p>\n<figure class=\"full-width\">\n<div><figcaption>WebConfiguration.jav<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u043b\u044f \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u0440\u043e\u043b\u044f \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c BCrypt. <\/p>\n<p> \u0414\u0430\u043b\u0435\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c <strong>SecurityFilterChain<\/strong>:<\/p>\n<pre><code class=\"java\">http.headers(headers -> headers              .frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin))               .csrf(AbstractHttpConfigurer::disable)               .cors(AbstractHttpConfigurer::disable); <\/code><\/pre>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0440\u043e\u0447\u043a\u0435 \u043c\u044b \u043e\u0442\u043a\u043b\u044e\u0447\u0430\u0435\u043c CSRF &amp;&amp; CORS \u0442\u0430\u043a \u043a\u0430\u043a \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0444\u0443\u043d\u0446\u0438\u043e\u043d\u0430\u043b\u0430 \u043d\u0430\u043c \u043e\u043d\u0438 \u043d\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u044f\u0442\u0441\u044f. <\/p>\n<blockquote>\n<p>\u0412 \u043f\u0440\u043e\u0434\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c  CSRF &amp;&amp; CORS<\/p>\n<\/blockquote>\n<p>\u0422.\u043a h2-console \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 frame \u0442\u043e \u0434\u043b\u044f \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c <strong>header x-frame-options &#171;SAMEORIGIN&#187;<\/strong> \u0438\u043b\u0438 \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0435\u0433\u043e <strong>FrameOptionsConfig::disable<\/strong><\/p>\n<pre><code class=\"java\">headers.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable) headers.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin)<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u043c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043a\u043e\u043d\u0441\u043e\u043b\u0438 h2 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 \u0434\u043b\u044f \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/p>\n<pre><code class=\"java\">http.authorizeHttpRequests(authz -> authz.requestMatchers(\"\/h2-console\/**\")                            .permitAll()                            .anyRequest()                            .authenticated());<\/code><\/pre>\n<p><strong>\u0413\u043b\u0430\u0432\u043d\u043e\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0432 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/strong> <\/p>\n<pre><code class=\"java\">http.addFilterAt(initialAuthenticationFilter, BasicAuthenticationFilter.class);<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u0432\u043e\u0439 \u0444\u0438\u043b\u044c\u0442\u0440 \u0432 \u0446\u0435\u043f\u043e\u0447\u043a\u0443 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 <strong><em>initialAuthenticationFilter<\/em><\/strong><em> \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b inject&#8217;\u0438\u043c \u0432 \u043c\u0435\u0442\u043e\u0434\u0435  <br \/><\/em><strong><em>public SecurityFilterChain securityFilterChain(HttpSecurity http, InitialAuthenticationFilter initialAuthenticationFilter)<\/em><\/strong><\/p>\n<figure class=\"full-width\">\n<div><figcaption>InitialAuthenticationFilter.java<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0442\u044c <strong>OncePerRequestFilter<\/strong> \u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0434\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 <\/p>\n<ul>\n<li>\n<p>doFilterInternal<\/p>\n<\/li>\n<li>\n<p>shouldNotFilter<\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u0444\u0438\u043b\u044c\u0442\u0440 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 <u>header<\/u> <strong>Authorization<\/strong>, \u0435\u0441\u043b\u0438 \u043e\u043d \u043f\u0443\u0441\u0442\u043e\u0439 \u0442\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043e \u043b\u0438 \u0432 \u0442\u0435\u043b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 JSON \u0441 \u0438\u043c\u0435\u043d\u0435\u043c \u0438 \u043f\u0430\u0440\u043e\u043b\u0435\u043c \u0434\u043b\u044f \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u0435\u0441\u043b\u0438 \u0432\u0441\u0435 \u043e\u043a \u0432\u044b\u0434\u0430\u0435\u0442 JWToken.<\/p>\n<p><strong>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441.<\/strong><\/p>\n<p>\u0414\u043b\u044f \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f JWToken \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0438 \u0437\u0430\u0438\u043d\u0436\u0435\u043a\u0442\u0438\u043c \u0434\u0432\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 <\/p>\n<p><strong><em>private final JwtService jwtService; <br \/>private final UsernamePasswordAuthenticationProvider authenticationProvider;<\/em><\/strong><\/p>\n<figure class=\"full-width\">\n<div><figcaption>JwtService.java<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 JWT \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <\/p>\n<figure class=\"\">\n<div><figcaption>pom.xml<\/figcaption><\/div>\n<\/figure>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043b\u044e\u0447 \u043f\u043e\u0434\u043f\u0438\u0441\u0438 \u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0441\u0430\u043c JWT.<\/p>\n<p><em>Keys.hmacShaKeyFor(signingKey.getBytes(StandardCharsets.UTF_8)); <\/em><\/p>\n<p>\u0448\u0438\u0444\u0440\u0443\u0435\u043c \u043a\u043b\u044e\u0447 BASE64<\/p>\n<p>\u0412 \u043c\u0435\u0442\u043e\u0434\u0435 <em>generatedJwt \u0441\u0442\u0440\u043e\u0438\u043c JWT. <\/em><\/p>\n<p>\u0417\u0430\u0434\u0430\u0435\u043c \u043f\u043e\u043b\u044f \u0432 payload \u0438 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443, \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0434\u0430\u0442\u0443 \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044f \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0438 \u043a\u043b\u044e\u0447 \u043f\u043e\u0434\u043f\u0438\u0441\u0438, \u0442\u0435\u043b\u043e \u043d\u0430\u0448\u0435\u0433\u043e JWT \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u0442\u044c \u0438\u0437 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u043f\u043e\u043b\u0435\u0439:<\/p>\n<pre><code class=\"json\">payload {    \"role\" : \"\",   \"user_id\" : \"\",   \"username\" : \"\",   \"exp\" : \"\",   \"sub\" : \"\"}<\/code><\/pre>\n<figure class=\"full-width\">\n<div><figcaption>UsernamePasswordAuthenticationProvider.java<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c \u043f\u0430\u0440\u043e\u043b\u044f \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e.<\/p>\n<p>\u041a\u043b\u0430\u0441\u0441 <strong>UserDetailsService<\/strong> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0435\u0441\u043b\u0438 \u043e\u043d \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u0432 \u0431\u0430\u0437\u0435<\/p>\n<figure class=\"full-width\">\n<div><figcaption>UserService.java<\/figcaption><\/div>\n<\/figure>\n<p>\u0412\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u043a \u043a\u043b\u0430\u0441\u0441\u0443 <strong>InitialAuthenticationFilter<\/strong>.<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 <strong><em>doFilterInternal<\/em><\/strong><em> <\/em><\/p>\n<ul>\n<li>\n<p>\u0438\u0437 request \u043c\u044b \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u043c JSON \u0441 \u043b\u043e\u0433\u0438\u043d\u043e\u043c \u0438 \u043f\u0430\u0440\u043e\u043b\u0435\u043c <\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e\u0435\u0440\u044f\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f <br \/><em>Authentication authentication = new UsernamePasswordAuthentication(username, password); authentication = authenticationProvider.authenticate(authentication);<\/em><\/p>\n<\/li>\n<li>\n<p>\u0435\u0441\u043b\u0438 \u0432\u0441\u0435 \u043e\u043a \u0432\u044b\u0434\u0430\u0435\u043c JWT \u0432  response \u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a <em>Authorization: Bearer *****<\/em><br \/>String jwt = jwtS<em>ervice.generatedJwt(authentication); response.setHeader(&#171;Authorization&#187;, HeaderValues.BEARER + jwt);<\/em><\/p>\n<\/li>\n<\/ul>\n<p>\u041c\u0435\u0442\u043e\u0434 <strong><em>shouldNotFilter<\/em><\/strong><\/p>\n<ul>\n<li>\n<p>\u041f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0444\u0438\u043b\u044c\u0442\u0440 \u043a \u043e\u0442\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c\u0443\/\u044b\u043c uri <br \/>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435  \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u0441\u044f \u043a \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c \u043d\u0430 uri \/login<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430<\/strong> <\/p>\n<pre><code class=\"bash\">curl -v -d '{\"username\":\"admin\", \"password\":\"123\"}' \\ -H \"Content-Type: application\/json\" \\ -X POST http:\/\/localhost:9090\/login<\/code><\/pre>\n<figure class=\"full-width\"><\/figure>\n<p>\u0412\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f <strong>header<\/strong> <\/p>\n<pre><code class=\"yaml\">Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJyb2xlIjpbIlJPTEVfQURNSU4iLCJST0xFX1VTRVIiXSwidXNlcl9pZCI6IjEiLCJ1c2VybmFtZSI6ImFkbWluIiwiZXhwIjoxNzAzMTU1NzAxLCJzdWIiOiJhZG1pbiJ9.aNHtaBa-7WDXO_MMl83MG9wxTO0MnMmEwdjgzSOrh0g<\/code><\/pre>\n<figure class=\"full-width\">\n<div><figcaption>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 JWToken<\/figcaption><\/div>\n<\/figure>\n<h3>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0447\u0430\u0441\u0442\u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/h3>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 <strong>JwtAuthorizationFilter<\/strong><\/p>\n<figure class=\"full-width\">\n<div><figcaption>JwtAuthorizationFilter.java<\/figcaption><\/div>\n<\/figure>\n<p>\u042d\u0442\u0430\u043f\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0444\u0438\u043b\u044c\u0442\u0440\u0430: <\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u0430 \u043a\u043e \u0432\u0441\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c \u043a\u0440\u043e\u043c\u0435 &#171;\/login&#187;<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 header Authorization \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 <\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0432\u0430\u043b\u0438\u0434\u043d\u043e\u0441\u0442\u0438 JWT<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0440\u043e\u043b\u0438<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/p>\n<\/li>\n<\/ul>\n<p>\u0417\u0430 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u0430 \u043a\u043e \u0432\u0441\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c \u043a\u0440\u043e\u043c\u0435 &#171;\/login&#187; \u0443 \u043d\u0430\u0441 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <strong><em>shouldNotFilter<\/em><\/strong>(HttpServletRequest request).<\/p>\n<p>\u0414\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0432\u0430\u043b\u0438\u0434\u043d\u043e\u0441\u0442\u0438 JWT \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043a\u043b\u0430\u0441\u0441 <strong>JwtService <\/strong>\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b:<\/p>\n<pre><code class=\"java\">@Override public Claims getClaims(String jwt) { return Jwts.parserBuilder() .setSigningKey(generatedSecretKey()) .build() .parseClaimsJws(jwt) .getBody(); }  @Override public boolean isValidJwt(Jwt jwt) { Claims claims = Jwts.parserBuilder() .setSigningKey(generatedSecretKey()) .build() .parseClaimsJws(jwt.getToken()) .getBody();  Optional&lt;User> user = userRepository.findByUsername(String.valueOf(claims.get(ClaimField.USERNAME)));  return claims.getExpiration().after(new Date()) &amp;&amp; user.isPresent(); }<\/code><\/pre>\n<p>\u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432 <strong>isValidJwt<\/strong> \u043c\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c, \u0438\u0441\u0442\u0435\u043a\u043b\u0430 \u043b\u0438 \u0434\u0430\u0442\u0430 \u0432\u044b\u0434\u0430\u0447\u0438. \u0415\u0441\u043b\u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u044c JWT \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0431\u0440\u043e\u0448\u0435\u043d\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435.<\/p>\n<p>\u0412 <strong>getClaims <\/strong>\u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043c\u0430\u043f\u0443<strong>&lt;String, Object><\/strong> \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u044b \u0432 JWT \u043f\u0440\u0438 \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043f\u0440\u0430\u0432\u0430 \u0438\u0437 JWT \u0438 \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c authentication.<\/p>\n<pre><code class=\"java\"> List&lt;GrantedAuthority> authorities = (List&lt;GrantedAuthority>) roles.stream()                .map(role -> new SimpleGrantedAuthority(role.toString()))                .collect(Collectors.toList());  UsernamePasswordAuthenticationToken authentication =                new UsernamePasswordAuthentication(username, null, authorities); SecurityContextHolder.getContext().setAuthentication(authentication);<\/code><\/pre>\n<ul>\n<li>\n<p><strong>SecurityContextHolder<\/strong>, \u0432 \u043d\u0435\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u044f \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435(Principal) \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0435\u043c \u0432 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u0441 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c.<\/p>\n<\/li>\n<li>\n<p> <strong>SecurityContext<\/strong>, \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 Authentication \u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u0443\u044e \u0441 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.<\/p>\n<\/li>\n<li>\n<p><strong>Authentication<\/strong> \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.<\/p>\n<\/li>\n<li>\n<p><strong>GrantedAuthority<\/strong> \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u0442 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0432\u044b\u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0432 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0435 \u0432\u0441\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 ROLE_USER, ROLE_ADMIN.<\/p>\n<\/li>\n<\/ul>\n<p>\u0422.\u043a. SecurityContextHolder \u0434\u043e\u043b\u0436\u0435\u043d \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0438\u0437\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e\u0431 Authentication, \u0442\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0435 \u0432 SecurityContext. \u0422\u0435\u043f\u0435\u0440\u044c \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u044b \u0442\u0440\u0435\u0431\u0443\u044e\u0449\u0438\u0435 Authentication \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0443 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0442\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u043c \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u044b.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0447\u0442\u043e \u0431\u044b \u043d\u0430\u0448 \u0444\u0438\u043b\u044c\u0442\u0440 \u0437\u0430\u0440\u0430\u0431\u043e\u0442\u0430\u043b \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432<\/p>\n<figure class=\"full-width\">\n<div><figcaption>WebConfiguration.java<\/figcaption><\/div>\n<\/figure>\n<pre><code class=\"java\">@Bean public SecurityFilterChain securityFilterChain(HttpSecurity http, InitialAuthenticationFilter initialAuthenticationFilter, JwtAuthorizationFilter jwtAuthorizationFilter) throws Exception {  http           .addFilterAt(initialAuthenticationFilter, BasicAuthenticationFilter.class)           .addFilterAt(jwtAuthorizationFilter, BasicAuthenticationFilter.class);  \/\/ *******************************     \/\/ Some code       \/\/ *******************************  return http.build(); } } <\/code><\/pre>\n<p>\u0414\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043e\u0442\u043d\u0440\u043e\u043b\u043b\u0435\u0440 <strong>UserController<\/strong><\/p>\n<figure class=\"full-width\">\n<div><figcaption>UserController.java<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u043e\u0441\u0442\u0443\u043f \u043f\u043e \u043f\u0440\u0430\u0432\u0430\u043c \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 FilterChain, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438<strong><a class=\"mention\" href=\"\/users\/preauthorize.\">@PreAuthorize.<\/a><\/strong><\/p>\n<p>\u0427\u0442\u043e \u0431\u044b \u0434\u0430\u043d\u043d\u0430\u044f \u0430\u043d\u043e\u0442\u0430\u0446\u0438\u044f \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e <strong><a class=\"mention\" href=\"\/users\/EnableMethodSecurity\">@EnableMethodSecurity<\/a><\/strong><\/p>\n<figure class=\"full-width\">\n<div><figcaption>SpringSecurityJwtApplication.java<\/figcaption><\/div>\n<\/figure>\n<h4>\u041f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c<\/h4>\n<ul>\n<li>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441 \u0431\u0435\u0437 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"bash\">curl -v http:\/\/localhost:9090\/user\/get\/myname <\/code><\/pre>\n<figure class=\"full-width\">\n<div><figcaption>\u0417\u0430\u043f\u0440\u043e\u0441 \u0431\u0435\u0437 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043e\u0442\u0432\u0435\u0442 403 Forbidden<\/p>\n<ul>\n<li>\n<p>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0443\u0435\u043c\u0441\u044f \u043f\u043e\u0434 <strong>User<\/strong> <\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0442\u043e\u043a\u0435\u043d<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0442\u043e\u043a\u0435\u043d<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u043c\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c<br \/><a href=\"http:\/\/localhost:9090\/user\/get\/myname\" rel=\"noopener noreferrer nofollow\">http:\/\/localhost:9090\/user\/get\/myname<\/a><\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u043c\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0434\u0430\u043d\u043d\u044b\u043c \u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 <br \/><a href=\"http:\/\/localhost:9090\/user\/get\/all\" rel=\"noopener noreferrer nofollow\">http:\/\/localhost:9090\/user\/get\/all<\/a><\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0434\u0430\u043d\u043d\u044b\u043c \u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c response 403 forbidden<\/figcaption><\/div>\n<\/figure>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c response 403 forbidden. \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442. <\/p>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 \u043a\u0430\u043a \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438, \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438, \u0432\u044b\u0434\u0430\u0447\u0438 JWT. <\/p>\n<p><strong>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u043e\u0435\u043a\u0442: <\/strong><a href=\"https:\/\/gitlab.com\/SilverRid\/SpringSecurityJWT\" rel=\"noopener noreferrer nofollow\"><strong>GitLab<\/strong><\/a><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/781066\/\"> https:\/\/habr.com\/ru\/articles\/781066\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-363118","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/363118","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=363118"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/363118\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=363118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=363118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=363118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}