{"id":465499,"date":"2025-07-01T15:21:23","date_gmt":"2025-07-01T15:21:23","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=465499"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=465499","title":{"rendered":"<span>\u041f\u0443\u0442\u0435\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u043f\u043e Ktor JWT auth \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/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><a href=\"https:\/\/ktor.io\/docs\/server-jwt.html\" rel=\"noopener noreferrer nofollow\">\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f Ktor \u043f\u043e server-jwt<\/a> \u043d\u0435\u043f\u043e\u043b\u043d\u0430. \u0415\u0441\u043b\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0437\u0430 \u0440\u0430\u043c\u043a\u0430\u043c\u0438 \u00abHello world\u00bb, \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043b\u0435\u0437\u0442\u044c \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u0438 \u0433\u043e\u0440\u043e\u0434\u0438\u0442\u044c \u043a\u043e\u0441\u0442\u044b\u043b\u0438. \u041a\u0430\u043a\u043e\u0439-\u0442\u043e \u043a\u043e\u043d\u0441\u0438\u0441\u0442\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u0438 \u0438 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u043e\u0441\u0442\u0438 \u0436\u0434\u0430\u0442\u044c \u043d\u0435 \u0441\u0442\u043e\u0438\u0442, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043d\u0435 \u043e\u0431\u043e\u0448\u043b\u043e\u0441\u044c \u0431\u0435\u0437<a href=\"https:\/\/habr.com\/ru\/articles\/874610\/\" rel=\"noopener noreferrer nofollow\"> \u0437\u0430\u0433\u043e\u0432\u043e\u0440\u0449\u0438\u043a\u043e\u0432<\/a>.<\/p>\n<figure class=\"\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c5d\/3a7\/fe1\/c5d3a7fe1798726cd357b27141b21af6.png\" alt=\" \u0413\u043e\u0432\u043e\u0440\u044f\u0442, \u0447\u0442\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 \u043c\u043e\u0433\u0443\u0442 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043d\u0430 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0435 \u0441\u043b\u043e\u0432\u043e \u00ab\u041a\u0442\u043e\u0440\u00bb.\" title=\" \u0413\u043e\u0432\u043e\u0440\u044f\u0442, \u0447\u0442\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 \u043c\u043e\u0433\u0443\u0442 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043d\u0430 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0435 \u0441\u043b\u043e\u0432\u043e \u00ab\u041a\u0442\u043e\u0440\u00bb.\" width=\"499\" height=\"316\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/c5d\/3a7\/fe1\/c5d3a7fe1798726cd357b27141b21af6.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c5d\/3a7\/fe1\/c5d3a7fe1798726cd357b27141b21af6.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption> \u0413\u043e\u0432\u043e\u0440\u044f\u0442, \u0447\u0442\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 \u043c\u043e\u0433\u0443\u0442 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043d\u0430 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0435 \u0441\u043b\u043e\u0432\u043e \u00ab\u041a\u0442\u043e\u0440\u00bb.<\/figcaption><\/div>\n<\/figure>\n<p>\u0421\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u043a\u0440\u043e\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0443\u044e \u0431\u0430\u0437\u0443 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 JWT \u0438 \u0443\u0431\u0435\u0440\u0435\u0436\u0435\u0442 \u043e\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u043e\u0434\u0432\u043e\u0434\u043d\u044b\u0445 \u043a\u0430\u043c\u043d\u0435\u0439.<\/p>\n<h2>\u041f\u0435\u0440\u0435\u0434 \u0447\u0442\u0435\u043d\u0438\u0435\u043c \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u044c\u0442\u0435\u0441\u044c<\/h2>\n<p>\u041f\u0440\u043e JWT-\u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/habr.com\/ru\/articles\/340146\/?code=32322f8d192a0505d5b2939fe3a2525c&amp;state=hSU8ESSYIwZMSee0CRp2uv0Y&amp;hl=en\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043d\u0430 \u0445\u0430\u0431\u0440\u0435<\/a>.<\/p>\n<h3>\u0422\u0435\u0440\u043c\u0438\u043d\u043e\u043b\u043e\u0433\u0438\u044f Ktor<\/h3>\n<p><code><strong>Principal<\/strong><\/code> \u2014 \u043a\u043b\u0438\u0435\u043d\u0442. \u041f\u0440\u0438\u043c\u0435\u0440 \u2014 <code>io.ktor.server.auth.jwt.JWTPrincipal<\/code>. \u041f\u043e\u0447\u0435\u043c\u0443 \u043d\u0435 <code>User<\/code>? \u041a\u043b\u0438\u0435\u043d\u0442\u043e\u043c \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0435\u0440\u0432\u0438\u0441, \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c.<\/p>\n<p><code><strong>HttpAuthHeader<\/strong><\/code> \u2014 <code>sealed class<\/code> c \u0434\u0432\u0443\u043c\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f\u043c\u0438: <code>Single<\/code> \u0438 <code>Parameterized<\/code>.<\/p>\n<p><code><strong>HttpAuthHeader.Parameterized<\/strong><\/code> \u2014 \u0445\u044d\u0434\u0435\u0440 \u043f\u043e \u0442\u0438\u043f\u0443 <code>Header: param1=\"value1\", param2=\"value2\"<\/code>. \u041f\u0440\u0438\u043c\u0435\u0440\u044b: <a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%94%D0%B0%D0%B9%D0%B4%D0%B6%D0%B5%D1%81%D1%82-%D0%B0%D1%83%D1%82%D0%B5%D0%BD%D1%82%D0%B8%D1%84%D0%B8%D0%BA%D0%B0%D1%86%D0%B8%D1%8F\" rel=\"noopener noreferrer nofollow\">digest<\/a>, <a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc7486\" rel=\"noopener noreferrer nofollow\">HOBA<\/a>. \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u043a\u0430\u0441\u0430\u0442\u044c\u0441\u044f \u043d\u0435 \u0431\u0443\u0434\u0435\u043c.<\/p>\n<p><code><strong>HttpAuthHeader.Single<\/strong><\/code> \u2014 \u0445\u044d\u0434\u0435\u0440 \u043f\u043e \u0442\u0438\u043f\u0443 <code>Header: Scheme Blob.<\/code>\u0414\u0432\u0430 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Basic_access_authentication\" rel=\"noopener noreferrer nofollow\">Basic \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/a> \u0441 \u043b\u043e\u0433\u0438\u043d\u043e\u043c \u0438 \u043f\u0430\u0440\u043e\u043b\u0435\u043c \u0432 base64: <code>Authorization: Basic bG9naW46cGFzc3dvcmQ=<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0441 JWT: <code>Authorization: Bearer &lt;headers&gt;.&lt;payload&gt;.&lt;signature&gt;<\/code>.<\/p>\n<\/li>\n<\/ul>\n<h2>\u041a\u0430\u043a \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c JWT \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e<\/h2>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"kotlin\">implementation(\"io.ktor:ktor-server-auth-jwt:$ktor_version\")  \/\/ \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u044f \u043e\u0442\u043b\u043e\u0432\u0430 \u043e\u0448\u0438\u0431\u043e\u043a implementation(\"io.ktor:ktor-server-status-pages:$ktor_version\")<\/code><\/pre>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c:<\/p>\n<pre><code class=\"kotlin\">routing {     \/\/ \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043e\u0434\u043d\u043e\u0433\u043e Authentication.provider     authenticate(\"ClientA\") {          get(\"\/\") {             \/\/ 0. \u0414\u043e\u0441\u0442\u0430\u0435\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0435 (payload \u0442\u043e\u043a\u0435\u043d\u0430)             val client = call.principal()             call.respondText(\"Hello World!\")         }     } } <\/code><\/pre>\n<p>\u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u0443\u0435\u043c JWT-\u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e. \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0443\u043a\u0430\u0437\u0430\u043d\u044b \u0432 \u0442\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u043a\u043e\u0434\u0435:<\/p>\n<pre><code class=\"kotlin\">install(Authentication) {     jwt(\"ClientA\") { \/\/ \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u043c AuthenticationConfig.providers          \/\/ 1. \u0423\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c, \u043a\u0430\u043a \u0434\u043e\u0441\u0442\u0430\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438         authHeader {call -&gt; \/*...*\/ }          \/\/ 2. \u0426\u0435\u043b\u044c \u0431\u043b\u043e\u043a\u0430 \u2014 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0444\u043e\u0440\u043c\u0430\u0442, \u043f\u043e\u0434\u043f\u0438\u0441\u044c \u0438 \u0441\u0440\u043e\u043a \u0433\u043e\u0434\u043d\u043e\u0441\u0442\u0438         verifier { httpAuthHeader -&gt; \/*...*\/ }          \/\/ 3. \u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f JWT.payload (\u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u043f\u043e\u043b\u0435 \u043d\u0435 \u0442\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0438\u043b\u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442)         validate { credential: JWTCredential -&gt; \/*...*\/ }          \/\/ 4. \u0420\u0435\u0430\u0433\u0438\u0440\u0443\u0435\u043c \u043d\u0430 \u043e\u0448\u0438\u0431\u043a\u0443         challenge { defaultScheme, realm -&gt; \/*...*\/}     } }  \/\/ \u041e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e.  install(StatusPages) {     \/\/ 5. \u0421\u044e\u0434\u0430 \u043f\u0440\u0438\u043b\u0435\u0442\u0430\u044e\u0442 \u043e\u0448\u0438\u0431\u043a\u0438 \u0438\u0437 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0431\u043b\u043e\u043a\u043e\u0432.     exception { call, e -&gt;         call.application.log.error(\"Error\", e)         \/\/ \u0442\u0443\u0442 \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0442\u0432\u0435\u0442 \u0441 call.respond     } }  \/\/ 6. \u0413\u0434\u0435-\u0442\u043e \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0430\u0445 \u043a\u0442\u043e\u0440 \/\/ internal val JWTLogger: Logger = LoggerFactory.getLogger(\"io.ktor.auth.jwt\") <\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0431\u043b\u043e\u043a <code>challenge<\/code> \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 \u0432 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0447\u0442\u043e-\u0442\u043e \u043d\u0435 \u0442\u0430\u043a, \u043a\u0430\u043a \u0438 \u0431\u044b\u043b\u043e \u0437\u0430\u044f\u0432\u043b\u0435\u043d\u043e. <\/p>\n<blockquote>\n<p>The\u00a0<code>challenge<\/code>\u00a0function allows you to configure a response to be sent if authentication fails. \u2014 \u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f Ktor<\/p>\n<\/blockquote>\n<p>\u0415\u0441\u043b\u0438 \u0431\u044b \u0431\u044b\u043b\u043e \u0442\u0430\u043a, \u0442\u043e \u0438 \u0441\u0442\u0430\u0442\u044c\u044f \u0431\u044b \u043d\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u0430\u0441\u044c.<\/p>\n<p>\u041e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0442\u0435\u0440\u043c\u0438\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0434\u0430\u043b\u044c\u0448\u0435: <strong>\u0423\u0421\u041f\u0415\u0428\u041d\u042b\u0419 \u0421\u0426\u0415\u041d\u0410\u0420\u0418\u0419<\/strong> \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438. \u0421\u0446\u0435\u043d\u0430\u0440\u0438\u0439 \u0431\u0443\u0434\u0435\u0442 \u0443\u0441\u043f\u0435\u0448\u043d\u044b\u043c, \u0435\u0441\u043b\u0438 <code>authHeader<\/code>(1) \u0432\u0435\u0440\u043d\u0435\u0442 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a, <code>verifier<\/code>(2) \u043d\u0435 \u0443\u043f\u0430\u0434\u0435\u0442, <code>validate<\/code>(3) \u0432\u0435\u0440\u043d\u0435\u0442 non-null <code>Principal<\/code>, <code>challenge<\/code>(4) \u0438<code>exception<\/code>(5) \u0431\u043b\u043e\u043a\u0438 \u043d\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u044f\u0442\u0441\u044f.<\/p>\n<h3>\u0414\u0435\u0442\u0430\u043b\u0438 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u0441 \u043e\u0434\u043d\u0438\u043c Authentication.provider\u043c<\/h3>\n<p>\u0412\u0441\u0435, \u0447\u0442\u043e \u043d\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u043f\u043e <strong>\u0443\u0441\u043f\u0435\u0448\u043d\u043e\u043c\u0443 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044e<\/strong>, \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u043e\u0442\u0432\u0435\u0442\u0443 401.<\/p>\n<p><strong>\u041f\u0443\u043d\u043a\u0442 1, <\/strong><code><strong>authHeader<\/strong><\/code>. \u0415\u0441\u043b\u0438 \u0435\u0433\u043e \u043d\u0435 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u00abAuthorization\u00bb. \u0415\u0441\u043b\u0438 \u0438\u0437 \u043d\u0435\u0433\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c null, \u043f\u043e\u043f\u0430\u0434\u0451\u043c \u0432 <code>challenge<\/code>(4) \u0431\u043b\u043e\u043a. \u0415\u0441\u043b\u0438 \u0432 \u043d\u0435\u043c \u0443\u043f\u0430\u0441\u0442\u044c, \u0442\u043e \u043d\u0435 \u043f\u043e\u043f\u0430\u0434\u0435\u043c \u0432<code>challenge<\/code>(4) \u0438 \u0432 \u043b\u043e\u0433\u0438 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043d\u0430\u043f\u0438\u0448\u0435\u0442\u0441\u044f (6), \u043d\u043e \u043f\u043e\u043f\u0430\u0434\u0435\u043c \u0432 <code>exception<\/code>(5). \u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"kotlin\">authHeader { call -&gt;    call.request.header(\"X-Custom-Auth\")?.let { parseAuthorizationHeader(it) } }<\/code><\/pre>\n<p><strong>\u041f\u0443\u043d\u043a\u0442 2, <\/strong><code><strong>verifier<\/strong><\/code>. \u0411\u043b\u043e\u043a \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u2014 \u0435\u0441\u043b\u0438 \u043d\u0435 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u043f\u043e\u043f\u0430\u0434\u0451\u043c \u0432 <code>challenge<\/code> (4). \u0415\u0441\u043b\u0438 \u0443\u043f\u0430\u0441\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u0438 \u0431\u043b\u043e\u043a\u0430, \u043f\u043e\u043f\u0430\u0434\u0451\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043b\u043e\u0433 (6). \u0415\u0441\u043b\u0438 JWTVerifier \u0432\u0435\u0440\u043d\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443, \u043f\u043e\u043f\u0430\u0434\u0435\u043c \u0432 <code>challenge<\/code> (4), \u043d\u043e \u0431\u0435\u0437 \u0434\u0435\u0442\u0430\u043b\u0435\u0439 \u043f\u0430\u0434\u0435\u043d\u0438\u044f. \u0414\u0435\u0442\u0430\u043b\u0438 \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0439\u0442\u0438 \u0432 \u043b\u043e\u0433\u0435 (6). \u0412 <code>exception<\/code> \u0442\u043e\u0436\u0435 \u043d\u0435 \u043f\u043e\u043f\u0430\u0434\u0435\u043c. <a href=\"https:\/\/ktor.io\/docs\/server-jwt.html#configure-verifier\" rel=\"noopener noreferrer nofollow\">\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0435\u0441\u0442\u044c \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a>, \u0432\u043e\u0442 \u0435\u0449\u0435 \u043e\u0434\u0438\u043d, \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u043e\u0433\u043e \u043a\u043b\u044e\u0447\u0430:<\/p>\n<pre><code class=\"kotlin\">private val keyFactory: KeyFactory = KeyFactory.getInstance(\"RSA\")  fun jwtVerifier(): JWTVerifier {     val publicKey = File(\"path\/to\/public.pem\").readText()         .replace(\"\\n\", \"\")         .replace(\"\\r\", \"\")         .replace(\"-----BEGIN PUBLIC KEY-----\", \"\")         .replace(\"-----END PUBLIC KEY-----\", \"\")     val keySpecX509 = X509EncodedKeySpec(Base64.getDecoder().decode(publicKeyContent))     val pubKey = keyFactory.generatePublic(keySpecX509) as RSAPublicKey     val algo = Algorithm.RSA256(pubKey, null)     return JWT.require(algo).build() }  verifier { httpAuthHeader -&gt;     if (httpAuthHeader is HttpAuthHeader.Single) {         jwtVerifier()     } else {         environment.log.error(\"HttpAuthHeader does not adhere scheme:  \")         null     } }<\/code><\/pre>\n<p><strong>\u041f\u0443\u043d\u043a\u0442 3, <\/strong><code><strong>validate<\/strong><\/code>. \u0414\u043b\u044f <strong>\u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0433\u043e \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f<\/strong> \u043d\u0443\u0436\u043d\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c non-null \u043e\u0431\u044a\u0435\u043a\u0442 \u0438\u0437 \u0431\u043b\u043e\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0442\u044c \u043a\u043b\u0438\u0435\u043d\u0442\u0430. \u0415\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0447\u0435\u0440\u0435\u0437 \u0432\u044b\u0437\u043e\u0432 <code>call.principal&lt;T&gt;()<\/code>.<\/p>\n<p>\u041f\u043e \u0437\u0430\u0434\u0443\u043c\u043a\u0435 \u0430\u0432\u0442\u043e\u0440\u043e\u0432 <strong>\u043d\u0430 \u043e\u0448\u0438\u0431\u043a\u0430\u0445 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043d\u0435 \u043f\u0430\u0434\u0430\u0442\u044c, \u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c null<\/strong>. \u0415\u0441\u043b\u0438 \u0436\u0435 \u0432 \u0431\u043b\u043e\u043a\u0435 \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u0442\u0441\u044f \u043e\u0448\u0438\u0431\u043a\u0430, \u043c\u044b \u043d\u0435 \u043f\u043e\u043f\u0430\u0434\u0451\u043c \u043d\u0438 \u0432<code>challenge<\/code>(4), \u043d\u0438 \u0432<code>exception<\/code>(5), \u043d\u043e \u043d\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u0435\u043c \u043b\u043e\u0433 (6). \u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"kotlin\">validate { credential: JWTCredential -&gt;     val scopes = credential.payload.getClaim(\"scopes\").asList(String::class.java)     if (scopes == null) return@validate null     JWTPrincipal(credential.payload) }<\/code><\/pre>\n<p><strong>\u041f\u0443\u043d\u043a\u0442 4, <\/strong><code><strong>challenge<\/strong><\/code>. \u0411\u043b\u043e\u043a \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445:<\/p>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 <code>authHeader<\/code> \u0432\u0435\u0440\u043d\u0435\u0442 null.<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b\u0438 <code>verifier<\/code> (2) (\u0441\u0442\u0440\u0430\u043d\u043d\u043e, \u0447\u0442\u043e \u043e\u043d\u0438 \u0432\u043e\u043e\u0431\u0449\u0435 \u0434\u0430\u044e\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435).<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 <code>JWTVerifier<\/code>, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u0432 <code>verifier<\/code>(2), \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 <code>validate<\/code>(3) \u0432\u0435\u0440\u043d\u0435\u0442 <code>null<\/code>. \u0415\u0441\u043b\u0438 \u0432\u043d\u0443\u0442\u0440\u0438 \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0435\u0442 \u043f\u0430\u0434\u0435\u043d\u0438\u0435, \u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438\u0434\u0435\u0442 \u0432 <code>exception<\/code>. \u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"kotlin\">challenge { defaultScheme: String, realm: String -&gt;   \/\/ Bearer, Ktor Server     val failures: List&lt;AuthenticationFailedCause&gt; = call.authentication.allFailures     failures.forEach { call.application.log.error(\"failure: $it\") }          call.respondText(status = HttpStatusCode.Unauthorized) {        \"Authentication error, but what exactly?\"      } }<\/code><\/pre>\n<p>\u0412\u0441\u0451 \u0431\u044b \u043d\u0438\u0447\u0435\u0433\u043e, \u043d\u043e \u0432\u044b\u0442\u0430\u0449\u0438\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043f\u0440\u0438\u0447\u0438\u043d\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u043d\u0435 \u0442\u0430\u043a-\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e. <code>AuthenticationFailedCause<\/code> \u2014 \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442 \u0431\u0435\u0437 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438: \u043b\u0438\u0431\u043e <code>NoCredentials<\/code>, \u043b\u0438\u0431\u043e <code>InvalidCredentials<\/code>. \u0410 \u0432\u043e\u0442 <code>AuthenticationFailedCause.Error<\/code> \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0438\u043a\u0435\u043c \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 <code>JWTAuthenticationProvider<\/code>. <code>Error<\/code> \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0439\u0442\u0438 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 Ktor oauth, \u0447\u0442\u043e \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0440\u0430\u043c\u043a\u0438 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438.<\/p>\n<h2>\u0422\u0430\u0431\u043b\u0438\u0446\u0430 \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u044f Ktor jwt auth<\/h2>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th data-colwidth=\"339\" width=\"339\">\n<p align=\"left\">\u041e\u0448\u0438\u0431\u043a\u0430 \u0432 \u0431\u043b\u043e\u043a\u0435 \u2193<\/p>\n<\/th>\n<th data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">challenge<\/p>\n<\/th>\n<th>\n<p align=\"left\">log<\/p>\n<\/th>\n<th>\n<p align=\"left\">exception<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>authHeader<\/code> \u0432\u0435\u0440\u043d\u0443\u043b null<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>authHeader<\/code> \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u043b \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<td>\n<p align=\"left\">+<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>verifier<\/code> \u043d\u0435 \u0437\u0430\u0434\u0430\u043d<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>verifier<\/code> \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u043b \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<td>\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>JWTVerifier<\/code> \u0432\u0435\u0440\u043d\u0443\u043b \u043e\u0448\u0438\u0431\u043a\u0443<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>validate<\/code> \u0432\u0435\u0440\u043d\u0443\u043b null<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>validate<\/code> \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u043b \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<td>\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>challenge<\/code> \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u043b \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<td>\n<p align=\"left\">+<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u0425\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b, \u0447\u0442\u043e challenge-\u0431\u043b\u043e\u043a \u043e\u0442\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u043b \u0432\u043e \u0432\u0441\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 (\u043a\u0440\u043e\u043c\u0435 \u0441\u0430\u043c\u043e\u0433\u043e challenge-\u0431\u043b\u043e\u043a\u0430), \u0438 \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u043f\u043e\u0434\u043e\u0431\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0432 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435 (<a href=\"https:\/\/github.com\/D00mch\/ktor_jwt_example\/blob\/main\/src\/main\/kotlin\/auth\/jwt\/JWTAuth.kt\" rel=\"noopener noreferrer nofollow\">github<\/a>).<\/p>\n<h3>\u041a\u0430\u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0438\u043f\u043e\u0432 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438<\/h3>\n<p>\u0412\u0441\u0435 \u0432\u044b\u0448\u0435\u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0430 \u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u043f\u0440\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0442\u0438\u043f\u043e\u0432 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0439 <code>AuthenticationStrategy<\/code>.<\/p>\n<pre><code class=\"kotlin\">routing {     authenticate(         \"ClientA\",         \"ClientB\",         \/\/ \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e         strategy = AuthenticationStrategy.FirstSuccessful,     ) {\/*...*\/} }<\/code><\/pre>\n<p>\u0418\u043c\u0435\u044e\u0449\u0438\u0435\u0441\u044f 3 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \u043f\u043e\u043d\u044f\u0442\u043d\u044b \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u043e:<\/p>\n<pre><code class=\"kotlin\">enum class AuthenticationStrategy { Optional, FirstSuccessful, Required }<\/code><\/pre>\n<p><code><strong>AuthenticationStrategy.Optional<\/strong><\/code> \u2014 \u0435\u0441\u043b\u0438 \u043d\u0438 \u043e\u0434\u0438\u043d \u0438\u0437 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 \u043d\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u0442 <strong>\u0443\u0441\u043f\u0435\u0448\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong> \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438, \u0442\u043e  \u043c\u044b \u043d\u0435 \u043f\u043e\u043f\u0430\u0434\u0435\u043c \u0432 <code>exception<\/code> (5) \u0431\u043b\u043e\u043a \u0438 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c 401, \u0430 \u0432\u043d\u0443\u0442\u0440\u0438 \u0440\u0443\u0447\u043a\u0438 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <code>call.principal()<\/code> \u0432\u0435\u0440\u043d\u0435\u0442\u0441\u044f <code>null<\/code>.<\/p>\n<p><code><strong>AuthenticationStrategy.FirstSuccessful<\/strong><\/code> \u2014 \u043f\u0435\u0440\u0432\u044b\u0439 \u0438\u0437 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0439\u0434\u0435\u0442 <strong>\u0443\u0441\u043f\u0435\u0448\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong>, \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0438 \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0438\u043a\u043e\u043c\u0443. Jwt-\u0431\u043b\u043e\u043a\u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432  \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u0437\u0432\u0430\u043d\u044b \u0432 \u0442\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 <code>authenticate<\/code>. \u0422\u0435, \u043a\u0442\u043e \u0443\u0441\u043f\u0435\u043b \u0443\u043f\u0430\u0441\u0442\u044c \u0432 \u043b\u044e\u0431\u043e\u043c \u0438\u0437 \u0431\u043b\u043e\u043a\u043e\u0432, \u043d\u0438 \u043d\u0430 \u0447\u0442\u043e \u043d\u0435 \u043f\u043e\u0432\u043b\u0438\u044f\u044e\u0442, \u043a\u0440\u043e\u043c\u0435 \u043b\u043e\u0433\u043e\u0432 (6), \u0430 <code>challenge<\/code>(4) \u0438 <code>exception<\/code>(5) \u043d\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u044f\u0442\u0441\u044f. \u0412 <code>call.principal()<\/code> \u0432\u0435\u0440\u043d\u0435\u0442\u0441\u044f <code>Principal<\/code> \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0433\u043e. \u0415\u0441\u043b\u0438 \u043d\u0438\u043a\u0442\u043e \u043d\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u0442 <strong>\u0443\u0441\u043f\u0435\u0448\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong>, \u0432\u0435\u0440\u043d\u0435\u0442\u0441\u044f 401 \u0438\u043b\u0438 \u0442\u043e, \u0447\u0442\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043e \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 <code>challenge<\/code>(4) \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438\u043b\u0438 \u043e\u0431\u0449\u0435\u0433\u043e \u0431\u043b\u043e\u043a\u0430 <code>exception<\/code>(5).<\/p>\n<p><code><strong>AuthenticationStrategy.Required<\/strong><\/code> \u2014 \u0432\u0441\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0440\u043e\u0439\u0442\u0438 <strong>\u0443\u0441\u043f\u0435\u0448\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong>, \u0438\u043d\u0430\u0447\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c 401. \u0412 <code>call.principal&lt;Any&gt;()<\/code> \u0432\u0435\u0440\u043d\u0435\u0442\u0441\u044f principal \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u0432 <code>authenticate<\/code> \u043a\u043b\u0438\u0435\u043d\u0442\u0430 (\u00abClientA\u00bb \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b\u0448\u0435 \u0438\u0437 \u044d\u0442\u043e\u0433\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0430). \u041d\u043e \u0435\u0441\u043b\u0438 \u0432\u044b\u0437\u0432\u0430\u0442\u044c <code>call.principal&lt;ClientBPrincipal&gt;()<\/code>, \u0442\u043e \u0432\u0435\u0440\u043d\u0435\u0442\u0441\u044f <code>ClientBPrincipal<\/code>.<\/p>\n<h3>\u0413\u043b\u0443\u0431\u043e\u043a\u043e\u0435 \u0437\u0430\u0433\u043b\u0430\u0442\u044b\u0432\u0430\u043d\u0438\u0435<\/h3>\n<p>\u041a\u0442\u043e\u0440 \u043f\u043e\u0440\u0430\u0436\u0430\u0435\u0442 \u0433\u0438\u0431\u043a\u043e\u0441\u0442\u044c\u044e, \u0434\u043e\u0441\u0442\u043e\u0439\u043d\u043e\u0439 \u0441\u0430\u043c\u044b\u0445 \u0441\u043c\u0435\u043b\u044b\u0445 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0439 \u0441 \u043f\u043e\u0434\u043f\u0438\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u0440\u0435\u0431\u0440\u0430\u043c\u0438.<\/p>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435, \u0447\u0442\u043e \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u043e\u043f\u0430\u0441\u0442\u044c \u0432 \u043a\u0440\u0430\u0440\u0442\u0438\u0440\u0443. \u0412 \u0434\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043f\u0430\u0441\u0442\u044c \u043b\u0438\u0431\u043e \u0447\u0435\u0440\u0435\u0437 \u043f\u0430\u0440\u0430\u0434\u043d\u044b\u0439 \u043f\u043e\u0434\u044a\u0435\u0437\u0434 \u0441 \u043b\u0438\u0444\u0442\u043e\u043c, \u043b\u0438\u0431\u043e \u0447\u0435\u0440\u0435\u0437 \u0431\u043e\u043a\u043e\u0432\u043e\u0439 \u043f\u043e\u0434\u044a\u0435\u0437\u0434 \u0441 \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435\u0439. \u041e\u0442 \u043e\u0431\u043e\u0438\u0445 \u043f\u043e\u0434\u044a\u0435\u0437\u0434\u043e\u0432 \u043d\u0443\u0436\u043d\u044b \u043a\u043b\u044e\u0447\u0438. \u0412\u043d\u0443\u0442\u0440\u0438 \u0434\u043e\u043c\u0430 \u0431\u0443\u0434\u0435\u0442 \u043a\u0432\u0430\u0440\u0442\u0438\u0440\u0430, \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0442\u043e\u0436\u0435 \u043d\u0443\u0436\u0435\u043d \u043a\u043b\u044e\u0447.<\/p>\n<pre><code class=\"kotlin\">object Auth {   val scope1 = \"\u043a\u043b\u044e\u0447 \u043e\u0442 \u043f\u0430\u0440\u0430\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u044a\u0435\u0437\u0434\u0430\"    val scope2 = \"\u043a\u043b\u044e\u0447 \u043e\u0442 \u0431\u043e\u043a\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u044a\u0435\u0437\u0434\u0430\"   val scope3 = \"\u043a\u043b\u044e\u0447 \u043e\u0442 \u043a\u0432\u0430\u0440\u0442\u0438\u0440\u044b\" }  route(\"building\/\") {    authenticate(Auth.scope1, Auth.scope2, strategy = FirstSuccessful) {      route(\"flat\") {        authenticate(Auth.scope3, strategy = Required) {          get() { \/*..*\/ }         post() { \/*..*\/ }       }     }   } }<\/code><\/pre>\n<p>\u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a, \u0447\u0442\u043e \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a <code>flat<\/code> \u043d\u0443\u0436\u043d\u044b 2 \u043a\u043b\u044e\u0447\u0430 \u2014 \u043e\u0434\u0438\u043d \u043e\u0442 \u0434\u043e\u043c\u0430 (scope1 \u0438\u043b\u0438 scope2), \u0434\u0440\u0443\u0433\u043e\u0439 \u043e\u0442 \u043a\u0432\u0430\u0440\u0442\u0438\u0440\u044b (scope3). <\/p>\n<p><strong>\u041d\u043e \u044d\u0442\u043e \u043d\u0435 \u0442\u0430\u043a<\/strong>!<\/p>\n<p>\u0414\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0440\u0443\u0447\u043a\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043b\u044e\u0431\u043e\u0433\u043e \u0438\u0437 \u043a\u043b\u044e\u0447\u0435\u0439, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043d\u0435 <code>scope3<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043c\u0435\u0447\u0435\u043d \u043a\u0430\u043a <code>required<\/code>. \u0422\u0430\u043a\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043e\u0442 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0432\u0441\u0435 <code>authenticate<\/code> \u0440\u0430\u0432\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u044b, \u0430 \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0439 \u0448\u043b\u0430 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f <code>FirstSuccessful<\/code>.<\/p>\n<p>\u041d\u043e \u0447\u0442\u043e, \u0435\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 <code>(scope1 || scope2) &amp;&amp; scope3<\/code>. \u041a\u0430\u043a \u0434\u0443\u043c\u0430\u0435\u0442\u0435, \u0431\u0443\u0434\u0443\u0442 \u043b\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0434\u0432\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0438\u0436\u0435?<\/p>\n<pre><code class=\"kotlin\">authenticate(\"scope3\", strategy = AuthenticationStrategy.Required) {    route(\"building\/flat\") {      authenticate(\"scope1\", strategy = AuthenticationStrategy.Required) {        get() { \/*..*\/ }       post() { \/*..*\/ }     }     authenticate(\"scope2\", strategy = AuthenticationStrategy.Required) {        get() { \/*..*\/ }       post() { \/*..*\/ }     }   } }<\/code><\/pre>\n<p>\u0412\u0442\u043e\u0440\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f:<\/p>\n<pre><code class=\"kotlin\">route(\"building\/flat\") {     authenticate(\"scope1\" ,\"scope3\", strategy = AuthenticationStrategy.Required) {         get() { \/*..*\/ }         post() { \/*..*\/ }     }     authenticate(\"scope2\" ,\"scope3\", strategy = AuthenticationStrategy.Required) {         get() { \/*..*\/ }         post() { \/*..*\/ }     } }<\/code><\/pre>\n<p>\u041e\u0431\u0435 \u0431\u0443\u0434\u0443\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043d\u0435 \u0442\u0430\u043a, \u043a\u0430\u043a \u043c\u044b \u0445\u043e\u0442\u0438\u043c. \u041f\u0440\u043e\u0439\u0434\u0443\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0441\u043e <code>scope1<\/code> \u0438 <code>scope3<\/code>, \u0442.\u043a. \u043e\u043d\u0438 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u044b \u043f\u0435\u0440\u0432\u044b\u043c\u0438. \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0447\u0435\u0440\u0435\u0437 \u043f\u043e\u043f\u044b\u0442\u043a\u0443 \u0434\u043e\u0441\u0442\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e principal \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0447\u0442\u043e \u0435\u0441\u0442\u044c <code>scope1<\/code> \u0438\u043b\u0438 <code>scope2<\/code> \u2014 \u0442\u043e\u0436\u0435 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f. \u041a\u0430\u043a\u0438\u0435 \u0431\u044b \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \u043d\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c, \u0431\u0443\u0434\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d non-null principal:<\/p>\n<pre><code class=\"kotlin\">route(\"building\/\") {    authenticate(Auth.scope1, strategy = &lt;FirstSuccessful | Optinal&gt;) {      route(\"flat\") {        authenticate(Auth.scope2, strategy = &lt;\u043b\u044e\u0431\u0430\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f&gt;) {          get() {              val principal1 = call.principal1&lt;Principal1&gt;()             val principal2 = call.principal1&lt;Principal2&gt;()             val alwaysTrue = principal1 == null || principal2 == null         }       }     }   } }<\/code><\/pre>\n<p>\u041a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438, \u043a\u0430\u043a \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f, \u0438 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u0438\u043b\u0438\u0441\u044c \u043f\u0435\u0440\u0432\u044b\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u043b\u043e\u0441\u044c.<\/p>\n<p>\u041d\u0435 \u0431\u0443\u0434\u0443 \u0431\u043e\u043b\u044c\u0448\u0435 \u043c\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044f \u0438 \u0441\u043e\u043e\u0431\u0449\u0443, \u0447\u0442\u043e \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 <code>AuthenticationStrategy<\/code> \u0437\u0430\u0434\u0430\u0447\u0443 \u043d\u0435 \u0440\u0435\u0448\u0438\u0442\u044c. \u041d\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 <code>scope4<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u0438\u0442\u0441\u044f \u0443\u0441\u043b\u043e\u0432\u0438\u0435\u043c <code>(scope1 || scope2) &amp;&amp; scope3<\/code>:<\/p>\n<pre><code class=\"kotlin\">object Scope4Principal  \/\/ \u0432 \u0431\u043b\u043e\u043a\u0435 install(Authentication) jwt(\"scope4\") {     verifier { makeVerfier() }     validate { credential: JWTCredential -&gt;         val scopes = credential.payload.getClaim(\"scopes\").asList(String::class.java)             ?: emptyList()         if (scopes.contains(\"scope3\") &amp;&amp;              (scopes.contains(\"scope1\") || scopes.contains(\"scope2\")))             Scope4Principal         } else {             null         }     } }  \/\/ \u0432 \u0431\u043b\u043e\u043a\u0435 routing authenticate(Auth.scope4) {    route(\"building\/flat\") {      get() { \/*..*\/ }     post() { \/*..*\/ }   } }<\/code><\/pre>\n<h3>\u041a\u0430\u043a \u0443\u0437\u043d\u0430\u0442\u044c \u043e \u043f\u0440\u0438\u0447\u0438\u043d\u0435 InvalidCredentials<\/h3>\n<p>\u2014  \u0417\u0430\u0447\u0435\u043c \u0443\u0437\u043d\u0430\u0432\u0430\u0442\u044c? \u2014 \u0441\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044c?<br \/>\u2014 \u0427\u0442\u043e\u0431\u044b \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u0440\u0430\u0437\u0431\u043e\u0440 \u043f\u0440\u043e\u0431\u043b\u0435\u043c, \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0432 \u0432 \u043b\u043e\u0433\u0438. \u041b\u0438\u0431\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u043e\u0442\u0432\u0435\u0442 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0442\u0438\u043f\u0430 \u043e\u0448\u0438\u0431\u043a\u0438 (\u0435\u0441\u043b\u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u0438\u043a\u0438 \u043d\u0435 \u043f\u0440\u043e\u0442\u0438\u0432).<\/p>\n<p>\u041a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b, \u044d\u0442\u043e \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u0438 \u043b\u0435\u0433\u043a\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u043d\u043e \u043d\u0435\u0442. \u0412 \u0431\u043b\u043e\u043a\u0435 <code>challenge<\/code>(4) \u0435\u0441\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043e <code>call.authentication.allErrors<\/code> \u0438 <code>call.authentication.allFailures<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u043d\u0438\u0447\u0435\u0433\u043e, \u043a\u0440\u043e\u043c\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0441\u043b\u043e\u0432: <code>InvalidCredentials<\/code> \u0438\u043b\u0438 <code>NoCredentials<\/code>.<\/p>\n<p>\u041f\u043e \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0430\u043c \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0432\u0441\u044f \u043f\u043e\u043b\u0435\u0437\u043d\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u0442\u0435\u0440\u044f\u0435\u0442\u0441\u044f \u0432 \u0431\u043b\u043e\u043a\u0435 <code>catch<\/code>:<\/p>\n<pre><code class=\"kotlin\">internal suspend fun verifyAndValidate(     call: ApplicationCall,     jwtVerifier: JWTVerifier?,     token: HttpAuthHeader,     schemes: JWTAuthSchemes,     validate: suspend ApplicationCall.(JWTCredential) -&gt; Any? ): Any? {     val jwt = try {         token.getBlob(schemes)?.let { jwtVerifier?.verify(it) }     } catch (cause: JWTVerificationException) {         JWTLogger.debug(\"JWT verification failed: ${cause.message}\", cause)         \/\/ \u0442\u0443\u0442 \u0438 \u043f\u043e\u0442\u0435\u0440\u044f\u043b\u0430\u0441\u044c \u0432\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e `cause`, \u043d\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u0446\u0435\u043f\u0438\u0442\u044c\u0441\u044f \u0437\u0430 JWTLogger         null     } ?: return null     \/*...*\/ }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043b\u043e\u0433\u043e\u0432, \u0442\u043e Ktor \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 <code>org.slf4j.Logger<\/code> \u0441 \u0438\u043c\u0435\u043d\u0435\u043c <code>io.ktor.auth.jwt<\/code>. \u0412\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u044b\u0432\u043e\u0434 \u043f\u0435\u0440\u0432\u044b\u0445 \u0442\u0440\u0435\u0445 \u0441\u0442\u0440\u043e\u043a \u043f\u043e \u043e\u0448\u0438\u0431\u043a\u0430\u043c \u0441 jwt \u043e\u0442 Ktor \u0441 logback.xml:<\/p>\n<pre><code class=\"xml\">&lt;configuration&gt;     &lt;appender name=\"STDOUT\" class=\"ch.qos.logback.core.ConsoleAppender\"&gt;         &lt;encoder&gt;             &lt;pattern&gt;%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n&lt;\/pattern&gt;         &lt;\/encoder&gt;     &lt;\/appender&gt;     &lt;root level=\"INFO\"&gt;         &lt;appender-ref ref=\"STDOUT\"\/&gt;     &lt;\/root&gt;     &lt;logger name=\"org.eclipse.jetty\" level=\"INFO\"\/&gt;     &lt;logger name=\"io.netty\" level=\"INFO\"\/&gt;       &lt;!--The below settings are used to show io.ktor.server.auth.jwt issues--&gt;     &lt;appender name=\"STDOUT_SHORT\" class=\"ch.qos.logback.core.ConsoleAppender\"&gt;         &lt;encoder&gt;             &lt;pattern&gt;%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{2} - %msg%n{2}&lt;\/pattern&gt;         &lt;\/encoder&gt;     &lt;\/appender&gt;     &lt;root level=\"TRACE\"&gt;         &lt;appender-ref ref=\"STDOUT_SHORT\"\/&gt;     &lt;\/root&gt;     &lt;logger name=\"io.ktor.auth.jwt\" level=\"TRACE\" additivity=\"false\"&gt;         &lt;appender-ref ref=\"STDOUT_SHORT\"\/&gt;     &lt;\/logger&gt; &lt;\/configuration&gt;<\/code><\/pre>\n<p>\u041c\u043d\u0435 \u044d\u0442\u043e\u0442 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043d\u0435 \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0437\u0430\u0432\u0438\u0441\u0435\u0442\u044c \u043e\u0442 \u0434\u0435\u0442\u0430\u043b\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u0427\u0442\u043e \u0435\u0441\u043b\u0438 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0438 \u043f\u0435\u0440\u0435\u0438\u043c\u0435\u043d\u0443\u044e\u0442 \u043b\u043e\u0433\u0433\u0435\u0440 \u0438\u043b\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0442 \u043e\u0448\u0438\u0431\u043a\u0435? \u041d\u0435 \u0431\u0443\u0434\u0435\u043c \u0436\u0435 \u043c\u044b \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u044d\u0442\u0438\u043c.<\/p>\n<p>\u0411\u0435\u0437 \u043a\u043e\u0441\u0442\u044b\u043b\u0435\u0439 \u0434\u043e\u0441\u0442\u0430\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u043e\u0442\u0432\u0435\u0442, \u043d\u0435\u043b\u044c\u0437\u044f.<\/p>\n<p>\u0415\u0441\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u2014 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c JWTVerifier \u0435\u0449\u0435 \u0440\u0430\u0437, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443. \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u043b\u043e\u0445\u043e\u0435: \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 <code>challeng\u0435<\/code> \u0435\u0449\u0435 \u0440\u0430\u0437 \u0434\u043e\u0441\u0442\u0430\u0432\u0430\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0438 \u0435\u0449\u0435 \u0440\u0430\u0437 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c <code>verify<\/code>. \u041b\u0430\u0434\u043d\u043e \u0431\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u043e, \u043d\u043e \u0432\u0435\u0434\u044c \u043d\u0435\u0442 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0439, \u0447\u0442\u043e \u043c\u044b \u043f\u043e\u043f\u0430\u0434\u0435\u043c \u0432 \u044d\u0442\u043e\u0442 <code>challenge<\/code> (\u0441\u043c. \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u044f ktor jwt auth \u0432\u044b\u0448\u0435).<\/p>\n<p>\u0418\u043b\u043b\u044e\u0437\u043e\u0440\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u2014 \u043e\u0431\u0435\u0440\u043d\u0443\u0442\u044c <code>JWTVerifier<\/code> \u0432 \u0441\u0432\u043e\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438.<\/p>\n<pre><code class=\"kotlin\">class SpyJwtVerifier(private val delegate: JWTVerifier) : JWTVerifier by delegate {     var exception: Exception? = null         private set      override fun verify(token: String?): DecodedJWT = try {         delegate.verify(token)     } catch (e: Exception) {         exception = e         throw e     } }  jwt(\"ClientB\") {     val spyVerifier = SpyJwtVerifier(webTokenVerifier())     verifier { httpAuthHeader -&amp;gt; spyVerifier }          challenge { _, _ -&gt;         spyVerifier.exception?.let { e -&amp;gt;             call.application.log.error(\"Verification error\")             call.respond(HttpStatusCode.Unauthorized, mapOf(\"message\" to e.toString()))             return@let         }         call.respond(             HttpStatusCode.Unauthorized,              mapOf(\"message\" to call.authentication.allFailures.first())         )     } }<\/code><\/pre>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0442\u0443\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e <code>spyVerifier<\/code> \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043e\u0434\u0438\u043d \u0440\u0430\u0437. \u0411\u0443\u0434\u0435\u0442 \u0433\u043e\u043d\u043a\u0430.<\/p>\n<p>\u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u2014 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 <code>AuthenticationProvider<\/code>. \u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u2014 \u0432\u0437\u044f\u0442\u044c <a href=\"https:\/\/github.com\/ktorio\/ktor\/blob\/main\/ktor-server\/ktor-server-plugins\/ktor-server-auth-jwt\/jvm\/src\/io\/ktor\/server\/auth\/jwt\/JWTAuth.kt\" rel=\"noopener noreferrer nofollow\">\u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438<\/a> \u0438 \u043f\u043e\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0434\u0432\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>onAuthenticate<\/code> (\u0440\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c try-catch \u0431\u043b\u043e\u043a \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c <code>challenge<\/code> \u0432 catch) \u0438 \u043b\u043e\u0432\u0438\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438 \u0432\u043d\u0443\u0442\u0440\u0438 <code>verifyAndValidate<\/code>. \u0414\u0435\u0442\u0430\u043b\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 <a href=\"https:\/\/github.com\/D00mch\/ktor_jwt_example\/blob\/main\/src\/main\/kotlin\/auth\/jwt\/JWTAuth.kt\" rel=\"noopener noreferrer nofollow\">\u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435<\/a>.  \u0422\u0430\u0431\u043b\u0438\u0446\u0430 \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u043e\u0439:<\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th data-colwidth=\"339\" width=\"339\">\n<p>\u041e\u0448\u0438\u0431\u043a\u0430 \u0432 \u0431\u043b\u043e\u043a\u0435 \u2193<\/p>\n<\/th>\n<th data-colwidth=\"175\" width=\"175\">\n<p>challenge<\/p>\n<\/th>\n<th>\n<p>log<\/p>\n<\/th>\n<th>\n<p>exception<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>authHeader<\/code> \u0432\u0435\u0440\u043d\u0443\u043b null<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>authHeader<\/code> \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u043b \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>verifier<\/code> \u043d\u0435 \u0437\u0430\u0434\u0430\u043d<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>verifier<\/code> \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u043b \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>JWTVerifier<\/code> \u0432\u0435\u0440\u043d\u0443\u043b \u043e\u0448\u0438\u0431\u043a\u0443<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>validate<\/code> \u0432\u0435\u0440\u043d\u0443\u043b null<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>validate<\/code> \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u043b \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>challenge<\/code> \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u043b \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<td>\n<p align=\"left\">+<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h3>\u0412\u043c\u0435\u0441\u0442\u043e \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f<\/h3>\n<p>\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 \u0434\u0435\u0442\u0430\u043b\u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0438 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f <a href=\"https:\/\/ktor.io\/docs\/server-jwt.html\" rel=\"noopener noreferrer nofollow\">ktor-server-auth<\/a>. \u041d\u0430 \u043c\u043d\u043e\u0433\u0438\u0435 \u043d\u0435\u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u0432\u0435\u0442\u0438\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0432\u0448\u0438\u0441\u044c \u043a <strong>\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u044f Ktor jwt auth<\/strong> \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0435 \u0441\u0442\u0430\u0442\u044c\u0438.<\/p>\n<p>\u041f\u043e\u0438\u0433\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439 \u043c\u043e\u0436\u043d\u043e \u0432 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435 (<a href=\"https:\/\/github.com\/D00mch\/ktor_jwt_example?tab=readme-ov-file\" rel=\"noopener noreferrer nofollow\">github<\/a>), \u0442\u0430\u043c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0442\u0435\u0441\u0442\u044b \u0438 \u043f\u043e\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <a href=\"https:\/\/github.com\/D00mch\/ktor_jwt_example\/blob\/main\/src\/main\/kotlin\/auth\/jwt\/JWTAuth.kt\" rel=\"noopener noreferrer nofollow\">AuthenticationProvider<\/a>.<\/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\/921076\/\"> https:\/\/habr.com\/ru\/articles\/921076\/<\/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><a href=\"https:\/\/ktor.io\/docs\/server-jwt.html\" rel=\"noopener noreferrer nofollow\">\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f Ktor \u043f\u043e server-jwt<\/a> \u043d\u0435\u043f\u043e\u043b\u043d\u0430. \u0415\u0441\u043b\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0437\u0430 \u0440\u0430\u043c\u043a\u0430\u043c\u0438 \u00abHello world\u00bb, \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043b\u0435\u0437\u0442\u044c \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u0438 \u0433\u043e\u0440\u043e\u0434\u0438\u0442\u044c \u043a\u043e\u0441\u0442\u044b\u043b\u0438. \u041a\u0430\u043a\u043e\u0439-\u0442\u043e \u043a\u043e\u043d\u0441\u0438\u0441\u0442\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u0438 \u0438 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u043e\u0441\u0442\u0438 \u0436\u0434\u0430\u0442\u044c \u043d\u0435 \u0441\u0442\u043e\u0438\u0442, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043d\u0435 \u043e\u0431\u043e\u0448\u043b\u043e\u0441\u044c \u0431\u0435\u0437<a href=\"https:\/\/habr.com\/ru\/articles\/874610\/\" rel=\"noopener noreferrer nofollow\"> \u0437\u0430\u0433\u043e\u0432\u043e\u0440\u0449\u0438\u043a\u043e\u0432<\/a>.<\/p>\n<figure class=\"\">\n<div><figcaption> \u0413\u043e\u0432\u043e\u0440\u044f\u0442, \u0447\u0442\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 \u043c\u043e\u0433\u0443\u0442 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043d\u0430 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0435 \u0441\u043b\u043e\u0432\u043e \u00ab\u041a\u0442\u043e\u0440\u00bb.<\/figcaption><\/div>\n<\/figure>\n<p>\u0421\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u043a\u0440\u043e\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0443\u044e \u0431\u0430\u0437\u0443 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 JWT \u0438 \u0443\u0431\u0435\u0440\u0435\u0436\u0435\u0442 \u043e\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u043e\u0434\u0432\u043e\u0434\u043d\u044b\u0445 \u043a\u0430\u043c\u043d\u0435\u0439.<\/p>\n<h2>\u041f\u0435\u0440\u0435\u0434 \u0447\u0442\u0435\u043d\u0438\u0435\u043c \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u044c\u0442\u0435\u0441\u044c<\/h2>\n<p>\u041f\u0440\u043e JWT-\u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/habr.com\/ru\/articles\/340146\/?code=32322f8d192a0505d5b2939fe3a2525c&amp;state=hSU8ESSYIwZMSee0CRp2uv0Y&amp;hl=en\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043d\u0430 \u0445\u0430\u0431\u0440\u0435<\/a>.<\/p>\n<h3>\u0422\u0435\u0440\u043c\u0438\u043d\u043e\u043b\u043e\u0433\u0438\u044f Ktor<\/h3>\n<p><code><strong>Principal<\/strong><\/code> \u2014 \u043a\u043b\u0438\u0435\u043d\u0442. \u041f\u0440\u0438\u043c\u0435\u0440 \u2014 <code>io.ktor.server.auth.jwt.JWTPrincipal<\/code>. \u041f\u043e\u0447\u0435\u043c\u0443 \u043d\u0435 <code>User<\/code>? \u041a\u043b\u0438\u0435\u043d\u0442\u043e\u043c \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0435\u0440\u0432\u0438\u0441, \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c.<\/p>\n<p><code><strong>HttpAuthHeader<\/strong><\/code> \u2014 <code>sealed class<\/code> c \u0434\u0432\u0443\u043c\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f\u043c\u0438: <code>Single<\/code> \u0438 <code>Parameterized<\/code>.<\/p>\n<p><code><strong>HttpAuthHeader.Parameterized<\/strong><\/code> \u2014 \u0445\u044d\u0434\u0435\u0440 \u043f\u043e \u0442\u0438\u043f\u0443 <code>Header: param1=\"value1\", param2=\"value2\"<\/code>. \u041f\u0440\u0438\u043c\u0435\u0440\u044b: <a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%94%D0%B0%D0%B9%D0%B4%D0%B6%D0%B5%D1%81%D1%82-%D0%B0%D1%83%D1%82%D0%B5%D0%BD%D1%82%D0%B8%D1%84%D0%B8%D0%BA%D0%B0%D1%86%D0%B8%D1%8F\" rel=\"noopener noreferrer nofollow\">digest<\/a>, <a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc7486\" rel=\"noopener noreferrer nofollow\">HOBA<\/a>. \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u043a\u0430\u0441\u0430\u0442\u044c\u0441\u044f \u043d\u0435 \u0431\u0443\u0434\u0435\u043c.<\/p>\n<p><code><strong>HttpAuthHeader.Single<\/strong><\/code> \u2014 \u0445\u044d\u0434\u0435\u0440 \u043f\u043e \u0442\u0438\u043f\u0443 <code>Header: Scheme Blob.<\/code>\u0414\u0432\u0430 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Basic_access_authentication\" rel=\"noopener noreferrer nofollow\">Basic \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/a> \u0441 \u043b\u043e\u0433\u0438\u043d\u043e\u043c \u0438 \u043f\u0430\u0440\u043e\u043b\u0435\u043c \u0432 base64: <code>Authorization: Basic bG9naW46cGFzc3dvcmQ=<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0441 JWT: <code>Authorization: Bearer &lt;headers&gt;.&lt;payload&gt;.&lt;signature&gt;<\/code>.<\/p>\n<\/li>\n<\/ul>\n<h2>\u041a\u0430\u043a \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c JWT \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e<\/h2>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"kotlin\">implementation(\"io.ktor:ktor-server-auth-jwt:$ktor_version\")  \/\/ \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u044f \u043e\u0442\u043b\u043e\u0432\u0430 \u043e\u0448\u0438\u0431\u043e\u043a implementation(\"io.ktor:ktor-server-status-pages:$ktor_version\")<\/code><\/pre>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c:<\/p>\n<pre><code class=\"kotlin\">routing {     \/\/ \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043e\u0434\u043d\u043e\u0433\u043e Authentication.provider     authenticate(\"ClientA\") {          get(\"\/\") {             \/\/ 0. \u0414\u043e\u0441\u0442\u0430\u0435\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0435 (payload \u0442\u043e\u043a\u0435\u043d\u0430)             val client = call.principal()             call.respondText(\"Hello World!\")         }     } } <\/code><\/pre>\n<p>\u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u0443\u0435\u043c JWT-\u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e. \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0443\u043a\u0430\u0437\u0430\u043d\u044b \u0432 \u0442\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u043a\u043e\u0434\u0435:<\/p>\n<pre><code class=\"kotlin\">install(Authentication) {     jwt(\"ClientA\") { \/\/ \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u043c AuthenticationConfig.providers          \/\/ 1. \u0423\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c, \u043a\u0430\u043a \u0434\u043e\u0441\u0442\u0430\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438         authHeader {call -&gt; \/*...*\/ }          \/\/ 2. \u0426\u0435\u043b\u044c \u0431\u043b\u043e\u043a\u0430 \u2014 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0444\u043e\u0440\u043c\u0430\u0442, \u043f\u043e\u0434\u043f\u0438\u0441\u044c \u0438 \u0441\u0440\u043e\u043a \u0433\u043e\u0434\u043d\u043e\u0441\u0442\u0438         verifier { httpAuthHeader -&gt; \/*...*\/ }          \/\/ 3. \u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f JWT.payload (\u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u043f\u043e\u043b\u0435 \u043d\u0435 \u0442\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0438\u043b\u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442)         validate { credential: JWTCredential -&gt; \/*...*\/ }          \/\/ 4. \u0420\u0435\u0430\u0433\u0438\u0440\u0443\u0435\u043c \u043d\u0430 \u043e\u0448\u0438\u0431\u043a\u0443         challenge { defaultScheme, realm -&gt; \/*...*\/}     } }  \/\/ \u041e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e.  install(StatusPages) {     \/\/ 5. \u0421\u044e\u0434\u0430 \u043f\u0440\u0438\u043b\u0435\u0442\u0430\u044e\u0442 \u043e\u0448\u0438\u0431\u043a\u0438 \u0438\u0437 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0431\u043b\u043e\u043a\u043e\u0432.     exception { call, e -&gt;         call.application.log.error(\"Error\", e)         \/\/ \u0442\u0443\u0442 \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0442\u0432\u0435\u0442 \u0441 call.respond     } }  \/\/ 6. \u0413\u0434\u0435-\u0442\u043e \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0430\u0445 \u043a\u0442\u043e\u0440 \/\/ internal val JWTLogger: Logger = LoggerFactory.getLogger(\"io.ktor.auth.jwt\") <\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0431\u043b\u043e\u043a <code>challenge<\/code> \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 \u0432 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0447\u0442\u043e-\u0442\u043e \u043d\u0435 \u0442\u0430\u043a, \u043a\u0430\u043a \u0438 \u0431\u044b\u043b\u043e \u0437\u0430\u044f\u0432\u043b\u0435\u043d\u043e. <\/p>\n<blockquote>\n<p>The\u00a0<code>challenge<\/code>\u00a0function allows you to configure a response to be sent if authentication fails. \u2014 \u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f Ktor<\/p>\n<\/blockquote>\n<p>\u0415\u0441\u043b\u0438 \u0431\u044b \u0431\u044b\u043b\u043e \u0442\u0430\u043a, \u0442\u043e \u0438 \u0441\u0442\u0430\u0442\u044c\u044f \u0431\u044b \u043d\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u0430\u0441\u044c.<\/p>\n<p>\u041e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0442\u0435\u0440\u043c\u0438\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0434\u0430\u043b\u044c\u0448\u0435: <strong>\u0423\u0421\u041f\u0415\u0428\u041d\u042b\u0419 \u0421\u0426\u0415\u041d\u0410\u0420\u0418\u0419<\/strong> \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438. \u0421\u0446\u0435\u043d\u0430\u0440\u0438\u0439 \u0431\u0443\u0434\u0435\u0442 \u0443\u0441\u043f\u0435\u0448\u043d\u044b\u043c, \u0435\u0441\u043b\u0438 <code>authHeader<\/code>(1) \u0432\u0435\u0440\u043d\u0435\u0442 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a, <code>verifier<\/code>(2) \u043d\u0435 \u0443\u043f\u0430\u0434\u0435\u0442, <code>validate<\/code>(3) \u0432\u0435\u0440\u043d\u0435\u0442 non-null <code>Principal<\/code>, <code>challenge<\/code>(4) \u0438<code>exception<\/code>(5) \u0431\u043b\u043e\u043a\u0438 \u043d\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u044f\u0442\u0441\u044f.<\/p>\n<h3>\u0414\u0435\u0442\u0430\u043b\u0438 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u0441 \u043e\u0434\u043d\u0438\u043c Authentication.provider\u043c<\/h3>\n<p>\u0412\u0441\u0435, \u0447\u0442\u043e \u043d\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u043f\u043e <strong>\u0443\u0441\u043f\u0435\u0448\u043d\u043e\u043c\u0443 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044e<\/strong>, \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u043e\u0442\u0432\u0435\u0442\u0443 401.<\/p>\n<p><strong>\u041f\u0443\u043d\u043a\u0442 1, <\/strong><code><strong>authHeader<\/strong><\/code>. \u0415\u0441\u043b\u0438 \u0435\u0433\u043e \u043d\u0435 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u00abAuthorization\u00bb. \u0415\u0441\u043b\u0438 \u0438\u0437 \u043d\u0435\u0433\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c null, \u043f\u043e\u043f\u0430\u0434\u0451\u043c \u0432 <code>challenge<\/code>(4) \u0431\u043b\u043e\u043a. \u0415\u0441\u043b\u0438 \u0432 \u043d\u0435\u043c \u0443\u043f\u0430\u0441\u0442\u044c, \u0442\u043e \u043d\u0435 \u043f\u043e\u043f\u0430\u0434\u0435\u043c \u0432<code>challenge<\/code>(4) \u0438 \u0432 \u043b\u043e\u0433\u0438 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043d\u0430\u043f\u0438\u0448\u0435\u0442\u0441\u044f (6), \u043d\u043e \u043f\u043e\u043f\u0430\u0434\u0435\u043c \u0432 <code>exception<\/code>(5). \u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"kotlin\">authHeader { call -&gt;    call.request.header(\"X-Custom-Auth\")?.let { parseAuthorizationHeader(it) } }<\/code><\/pre>\n<p><strong>\u041f\u0443\u043d\u043a\u0442 2, <\/strong><code><strong>verifier<\/strong><\/code>. \u0411\u043b\u043e\u043a \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u2014 \u0435\u0441\u043b\u0438 \u043d\u0435 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u043f\u043e\u043f\u0430\u0434\u0451\u043c \u0432 <code>challenge<\/code> (4). \u0415\u0441\u043b\u0438 \u0443\u043f\u0430\u0441\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u0438 \u0431\u043b\u043e\u043a\u0430, \u043f\u043e\u043f\u0430\u0434\u0451\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043b\u043e\u0433 (6). \u0415\u0441\u043b\u0438 JWTVerifier \u0432\u0435\u0440\u043d\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443, \u043f\u043e\u043f\u0430\u0434\u0435\u043c \u0432 <code>challenge<\/code> (4), \u043d\u043e \u0431\u0435\u0437 \u0434\u0435\u0442\u0430\u043b\u0435\u0439 \u043f\u0430\u0434\u0435\u043d\u0438\u044f. \u0414\u0435\u0442\u0430\u043b\u0438 \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0439\u0442\u0438 \u0432 \u043b\u043e\u0433\u0435 (6). \u0412 <code>exception<\/code> \u0442\u043e\u0436\u0435 \u043d\u0435 \u043f\u043e\u043f\u0430\u0434\u0435\u043c. <a href=\"https:\/\/ktor.io\/docs\/server-jwt.html#configure-verifier\" rel=\"noopener noreferrer nofollow\">\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0435\u0441\u0442\u044c \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a>, \u0432\u043e\u0442 \u0435\u0449\u0435 \u043e\u0434\u0438\u043d, \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u043e\u0433\u043e \u043a\u043b\u044e\u0447\u0430:<\/p>\n<pre><code class=\"kotlin\">private val keyFactory: KeyFactory = KeyFactory.getInstance(\"RSA\")  fun jwtVerifier(): JWTVerifier {     val publicKey = File(\"path\/to\/public.pem\").readText()         .replace(\"\\n\", \"\")         .replace(\"\\r\", \"\")         .replace(\"-----BEGIN PUBLIC KEY-----\", \"\")         .replace(\"-----END PUBLIC KEY-----\", \"\")     val keySpecX509 = X509EncodedKeySpec(Base64.getDecoder().decode(publicKeyContent))     val pubKey = keyFactory.generatePublic(keySpecX509) as RSAPublicKey     val algo = Algorithm.RSA256(pubKey, null)     return JWT.require(algo).build() }  verifier { httpAuthHeader -&gt;     if (httpAuthHeader is HttpAuthHeader.Single) {         jwtVerifier()     } else {         environment.log.error(\"HttpAuthHeader does not adhere scheme:  \")         null     } }<\/code><\/pre>\n<p><strong>\u041f\u0443\u043d\u043a\u0442 3, <\/strong><code><strong>validate<\/strong><\/code>. \u0414\u043b\u044f <strong>\u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0433\u043e \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f<\/strong> \u043d\u0443\u0436\u043d\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c non-null \u043e\u0431\u044a\u0435\u043a\u0442 \u0438\u0437 \u0431\u043b\u043e\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0442\u044c \u043a\u043b\u0438\u0435\u043d\u0442\u0430. \u0415\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0447\u0435\u0440\u0435\u0437 \u0432\u044b\u0437\u043e\u0432 <code>call.principal&lt;T&gt;()<\/code>.<\/p>\n<p>\u041f\u043e \u0437\u0430\u0434\u0443\u043c\u043a\u0435 \u0430\u0432\u0442\u043e\u0440\u043e\u0432 <strong>\u043d\u0430 \u043e\u0448\u0438\u0431\u043a\u0430\u0445 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043d\u0435 \u043f\u0430\u0434\u0430\u0442\u044c, \u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c null<\/strong>. \u0415\u0441\u043b\u0438 \u0436\u0435 \u0432 \u0431\u043b\u043e\u043a\u0435 \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u0442\u0441\u044f \u043e\u0448\u0438\u0431\u043a\u0430, \u043c\u044b \u043d\u0435 \u043f\u043e\u043f\u0430\u0434\u0451\u043c \u043d\u0438 \u0432<code>challenge<\/code>(4), \u043d\u0438 \u0432<code>exception<\/code>(5), \u043d\u043e \u043d\u0430\u043f\u0435\u0447\u0430\u0442\u0430\u0435\u043c \u043b\u043e\u0433 (6). \u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"kotlin\">validate { credential: JWTCredential -&gt;     val scopes = credential.payload.getClaim(\"scopes\").asList(String::class.java)     if (scopes == null) return@validate null     JWTPrincipal(credential.payload) }<\/code><\/pre>\n<p><strong>\u041f\u0443\u043d\u043a\u0442 4, <\/strong><code><strong>challenge<\/strong><\/code>. \u0411\u043b\u043e\u043a \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445:<\/p>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 <code>authHeader<\/code> \u0432\u0435\u0440\u043d\u0435\u0442 null.<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b\u0438 <code>verifier<\/code> (2) (\u0441\u0442\u0440\u0430\u043d\u043d\u043e, \u0447\u0442\u043e \u043e\u043d\u0438 \u0432\u043e\u043e\u0431\u0449\u0435 \u0434\u0430\u044e\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435).<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 <code>JWTVerifier<\/code>, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u0432 <code>verifier<\/code>(2), \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 <code>validate<\/code>(3) \u0432\u0435\u0440\u043d\u0435\u0442 <code>null<\/code>. \u0415\u0441\u043b\u0438 \u0432\u043d\u0443\u0442\u0440\u0438 \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0435\u0442 \u043f\u0430\u0434\u0435\u043d\u0438\u0435, \u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438\u0434\u0435\u0442 \u0432 <code>exception<\/code>. \u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"kotlin\">challenge { defaultScheme: String, realm: String -&gt;   \/\/ Bearer, Ktor Server     val failures: List&lt;AuthenticationFailedCause&gt; = call.authentication.allFailures     failures.forEach { call.application.log.error(\"failure: $it\") }          call.respondText(status = HttpStatusCode.Unauthorized) {        \"Authentication error, but what exactly?\"      } }<\/code><\/pre>\n<p>\u0412\u0441\u0451 \u0431\u044b \u043d\u0438\u0447\u0435\u0433\u043e, \u043d\u043e \u0432\u044b\u0442\u0430\u0449\u0438\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043f\u0440\u0438\u0447\u0438\u043d\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u043d\u0435 \u0442\u0430\u043a-\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e. <code>AuthenticationFailedCause<\/code> \u2014 \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442 \u0431\u0435\u0437 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438: \u043b\u0438\u0431\u043e <code>NoCredentials<\/code>, \u043b\u0438\u0431\u043e <code>InvalidCredentials<\/code>. \u0410 \u0432\u043e\u0442 <code>AuthenticationFailedCause.Error<\/code> \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0438\u043a\u0435\u043c \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 <code>JWTAuthenticationProvider<\/code>. <code>Error<\/code> \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0439\u0442\u0438 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 Ktor oauth, \u0447\u0442\u043e \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0440\u0430\u043c\u043a\u0438 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438.<\/p>\n<h2>\u0422\u0430\u0431\u043b\u0438\u0446\u0430 \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u044f Ktor jwt auth<\/h2>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th data-colwidth=\"339\" width=\"339\">\n<p align=\"left\">\u041e\u0448\u0438\u0431\u043a\u0430 \u0432 \u0431\u043b\u043e\u043a\u0435 \u2193<\/p>\n<\/th>\n<th data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">challenge<\/p>\n<\/th>\n<th>\n<p align=\"left\">log<\/p>\n<\/th>\n<th>\n<p align=\"left\">exception<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>authHeader<\/code> \u0432\u0435\u0440\u043d\u0443\u043b null<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>authHeader<\/code> \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u043b \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<td>\n<p align=\"left\">+<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>verifier<\/code> \u043d\u0435 \u0437\u0430\u0434\u0430\u043d<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>verifier<\/code> \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u043b \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<td>\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>JWTVerifier<\/code> \u0432\u0435\u0440\u043d\u0443\u043b \u043e\u0448\u0438\u0431\u043a\u0443<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>validate<\/code> \u0432\u0435\u0440\u043d\u0443\u043b null<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>validate<\/code> \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u043b \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<td>\n<p align=\"left\">+<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"339\" width=\"339\">\n<p align=\"left\"><code>challenge<\/code> \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u043b \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<td data-colwidth=\"175\" width=\"175\">\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u2013<\/p>\n<\/td>\n<td>\n<p align=\"left\">+<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u0425\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b, \u0447\u0442\u043e challenge-\u0431\u043b\u043e\u043a \u043e\u0442\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u043b \u0432\u043e \u0432\u0441\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 (\u043a\u0440\u043e\u043c\u0435 \u0441\u0430\u043c\u043e\u0433\u043e challenge-\u0431\u043b\u043e\u043a\u0430), \u0438 \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u043f\u043e\u0434\u043e\u0431\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0432 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435 (<a href=\"https:\/\/github.com\/D00mch\/ktor_jwt_example\/blob\/main\/src\/main\/kotlin\/auth\/jwt\/JWTAuth.kt\" rel=\"noopener noreferrer nofollow\">github<\/a>).<\/p>\n<h3>\u041a\u0430\u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0438\u043f\u043e\u0432 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438<\/h3>\n<p>\u0412\u0441\u0435 \u0432\u044b\u0448\u0435\u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0430 \u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u043f\u0440\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0442\u0438\u043f\u043e\u0432 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0439 <code>AuthenticationStrategy<\/code>.<\/p>\n<pre><code class=\"kotlin\">routing {     authenticate(         \"ClientA\",         \"ClientB\",         \/\/ \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e         strategy = AuthenticationStrategy.FirstSuccessful,     ) {\/*...*\/} }<\/code><\/pre>\n<p>\u0418\u043c\u0435\u044e\u0449\u0438\u0435\u0441\u044f 3 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \u043f\u043e\u043d\u044f\u0442\u043d\u044b \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u043e:<\/p>\n<pre><code class=\"kotlin\">enum class AuthenticationStrategy { Optional, FirstSuccessful, Required }<\/code><\/pre>\n<p><code><strong>AuthenticationStrategy.Optional<\/strong><\/code> \u2014 \u0435\u0441\u043b\u0438 \u043d\u0438 \u043e\u0434\u0438\u043d \u0438\u0437 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 \u043d\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u0442 <strong>\u0443\u0441\u043f\u0435\u0448\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong> \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438, \u0442\u043e  \u043c\u044b \u043d\u0435 \u043f\u043e\u043f\u0430\u0434\u0435\u043c \u0432 <code>exception<\/code> (5) \u0431\u043b\u043e\u043a \u0438 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c 401, \u0430 \u0432\u043d\u0443\u0442\u0440\u0438 \u0440\u0443\u0447\u043a\u0438 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <code>call.principal()<\/code> \u0432\u0435\u0440\u043d\u0435\u0442\u0441\u044f <code>null<\/code>.<\/p>\n<p><code><strong>AuthenticationStrategy.FirstSuccessful<\/strong><\/code> \u2014 \u043f\u0435\u0440\u0432\u044b\u0439 \u0438\u0437 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0439\u0434\u0435\u0442 <strong>\u0443\u0441\u043f\u0435\u0448\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong>, \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0438 \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0438\u043a\u043e\u043c\u0443. Jwt-\u0431\u043b\u043e\u043a\u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432  \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u0437\u0432\u0430\u043d\u044b \u0432 \u0442\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 <code>authenticate<\/code>. \u0422\u0435, \u043a\u0442\u043e \u0443\u0441\u043f\u0435\u043b \u0443\u043f\u0430\u0441\u0442\u044c \u0432 \u043b\u044e\u0431\u043e\u043c \u0438\u0437 \u0431\u043b\u043e\u043a\u043e\u0432, \u043d\u0438 \u043d\u0430 \u0447\u0442\u043e \u043d\u0435 \u043f\u043e\u0432\u043b\u0438\u044f\u044e\u0442, \u043a\u0440\u043e\u043c\u0435 \u043b\u043e\u0433\u043e\u0432 (6), \u0430 <code>challenge<\/code>(4) \u0438 <code>exception<\/code>(5) \u043d\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u044f\u0442\u0441\u044f. \u0412 <code>call.principal()<\/code> \u0432\u0435\u0440\u043d\u0435\u0442\u0441\u044f <code>Principal<\/code> \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0433\u043e. \u0415\u0441\u043b\u0438 \u043d\u0438\u043a\u0442\u043e \u043d\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u0442 <strong>\u0443\u0441\u043f\u0435\u0448\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong>, \u0432\u0435\u0440\u043d\u0435\u0442\u0441\u044f 401 \u0438\u043b\u0438 \u0442\u043e, \u0447\u0442\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043e \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 <code>challenge<\/code>(4) \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438\u043b\u0438 \u043e\u0431\u0449\u0435\u0433\u043e \u0431\u043b\u043e\u043a\u0430 <code>exception<\/code>(5).<\/p>\n<p><code><strong>AuthenticationStrategy.Required<\/strong><\/code> \u2014 \u0432\u0441\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0440\u043e\u0439\u0442\u0438 <strong>\u0443\u0441\u043f\u0435\u0448\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439<\/strong>, \u0438\u043d\u0430\u0447\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c 401. \u0412 <code>call.principal&lt;Any&gt;()<\/code> \u0432\u0435\u0440\u043d\u0435\u0442\u0441\u044f principal \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u0432 <code>authenticate<\/code> \u043a\u043b\u0438\u0435\u043d\u0442\u0430 (\u00abClientA\u00bb \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b\u0448\u0435 \u0438\u0437 \u044d\u0442\u043e\u0433\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0430). \u041d\u043e \u0435\u0441\u043b\u0438 \u0432\u044b\u0437\u0432\u0430\u0442\u044c <code>call.principal&lt;ClientBPrincipal&gt;()<\/code>, \u0442\u043e \u0432\u0435\u0440\u043d\u0435\u0442\u0441\u044f <code>ClientBPrincipal<\/code>.<\/p>\n<h3>\u0413\u043b\u0443\u0431\u043e\u043a\u043e\u0435 \u0437\u0430\u0433\u043b\u0430\u0442\u044b\u0432\u0430\u043d\u0438\u0435<\/h3>\n<p>\u041a\u0442\u043e\u0440 \u043f\u043e\u0440\u0430\u0436\u0430\u0435\u0442 \u0433\u0438\u0431\u043a\u043e\u0441\u0442\u044c\u044e, \u0434\u043e\u0441\u0442\u043e\u0439\u043d\u043e\u0439 \u0441\u0430\u043c\u044b\u0445 \u0441\u043c\u0435\u043b\u044b\u0445 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0439 \u0441 \u043f\u043e\u0434\u043f\u0438\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u0440\u0435\u0431\u0440\u0430\u043c\u0438.<\/p>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435, \u0447\u0442\u043e \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u043e\u043f\u0430\u0441\u0442\u044c \u0432 \u043a\u0440\u0430\u0440\u0442\u0438\u0440\u0443. \u0412 \u0434\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043f\u0430\u0441\u0442\u044c \u043b\u0438\u0431\u043e \u0447\u0435\u0440\u0435\u0437 \u043f\u0430\u0440\u0430\u0434\u043d\u044b\u0439 \u043f\u043e\u0434\u044a\u0435\u0437\u0434 \u0441 \u043b\u0438\u0444\u0442\u043e\u043c, \u043b\u0438\u0431\u043e \u0447\u0435\u0440\u0435\u0437 \u0431\u043e\u043a\u043e\u0432\u043e\u0439 \u043f\u043e\u0434\u044a\u0435\u0437\u0434 \u0441 \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435\u0439. \u041e\u0442 \u043e\u0431\u043e\u0438\u0445 \u043f\u043e\u0434\u044a\u0435\u0437\u0434\u043e\u0432 \u043d\u0443\u0436\u043d\u044b \u043a\u043b\u044e\u0447\u0438. \u0412\u043d\u0443\u0442\u0440\u0438 \u0434\u043e\u043c\u0430 \u0431\u0443\u0434\u0435\u0442 \u043a\u0432\u0430\u0440\u0442\u0438\u0440\u0430, \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0442\u043e\u0436\u0435 \u043d\u0443\u0436\u0435\u043d \u043a\u043b\u044e\u0447.<\/p>\n<pre><code class=\"kotlin\">object Auth {   val scope1 = \"\u043a\u043b\u044e\u0447 \u043e\u0442 \u043f\u0430\u0440\u0430\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u044a\u0435\u0437\u0434\u0430\"    val scope2 = \"\u043a\u043b\u044e\u0447 \u043e\u0442 \u0431\u043e\u043a\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u044a\u0435\u0437\u0434\u0430\"   val scope3 = \"\u043a\u043b\u044e\u0447 \u043e\u0442 \u043a\u0432\u0430\u0440\u0442\u0438\u0440\u044b\" }  route(\"building\/\") {    authenticate(Auth.scope1, Auth.scope2, strategy = FirstSuccessful) {      route(\"flat\") {        authenticate(Auth.scope3, strategy = Required) {          get() { \/*..*\/ }         post() { \/*..*\/ }       }     }   } }<\/code><\/pre>\n<p>\u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a, \u0447\u0442\u043e \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a <code>flat<\/code> \u043d\u0443\u0436\u043d\u044b 2 \u043a\u043b\u044e\u0447\u0430 \u2014 \u043e\u0434\u0438\u043d \u043e\u0442 \u0434\u043e\u043c\u0430 (scope1 \u0438\u043b\u0438 scope2), \u0434\u0440\u0443\u0433\u043e\u0439 \u043e\u0442 \u043a\u0432\u0430\u0440\u0442\u0438\u0440\u044b (scope3). <\/p>\n<p><strong>\u041d\u043e \u044d\u0442\u043e \u043d\u0435 \u0442\u0430\u043a<\/strong>!<\/p>\n<p>\u0414\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0440\u0443\u0447\u043a\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043b\u044e\u0431\u043e\u0433\u043e \u0438\u0437 \u043a\u043b\u044e\u0447\u0435\u0439, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043d\u0435 <code>scope3<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043c\u0435\u0447\u0435\u043d \u043a\u0430\u043a <code>required<\/code>. \u0422\u0430\u043a\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043e\u0442 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0432\u0441\u0435 <code>authenticate<\/code> \u0440\u0430\u0432\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u044b, \u0430 \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0439 \u0448\u043b\u0430 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f <code>FirstSuccessful<\/code>.<\/p>\n<p>\u041d\u043e \u0447\u0442\u043e, \u0435\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 <code>(scope1 || scope2) &amp;&amp; scope3<\/code>. \u041a\u0430\u043a \u0434\u0443\u043c\u0430\u0435\u0442\u0435, \u0431\u0443\u0434\u0443\u0442 \u043b\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0434\u0432\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0438\u0436\u0435?<\/p>\n<pre><code class=\"kotlin\">authenticate(\"scope3\", strategy = AuthenticationStrategy.Required) {    route(\"building\/flat\") {      authenticate(\"scope1\", strategy = AuthenticationStrategy.Required) {        get() { \/*..*\/ }       post() { \/*..*\/ }     }     authenticate(\"scope2\", strategy = <\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-465499","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/465499","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=465499"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/465499\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=465499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=465499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=465499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}