{"id":337450,"date":"2022-08-23T21:00:27","date_gmt":"2022-08-23T21:00:27","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=337450"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=337450","title":{"rendered":"<span>JSON Web Token (JWT) \u2014 \u043f\u0440\u0438\u043c\u0435\u0440 Java \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430 Spring Boot OAuth2 Resource Server 6.0<\/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>\u0414\u043e\u0431\u0440\u043e\u0435 \u0432\u0440\u0435\u043c\u044f!<\/p>\n<p>\u0427\u0430\u0441\u0442\u044c 2-\u044f \u043f\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u043c\u0443 \u0437\u0430\u043d\u044f\u0442\u0438\u044e \u043d\u043e\u0432\u043e\u0433\u043e \u0443\u0447\u0435\u0431\u043d\u043e\u0433\u043e \u043a\u0443\u0440\u0441\u0430: \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e JWT \u0447\u0435\u0440\u0435\u0437 \u043d\u043e\u0432\u044b\u0439 Spring Boot OAuth2 Resource Server (\u043f\u0435\u0440\u0432\u0430\u044f \u0447\u0430\u0441\u0442\u044c: <a href=\"https:\/\/habr.com\/ru\/post\/683936\/\" rel=\"noopener noreferrer nofollow\">Spring Boot 3.0 \u2014 \u0433\u043e\u0442\u043e\u0432\u0438\u043c\u0441\u044f \u0437\u0430\u0440\u0430\u043d\u0435\u0435<\/a>). \u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 JWT \u0438 \u0437\u0430\u0447\u0435\u043c, \u043f\u0438\u0441\u0430\u0442\u044c \u0437\u0434\u0435\u0441\u044c \u043d\u0435 \u0431\u0443\u0434\u0443  &#8212; \u0432 \u0441\u0435\u0442\u0438 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u043e\u0432 \u043c\u043d\u043e\u0433\u043e, \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u0437\u043d\u0430\u043a\u043e\u043c\u0441\u0442\u0432\u043e \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0441 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/JSON_Web_Token\" rel=\"noopener noreferrer nofollow\">\u0412\u0438\u043a\u0438\u043f\u0435\u0434\u0438\u0438<\/a>. \u0410 \u0432\u043e\u0442 <a href=\"https:\/\/gist.github.com\/zmts\/802dc9c3510d79fd40f9dc38a12bccfc\" rel=\"noopener noreferrer nofollow\">\u0445\u043e\u0440\u043e\u0448\u0430\u044f \u0441\u0441\u044b\u043b\u043a\u0430<\/a> \u043f\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 JWT+OAuth2. \u0417\u0434\u0435\u0441\u044c \u044f \u043f\u0440\u0438\u0432\u043e\u0436\u0443 Java \u043a\u043e\u0434, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 spring-projects &#8212; \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 <a href=\"https:\/\/github.com\/spring-projects\/spring-security-samples\/tree\/main\/servlet\/spring-boot\/java\/jwt\/login\" rel=\"noopener noreferrer nofollow\">JWT Login Sample<\/a> (\u0431\u0435\u0437 refresh token \u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430), &#171;\u0442\u0432\u043e\u0440\u0447\u0435\u0441\u043a\u0438 \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u0439&#187; \u0438 \u0441 \u043c\u043e\u0438\u043c\u0438 \u043f\u043e\u044f\u0441\u043d\u0435\u043d\u0438\u044f\u043c\u0438. \u0415\u0449\u0435 \u0440\u0430\u0437 &#8212; <strong>\u0431\u0435\u0437 \u0442\u0435\u043e\u0440\u0438\u0438<\/strong>, \u0434\u043b\u044f \u0442\u0435\u0445, \u043a\u043e\u043c\u0443 <strong>\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d \u043a\u043e\u0434 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 Java \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/strong>. \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0412\u044b &#8212; \u043f\u0440\u043e\u0448\u0443 \u043a \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u044e.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/539\/440\/ff7\/539440ff7c97b8bab537774e2a57089f.png\" width=\"1398\" height=\"742\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/539\/440\/ff7\/539440ff7c97b8bab537774e2a57089f.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043e\u0441\u043d\u043e\u0432\u044b, \u043a\u0443\u0440\u0441 \u0441\u0442\u0430\u0440\u0442\u0443\u0435\u0442 \u0441 \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 Spring Boot 2.x + HATEOAS: Basic \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0440\u043e\u043b\u0435\u0439, \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438, \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0432\u043e\u0438\u043c \u043f\u0440\u043e\u0444\u0438\u043b\u0435\u043c \u0438 \u0430\u0434\u043c\u0438\u043d\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 (<a href=\"https:\/\/github.com\/JavaOPs\/bootjava\/tree\/patched\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u0434 \u043d\u0430 GitHub<\/a>). \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f JWT \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f <a href=\"https:\/\/github.com\/JavaOPs\/cloudjava\/commits\/patched\" rel=\"noopener noreferrer nofollow\">\u0432 \u0432\u0435\u0442\u043a\u0435 patched \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 3\u0433\u043e \u043a\u043e\u043c\u0438\u0442\u0430<\/a>. \u0414\u043b\u044f \u0440\u0430\u0437\u0431\u043e\u0440\u0430 \u043a\u043e\u0434\u0430 \u043f\u0440\u043e\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0432\u044b\u0447\u0435\u043a\u0430\u0442\u044c \u0435\u0433\u043e \u043a \u0441\u0435\u0431\u0435:<\/p>\n<p><code>git clone --branch patched https:\/\/github.com\/JavaOPs\/cloudjava<\/code><\/p>\n<h3>\u041a\u043e\u043c\u0438\u0442 1_03_jwt_token: \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 JWT \u0442\u043e\u043a\u0435\u043d\u0430<\/h3>\n<p>\u0414\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0441\u0441\u0438\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0435 RS256 \u043a\u043b\u044e\u0447\u0438: \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043f\u043e\u0434\u043b\u0438\u043d\u043d\u043e\u0441\u0442\u044c \u0442\u043e\u043a\u0435\u043d\u0430 \u043c\u043e\u0436\u0435\u0442 \u043b\u044e\u0431\u043e\u0439, \u0430 \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u0442\u044c \u0435\u0433\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0442, \u0443 \u043a\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0439 \u043a\u043b\u044e\u0447. \u041a\u043b\u044e\u0447\u0438 \u043c\u043e\u0436\u043d\u043e \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u0438 \u0438\u043b\u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0438\u0445 \u0441\u0432\u043e\u0438\u043c\u0438 (<a href=\"https:\/\/stackoverflow.com\/questions\/51055884\/548473\" rel=\"noopener noreferrer nofollow\"><u>generate PKCS#8 private key with openssl<\/u><\/a>):<\/p>\n<pre><code class=\"bash\">openssl genpkey -out jwt.pem -algorithm RSA -pkeyopt rsa_keygen_bits:2048 - \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0439 \u043a\u043b\u044e\u0447 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 PKCS8\u043f\u0438\u0441\u0438 openssl rsa -in jwt.pem -pubout -out jwt.pub - \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0439 \u043a\u043b\u044e\u0447 \u0434\u043b\u044f \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f<\/code><\/pre>\n<ul>\n<li>\n<p>\u041f\u043e\u043c\u0435\u0441\u0442\u0438\u043c \u043a\u043b\u044e\u0447\u0438 \u0432\u00a0<code>\\src\\main\\resources\\keys<\/code><\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c\u00a0<code>org.springframework.boot:spring-boot-starter-oauth2-resource-server<\/code><\/p>\n<\/li>\n<li>\n<p>\u041e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u043a\u043b\u044e\u0447\u0438 \u0432\u00a0<code>application.yaml<\/code>\u00a0\u0438 \u0438\u043d\u0436\u0435\u043a\u0442\u0438\u043c  \u0432  <code>JwtSecurityConfiguration<\/code>. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043b\u044e\u0447\u0435\u0439 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0431\u0438\u043d\u044b \u00a0<code>JwtDecoder\/JwtEncoder<\/code><\/p>\n<\/li>\n<li>\n<p><code>SecurityConfiguration<\/code>\u00a0\u043f\u0435\u0440\u0435\u0438\u043c\u0435\u043d\u043e\u0432\u044b\u0432\u0430\u0435\u043c \u0432 \u00a0<code>LoginSecurityConfiguration<\/code>. <code>httpBasic<\/code>\u00a0\u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f JWT<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c\u00a0<code>TokenController<\/code>, \u0433\u0434\u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c JWT. \u0414\u0435\u0442\u0430\u043b\u0438 \u0437\u0430\u043b\u043e\u0433\u0438\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f <code>AuthUser<\/code>\u00a0\u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e \u0432 JWT \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c\u00a0<code>JwtUtil#addUserDetails<\/code><\/p>\n<\/li>\n<li>\n<p>&#171;\u0412\u044b\u043a\u043b\u044e\u0447\u0430\u0435\u043c&#187; \u0442\u0435\u0441\u0442\u044b \u0447\u0435\u0440\u0435\u0437 Junit5 <code>@Disabled<\/code><\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041f\u0435\u0440\u0435\u0434 \u0441\u0431\u043e\u0440\u043a\u043e\u0439 \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c\u00a0<em>gradle clean<\/em>. \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 JWT:<\/strong><\/p>\n<pre><code class=\"yaml\">### JetBrains Tools->HTTP Client POST http:\/\/localhost:8080\/token Authorization: Basic admin@javaops.ru admin<\/code><\/pre>\n<p>\u0412 \u043e\u0442\u0432\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u043b\u0438\u043d\u043d\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0435 \u0442\u0438\u043f\u0430\u00a0<code>eyJhbGciOiJSUzI1NiJ9..<\/code>.\u041e\u043d\u0430 \u0445\u043e\u0440\u043e\u0448\u043e \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0438\u0437 JetBrains HTTP Client. \u0412 Unix \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c\u00a0<code>export<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u043a\u043e\u043c\u0438\u0442\u0430:<\/p>\n<pre><code class=\"bash\">export JWT=`curl -XPOST -u admin@javaops.ru:admin localhost:8080\/token`<\/code><\/pre>\n<h3>1_04_jwt_api: \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u043c API \u043d\u0430 JWT:<\/h3>\n<ul>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c\u00a0<code>JwtUser<\/code>, \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0435 JWT \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438\u0437 \u00a0<code>JwtUtil#createJwtUser<\/code>. \u0424\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u043f\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044e \u0434\u0435\u0442\u0430\u043b\u0435\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432 JWT \u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044e \u0438\u0445 \u0434\u043e\u043b\u0436\u0435\u043d \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0440\u044f\u0434\u043e\u043c.<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u00a0<code>JwtSecurityConfiguration<\/code>\u00a0<a href=\"https:\/\/stackoverflow.com\/questions\/54706291\/548473\" rel=\"noopener noreferrer nofollow\">\u0432\u0442\u043e\u0440\u0443\u044e \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e AAA \u043f\u043e JWT<\/a>\u00a0(\u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432 \u043d\u0435 \u0432\u0430\u0436\u0435\u043d). <a href=\"https:\/\/stackoverflow.com\/a\/73423923\/548473\" rel=\"noopener noreferrer nofollow\">\u041a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439\u00a0<\/a> <code>JwtAuthenticationToken<\/code>\u00a0\u0432\u00a0<code>JwtUser<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u0412 \u043c\u0435\u0442\u043e\u0434\u044b\u00a0<code>ProfileController<\/code>\u00a0\u0438\u043d\u0436\u0435\u043a\u0442\u0438\u043c \u043d\u0430\u0448 principal object\u00a0<code>JwtUser<\/code>\u00a0(\u0441\u00a0<code>@AuthenticationPrincipal<\/code>\u00a0\u0443 \u043c\u0435\u043d\u044f \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442). \u0412 \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c, \u0434\u043e\u0441\u0442\u0430\u0435\u043c \u0435\u0433\u043e \u0438\u0437 \u0431\u0430\u0437\u044b:\u00a0<code>AbstractUserController#findByJwtUser<\/code>\u00a0(\u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 API).<\/p>\n<\/li>\n<li>\n<p>\u0412\u00a0<em>SecurityContext<\/em>\u00a0\u043f\u0440\u0438 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438 \u043a API \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043f\u0430\u0434\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u00a0<code>JwtUser<\/code>, \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043c\u00a0<code>SecurityUtil<\/code><\/p>\n<\/li>\n<\/ul>\n<p><strong><em>gradle clean<\/em>\u00a0\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c API: \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 JWT \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u043c\u0435\u0441\u0442\u043e\u00a0<em>[JWT_TOKEN]<\/em>:<\/strong><\/p>\n<pre><code class=\"yaml\">GET http:\/\/localhost:8080\/api\/profile Authorization: Bearer [JWT_TOKEN] \u0438\u043b\u0438 curl -H \"Authorization: Bearer $JWT\" localhost:8080\/api\/profile<\/code><\/pre>\n<h3>1_05_jwt_test: \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0442\u0435\u0441\u0442\u044b<\/h3>\n<p>\u041a\u0430\u043a \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f\u00a0<a href=\"https:\/\/docs.spring.io\/spring-security\/reference\/servlet\/test\/mockmvc\/oauth2.html#testing-jwt\" rel=\"noopener noreferrer nofollow\"><u>jwt() \u043f\u043e\u0434\u043c\u0435\u043d\u043e\u0439<\/u><\/a>. \u041d\u043e \u044f \u0434\u0435\u043b\u0430\u044e \u0432\u0441\u0435 \u0447\u0435\u0441\u0442\u043d\u043e, \u0441 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435\u043c \u0442\u043e\u043a\u0435\u043d\u0430:<\/p>\n<ul>\n<li>\n<p>\u0412\u00a0<code>AbstractControllerTest<\/code>\u00a0\u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b\u00a0<code>getJWT<\/code>\u00a0\u0441 Basic \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0435\u0439<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0442\u0435\u0441\u0442\u044b \u043d\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f JWT:\u00a0<code>TokenControllerTest<\/code><\/p>\n<\/li>\n<li>\n<p>\u0414\u043b\u044f API \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439\u00a0<code>AbstractControllerTest#performJwt<\/code>\u00a0\u0441 \u0430\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u043c\u00a0<em>Bearer<\/em>\u00a0\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u043e\u043c. \u0427\u0442\u043e\u0431\u044b \u0442\u043e\u043a\u0435\u043d\u044b \u043d\u0435 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0442\u044c \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0442\u0435\u0441\u0442\u0435, \u043a\u044d\u0448\u0438\u0440\u0443\u0435\u043c \u0438\u0445 \u0432\u00a0<code>AbstractControllerTest#userJwtMap<\/code>\u00a0(\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0435 \u043d\u0443\u0436\u043d\u0430 &#8212; \u043d\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430, \u0435\u0441\u043b\u0438 \u0434\u0430\u0436\u0435 \u043e\u043d\u0438 \u043f\u0435\u0440\u0435\u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u0441\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437)<\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043c \u0442\u0435\u0441\u0442\u044b API \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u044d\u0442\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432. \u0413\u0434\u0435 \u043d\u0435\u0442 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438, \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f \u0441\u0442\u0430\u0440\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b\u00a0<code>perform<\/code><\/p>\n<\/li>\n<\/ul>\n<p><strong><em>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c\u0441\u044f: gradle test<\/em><\/strong><\/p>\n<p>\u041a\u043e\u0434 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439, \u043c\u043e\u0436\u043d\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u0430, \u043d\u043e, \u0435\u0441\u043b\u0438 \u0441\u0434\u0435\u043b\u0430\u0435\u0442\u0435, \u0431\u0443\u0434\u0443 \u043f\u0440\u0438\u0437\u043d\u0430\u0442\u0435\u043b\u0435\u043d:) \u0415\u0441\u043b\u0438 \u043a\u043e\u0434 \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f &#8212;  \u043f\u0440\u043e\u0433\u043e\u043b\u043e\u0441\u0443\u0439\u0442\u0435 &#171;\u0437\u0430&#187;, \u0435\u0441\u043b\u0438 \u043d\u0435\u0442 &#8212; \u043d\u0430\u043f\u0438\u0448\u0438\u0442\u0435 \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438. \u0415\u0441\u043b\u0438 \u0432\u044b \u0436\u0434\u0430\u043b\u0438 \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u0439 \u0440\u0430\u0431\u043e\u0442\u044b JWT &#8212; \u043d\u0435 \u043d\u0430\u0434\u043e \u043c\u0438\u043d\u0443\u0441\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0430\u0442\u044c\u044e &#8212; \u0432\u044b \u043f\u043b\u043e\u0445\u043e \u043f\u0440\u043e\u0447\u043b\u0438 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e \u043a \u043d\u0435\u0439.<\/p>\n<p>\u041f\u0440\u0438\u044f\u0442\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0438\u043d\u0433\u0430!<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/684270\/\"> https:\/\/habr.com\/ru\/post\/684270\/<\/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>\u0414\u043e\u0431\u0440\u043e\u0435 \u0432\u0440\u0435\u043c\u044f!<\/p>\n<p>\u0427\u0430\u0441\u0442\u044c 2-\u044f \u043f\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u043c\u0443 \u0437\u0430\u043d\u044f\u0442\u0438\u044e \u043d\u043e\u0432\u043e\u0433\u043e \u0443\u0447\u0435\u0431\u043d\u043e\u0433\u043e \u043a\u0443\u0440\u0441\u0430: \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e JWT \u0447\u0435\u0440\u0435\u0437 \u043d\u043e\u0432\u044b\u0439 Spring Boot OAuth2 Resource Server (\u043f\u0435\u0440\u0432\u0430\u044f \u0447\u0430\u0441\u0442\u044c: <a href=\"https:\/\/habr.com\/ru\/post\/683936\/\" rel=\"noopener noreferrer nofollow\">Spring Boot 3.0 \u2014 \u0433\u043e\u0442\u043e\u0432\u0438\u043c\u0441\u044f \u0437\u0430\u0440\u0430\u043d\u0435\u0435<\/a>). \u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 JWT \u0438 \u0437\u0430\u0447\u0435\u043c, \u043f\u0438\u0441\u0430\u0442\u044c \u0437\u0434\u0435\u0441\u044c \u043d\u0435 \u0431\u0443\u0434\u0443  &#8212; \u0432 \u0441\u0435\u0442\u0438 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u043e\u0432 \u043c\u043d\u043e\u0433\u043e, \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u0437\u043d\u0430\u043a\u043e\u043c\u0441\u0442\u0432\u043e \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0441 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/JSON_Web_Token\" rel=\"noopener noreferrer nofollow\">\u0412\u0438\u043a\u0438\u043f\u0435\u0434\u0438\u0438<\/a>. \u0410 \u0432\u043e\u0442 <a href=\"https:\/\/gist.github.com\/zmts\/802dc9c3510d79fd40f9dc38a12bccfc\" rel=\"noopener noreferrer nofollow\">\u0445\u043e\u0440\u043e\u0448\u0430\u044f \u0441\u0441\u044b\u043b\u043a\u0430<\/a> \u043f\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 JWT+OAuth2. \u0417\u0434\u0435\u0441\u044c \u044f \u043f\u0440\u0438\u0432\u043e\u0436\u0443 Java \u043a\u043e\u0434, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 spring-projects &#8212; \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 <a href=\"https:\/\/github.com\/spring-projects\/spring-security-samples\/tree\/main\/servlet\/spring-boot\/java\/jwt\/login\" rel=\"noopener noreferrer nofollow\">JWT Login Sample<\/a> (\u0431\u0435\u0437 refresh token \u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430), &#171;\u0442\u0432\u043e\u0440\u0447\u0435\u0441\u043a\u0438 \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u0439&#187; \u0438 \u0441 \u043c\u043e\u0438\u043c\u0438 \u043f\u043e\u044f\u0441\u043d\u0435\u043d\u0438\u044f\u043c\u0438. \u0415\u0449\u0435 \u0440\u0430\u0437 &#8212; <strong>\u0431\u0435\u0437 \u0442\u0435\u043e\u0440\u0438\u0438<\/strong>, \u0434\u043b\u044f \u0442\u0435\u0445, \u043a\u043e\u043c\u0443 <strong>\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d \u043a\u043e\u0434 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 Java \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/strong>. \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0412\u044b &#8212; \u043f\u0440\u043e\u0448\u0443 \u043a \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u044e.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043e\u0441\u043d\u043e\u0432\u044b, \u043a\u0443\u0440\u0441 \u0441\u0442\u0430\u0440\u0442\u0443\u0435\u0442 \u0441 \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 Spring Boot 2.x + HATEOAS: Basic \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0440\u043e\u043b\u0435\u0439, \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438, \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0432\u043e\u0438\u043c \u043f\u0440\u043e\u0444\u0438\u043b\u0435\u043c \u0438 \u0430\u0434\u043c\u0438\u043d\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 (<a href=\"https:\/\/github.com\/JavaOPs\/bootjava\/tree\/patched\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u0434 \u043d\u0430 GitHub<\/a>). \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f JWT \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f <a href=\"https:\/\/github.com\/JavaOPs\/cloudjava\/commits\/patched\" rel=\"noopener noreferrer nofollow\">\u0432 \u0432\u0435\u0442\u043a\u0435 patched \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 3\u0433\u043e \u043a\u043e\u043c\u0438\u0442\u0430<\/a>. \u0414\u043b\u044f \u0440\u0430\u0437\u0431\u043e\u0440\u0430 \u043a\u043e\u0434\u0430 \u043f\u0440\u043e\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0432\u044b\u0447\u0435\u043a\u0430\u0442\u044c \u0435\u0433\u043e \u043a \u0441\u0435\u0431\u0435:<\/p>\n<p><code>git clone --branch patched https:\/\/github.com\/JavaOPs\/cloudjava<\/code><\/p>\n<h3>\u041a\u043e\u043c\u0438\u0442 1_03_jwt_token: \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 JWT \u0442\u043e\u043a\u0435\u043d\u0430<\/h3>\n<p>\u0414\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0441\u0441\u0438\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0435 RS256 \u043a\u043b\u044e\u0447\u0438: \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043f\u043e\u0434\u043b\u0438\u043d\u043d\u043e\u0441\u0442\u044c \u0442\u043e\u043a\u0435\u043d\u0430 \u043c\u043e\u0436\u0435\u0442 \u043b\u044e\u0431\u043e\u0439, \u0430 \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u0442\u044c \u0435\u0433\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0442, \u0443 \u043a\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0439 \u043a\u043b\u044e\u0447. \u041a\u043b\u044e\u0447\u0438 \u043c\u043e\u0436\u043d\u043e \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u0438 \u0438\u043b\u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0438\u0445 \u0441\u0432\u043e\u0438\u043c\u0438 (<a href=\"https:\/\/stackoverflow.com\/questions\/51055884\/548473\" rel=\"noopener noreferrer nofollow\"><u>generate PKCS#8 private key with openssl<\/u><\/a>):<\/p>\n<pre><code class=\"bash\">openssl genpkey -out jwt.pem -algorithm RSA -pkeyopt rsa_keygen_bits:2048 - \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0439 \u043a\u043b\u044e\u0447 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 PKCS8\u043f\u0438\u0441\u0438 openssl rsa -in jwt.pem -pubout -out jwt.pub - \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0439 \u043a\u043b\u044e\u0447 \u0434\u043b\u044f \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f<\/code><\/pre>\n<ul>\n<li>\n<p>\u041f\u043e\u043c\u0435\u0441\u0442\u0438\u043c \u043a\u043b\u044e\u0447\u0438 \u0432\u00a0<code>\\src\\main\\resources\\keys<\/code><\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c\u00a0<code>org.springframework.boot:spring-boot-starter-oauth2-resource-server<\/code><\/p>\n<\/li>\n<li>\n<p>\u041e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u043a\u043b\u044e\u0447\u0438 \u0432\u00a0<code>application.yaml<\/code>\u00a0\u0438 \u0438\u043d\u0436\u0435\u043a\u0442\u0438\u043c  \u0432  <code>JwtSecurityConfiguration<\/code>. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043b\u044e\u0447\u0435\u0439 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0431\u0438\u043d\u044b \u00a0<code>JwtDecoder\/JwtEncoder<\/code><\/p>\n<\/li>\n<li>\n<p><code>SecurityConfiguration<\/code>\u00a0\u043f\u0435\u0440\u0435\u0438\u043c\u0435\u043d\u043e\u0432\u044b\u0432\u0430\u0435\u043c \u0432 \u00a0<code>LoginSecurityConfiguration<\/code>. <code>httpBasic<\/code>\u00a0\u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f JWT<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c\u00a0<code>TokenController<\/code>, \u0433\u0434\u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c JWT. \u0414\u0435\u0442\u0430\u043b\u0438 \u0437\u0430\u043b\u043e\u0433\u0438\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f <code>AuthUser<\/code>\u00a0\u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e \u0432 JWT \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c\u00a0<code>JwtUtil#addUserDetails<\/code><\/p>\n<\/li>\n<li>\n<p>&#171;\u0412\u044b\u043a\u043b\u044e\u0447\u0430\u0435\u043c&#187; \u0442\u0435\u0441\u0442\u044b \u0447\u0435\u0440\u0435\u0437 Junit5 <code>@Disabled<\/code><\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041f\u0435\u0440\u0435\u0434 \u0441\u0431\u043e\u0440\u043a\u043e\u0439 \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c\u00a0<em>gradle clean<\/em>. \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 JWT:<\/strong><\/p>\n<pre><code class=\"yaml\">### JetBrains Tools->HTTP Client POST http:\/\/localhost:8080\/token Authorization: Basic admin@javaops.ru admin<\/code><\/pre>\n<p>\u0412 \u043e\u0442\u0432\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u043b\u0438\u043d\u043d\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0435 \u0442\u0438\u043f\u0430\u00a0<code>eyJhbGciOiJSUzI1NiJ9..<\/code>.\u041e\u043d\u0430 \u0445\u043e\u0440\u043e\u0448\u043e \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0438\u0437 JetBrains HTTP Client. \u0412 Unix \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c\u00a0<code>export<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u043a\u043e\u043c\u0438\u0442\u0430:<\/p>\n<pre><code class=\"bash\">export JWT=`curl -XPOST -u admin@javaops.ru:admin localhost:8080\/token`<\/code><\/pre>\n<h3>1_04_jwt_api: \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u043c API \u043d\u0430 JWT:<\/h3>\n<ul>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c\u00a0<code>JwtUser<\/code>, \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0435 JWT \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438\u0437 \u00a0<code>JwtUtil#createJwtUser<\/code>. \u0424\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u043f\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044e \u0434\u0435\u0442\u0430\u043b\u0435\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432 JWT \u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044e \u0438\u0445 \u0434\u043e\u043b\u0436\u0435\u043d \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0440\u044f\u0434\u043e\u043c.<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u00a0<code>JwtSecurityConfiguration<\/code>\u00a0<a href=\"https:\/\/stackoverflow.com\/questions\/54706291\/548473\" rel=\"noopener noreferrer nofollow\">\u0432\u0442\u043e\u0440\u0443\u044e \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e AAA \u043f\u043e JWT<\/a>\u00a0(\u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432 \u043d\u0435 \u0432\u0430\u0436\u0435\u043d). <a href=\"https:\/\/stackoverflow.com\/a\/73423923\/548473\" rel=\"noopener noreferrer nofollow\">\u041a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439\u00a0<\/a> <code>JwtAuthenticationToken<\/code>\u00a0\u0432\u00a0<code>JwtUser<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u0412 \u043c\u0435\u0442\u043e\u0434\u044b\u00a0<code>ProfileController<\/code>\u00a0\u0438\u043d\u0436\u0435\u043a\u0442\u0438\u043c \u043d\u0430\u0448 principal object\u00a0<code>JwtUser<\/code>\u00a0(\u0441\u00a0<code>@AuthenticationPrincipal<\/code>\u00a0\u0443 \u043c\u0435\u043d\u044f \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442). \u0412 \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c, \u0434\u043e\u0441\u0442\u0430\u0435\u043c \u0435\u0433\u043e \u0438\u0437 \u0431\u0430\u0437\u044b:\u00a0<code>AbstractUserController#findByJwtUser<\/code>\u00a0(\u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 API).<\/p>\n<\/li>\n<li>\n<p>\u0412\u00a0<em>SecurityContext<\/em>\u00a0\u043f\u0440\u0438 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438 \u043a API \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043f\u0430\u0434\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u00a0<code>JwtUser<\/code>, \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043c\u00a0<code>SecurityUtil<\/code><\/p>\n<\/li>\n<\/ul>\n<p><strong><em>gradle clean<\/em>\u00a0\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c API: \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 JWT \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u043c\u0435\u0441\u0442\u043e\u00a0<em>[JWT_TOKEN]<\/em>:<\/strong><\/p>\n<pre><code class=\"yaml\">GET http:\/\/localhost:8080\/api\/profile Authorization: Bearer [JWT_TOKEN] \u0438\u043b\u0438 curl -H \"Authorization: Bearer $JWT\" localhost:8080\/api\/profile<\/code><\/pre>\n<h3>1_05_jwt_test: \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0442\u0435\u0441\u0442\u044b<\/h3>\n<p>\u041a\u0430\u043a \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f\u00a0<a href=\"https:\/\/docs.spring.io\/spring-security\/reference\/servlet\/test\/mockmvc\/oauth2.html#testing-jwt\" rel=\"noopener noreferrer nofollow\"><u>jwt() \u043f\u043e\u0434\u043c\u0435\u043d\u043e\u0439<\/u><\/a>. \u041d\u043e \u044f \u0434\u0435\u043b\u0430\u044e \u0432\u0441\u0435 \u0447\u0435\u0441\u0442\u043d\u043e, \u0441 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435\u043c \u0442\u043e\u043a\u0435\u043d\u0430:<\/p>\n<ul>\n<li>\n<p>\u0412\u00a0<code>AbstractControllerTest<\/code>\u00a0\u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b\u00a0<code>getJWT<\/code>\u00a0\u0441 Basic \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0435\u0439<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0442\u0435\u0441\u0442\u044b \u043d\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f JWT:\u00a0<code>TokenControllerTest<\/code><\/p>\n<\/li>\n<li>\n<p>\u0414\u043b\u044f API \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439\u00a0<code>AbstractControllerTest#performJwt<\/code>\u00a0\u0441 \u0430\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u043c\u00a0<em>Bearer<\/em>\u00a0\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u043e\u043c. \u0427\u0442\u043e\u0431\u044b \u0442\u043e\u043a\u0435\u043d\u044b \u043d\u0435 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0442\u044c \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0442\u0435\u0441\u0442\u0435, \u043a\u044d\u0448\u0438\u0440\u0443\u0435\u043c \u0438\u0445 \u0432\u00a0<code>AbstractControllerTest#userJwtMap<\/code>\u00a0(\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0435 \u043d\u0443\u0436\u043d\u0430 &#8212; \u043d\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430, \u0435\u0441\u043b\u0438 \u0434\u0430\u0436\u0435 \u043e\u043d\u0438 \u043f\u0435\u0440\u0435\u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u0441\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437)<\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043c \u0442\u0435\u0441\u0442\u044b API \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u044d\u0442\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432. \u0413\u0434\u0435 \u043d\u0435\u0442 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438, \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f \u0441\u0442\u0430\u0440\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b\u00a0<code>perform<\/code><\/p>\n<\/li>\n<\/ul>\n<p><strong><em>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c\u0441\u044f: gradle test<\/em><\/strong><\/p>\n<p>\u041a\u043e\u0434 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439, \u043c\u043e\u0436\u043d\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u0430, \u043d\u043e, \u0435\u0441\u043b\u0438 \u0441\u0434\u0435\u043b\u0430\u0435\u0442\u0435, \u0431\u0443\u0434\u0443 \u043f\u0440\u0438\u0437\u043d\u0430\u0442\u0435\u043b\u0435\u043d:) \u0415\u0441\u043b\u0438 \u043a\u043e\u0434 \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f &#8212;  \u043f\u0440\u043e\u0433\u043e\u043b\u043e\u0441\u0443\u0439\u0442\u0435 &#171;\u0437\u0430&#187;, \u0435\u0441\u043b\u0438 \u043d\u0435\u0442 &#8212; \u043d\u0430\u043f\u0438\u0448\u0438\u0442\u0435 \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438. \u0415\u0441\u043b\u0438 \u0432\u044b \u0436\u0434\u0430\u043b\u0438 \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u0439 \u0440\u0430\u0431\u043e\u0442\u044b JWT &#8212; \u043d\u0435 \u043d\u0430\u0434\u043e \u043c\u0438\u043d\u0443\u0441\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0430\u0442\u044c\u044e &#8212; \u0432\u044b \u043f\u043b\u043e\u0445\u043e \u043f\u0440\u043e\u0447\u043b\u0438 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e \u043a \u043d\u0435\u0439.<\/p>\n<p>\u041f\u0440\u0438\u044f\u0442\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0438\u043d\u0433\u0430!<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/684270\/\"> https:\/\/habr.com\/ru\/post\/684270\/<\/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-337450","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/337450","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=337450"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/337450\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=337450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=337450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=337450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}