{"id":486527,"date":"2026-07-07T16:48:15","date_gmt":"2026-07-07T16:48:15","guid":{"rendered":"https:\/\/savepearlharbor.com\/?p=486527"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=486527","title":{"rendered":"Spring Security: \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0447\u0435\u0440\u0435\u0437 REST"},"content":{"rendered":"<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0414\u043e\u0431\u0440\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u0443\u0442\u043e\u043a! \u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0447\u0435\u0440\u0435\u0437 REST. \u041d\u043e \u043f\u0435\u0440\u0435\u0434 \u0441\u0430\u043c\u043e\u0439 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0435\u0439, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0437\u043d\u0430\u0442\u044c \u043e\u0431 \u043e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u043e\u0439 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u0439 &#8212; <code>PasswordEncoder<\/code>.<\/p>\n<h2>PasswordEncoder<\/h2>\n<p>PasswordEncoder &#8212; \u044d\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 Spring Security, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u043d\u0430 \u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u043f\u0430\u0440\u043e\u043b\u044c \u0432 \u043d\u0435\u0447\u0438\u0442\u0430\u0435\u043c\u0443\u044e \u043a\u0430\u0448\u0443, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u043e, \u0434\u0430\u0436\u0435 \u043f\u0440\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0438, \u0435\u0441\u043b\u0438 \u043a\u0442\u043e-\u0442\u043e \u0441\u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043b\u0435\u0437\u0442\u044c \u0432 \u0431\u0434.<\/p>\n<p>\u0410 \u0437\u0430\u0447\u0435\u043c \u043e\u043d \u043d\u0443\u0436\u0435\u043d? \u0414\u043b\u044f \u0445\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u0440\u043e\u043b\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0436\u0435 \u0438 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c\u0441\u044f \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0435\u0441\u043b\u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f \u0432 \u043e\u0431\u044b\u0447\u043d\u043e\u043c \u0432\u0438\u0434\u0435 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 &#171;password123&#187;, \u0442\u043e \u0435\u0441\u043b\u0438 \u0445\u0430\u0446\u043a\u0435\u0440\u044b \u0441\u043c\u043e\u0433\u0443\u0442 \u0437\u0430\u043b\u0435\u0437\u0442\u044c \u0432 \u0431\u0430\u0437\u0443 \u0438 \u0443\u043a\u0440\u0430\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435, \u0442\u043e \u043e\u043d\u0438 \u0441\u043c\u043e\u0433\u0443\u0442 \u0437\u0430\u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u044b \u0432\u043b\u0430\u0434\u0435\u043b\u044c\u0446\u0435\u0432 \u044d\u0442\u0438\u0445 \u0436\u0435 \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u043e\u0432, \u0430 \u0435\u0441\u043b\u0438 \u0445\u0430\u0446\u043a\u0435\u0440\u044b \u0441\u043c\u043e\u0433\u0443\u0442 \u0437\u0430\u043b\u0435\u0437\u0442\u044c \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u0430\u0440\u043e\u043b\u0438 \u0431\u0443\u0434\u0443\u0442 \u0437\u0430\u0445\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u044b, \u0442\u043e \u043e\u043d\u0438 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u0430\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439. <\/p>\n<p>\u041c\u044b \u043f\u043e\u043d\u044f\u043b\u0438 \u0447\u0442\u043e \u043d\u0430\u0434\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0445\u044d\u0448, \u043d\u043e \u0447\u0442\u043e \u044d\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0438 \u043a\u0430\u043a \u043e\u043d \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f?&#8230;<br \/>\u0425\u044d\u0448 &#8212; \u044d\u0442\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u043d\u0430\u0437\u0430\u0434. (\u041f\u043e \u0443\u043c\u043d\u043e\u043c\u0443 \u044d\u0442\u043e <em>\u043a\u0440\u0438\u043f\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0445\u044d\u0448<\/em>, \u043d\u043e \u0431\u0443\u0434\u0435\u043c \u044d\u0442\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e <em>\u0445\u044d\u0448<\/em>)<\/p>\n<figure class=\"full-width \"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/193\/5d3\/ade\/1935d3adeae02d555480a2cafe72f1a6.png\" alt=\"\u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0445\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u0440\u043e\u043b\u044f \u0447\u0435\u0440\u0435\u0437 bcrypt\" title=\"\u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0445\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u0440\u043e\u043b\u044f \u0447\u0435\u0440\u0435\u0437 bcrypt\" width=\"688\" height=\"454\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/193\/5d3\/ade\/1935d3adeae02d555480a2cafe72f1a6.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/193\/5d3\/ade\/1935d3adeae02d555480a2cafe72f1a6.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0445\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u0440\u043e\u043b\u044f \u0447\u0435\u0440\u0435\u0437 bcrypt<\/figcaption><\/div>\n<\/figure>\n<p>*\u0412\u0430\u0436\u043d\u043e\u0435 \u0443\u0442\u043e\u0447\u043d\u0435\u043d\u0438\u0435, \u0447\u0442\u043e <code>cost<\/code> &#8212; \u044d\u0442\u043e \u0447\u0438\u0441\u043b\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0437\u0430\u0434\u0430\u0451\u0442 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438. \u041e\u0431\u044b\u0447\u043d\u043e \u044d\u0442\u043e \u0447\u0438\u0441\u043b\u043e \u043e\u0442 4 \u0434\u043e 31, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u041f\u0440\u0438\u043c\u0435\u0440 \u0431\u0443\u0434\u0435\u0442 \u043d\u0438\u0436\u0435). \u0410 \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c <code>\u0440\u0430\u0443\u043d\u0434<\/code> &#8212; \u044d\u0442\u043e \u043e\u0434\u0438\u043d \u043f\u0440\u043e\u0445\u043e\u0434 \u0447\u0435\u0440\u0435\u0437 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043a \u0434\u0430\u043d\u043d\u044b\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437 \u043f\u043e\u0434\u0440\u044f\u0434.<\/p>\n<p>\u041a\u0430\u043a \u0432\u0441\u0451 \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442?<\/p>\n<ol>\n<li>\n<p>\u0421\u043e\u043b\u044c \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e (\u0432 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435 \u043d\u0435\u0442 \u043d\u0438\u0447\u0435\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0433\u043e, \u043d\u043e \u043d\u0435 \u043e\u0431 \u044d\u0442\u043e\u043c) \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438\u0437 16 \u0431\u0430\u0439\u0442, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u0430 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u0430\u0440\u043e\u043b\u044f. \u042d\u0442\u043e \u0437\u0430\u0449\u0438\u0449\u0430\u0435\u0442 \u043e\u0442 rainbow-\u0442\u0430\u0431\u043b\u0438\u0446 (\u0434\u0430\u0436\u0435 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0435 \u043f\u0430\u0440\u043e\u043b\u0438 \u0434\u0430\u044e\u0442 \u0440\u0430\u0437\u043d\u044b\u0439 \u0445\u044d\u0448)<\/p>\n<\/li>\n<li>\n<p>\u041f\u0430\u0440\u043e\u043b\u044c \u0438 \u0441\u043e\u043b\u044c \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u044e\u0442\u0441\u044f \u0438 \u043f\u0440\u043e\u0445\u043e\u0434\u044f\u0442 \u0447\u0435\u0440\u0435\u0437 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c Blowfish \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0438\u0432\u0430\u0435\u0442 \u0441\u043e\u043b\u044c \u0438 \u043f\u0430\u0440\u043e\u043b\u044c. <\/p>\n<\/li>\n<li>\n<p>Cost-\u0444\u0430\u043a\u0442\u043e\u0440 \u0437\u0430\u0434\u0430\u0451\u0442 \u0447\u0438\u0441\u043b\u043e <code>\u0440\u0430\u0443\u043d\u0434\u043e\u0432<\/code> \u043a\u0430\u043a 2^cost. \u0427\u0435\u043c \u0432\u044b\u0448\u0435 cost, \u0442\u0435\u043c \u0434\u043e\u043b\u044c\u0448\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 &#8212; \u044d\u0442\u043e \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0437\u0430\u043c\u0435\u0434\u043b\u044f\u0435\u0442 brute-force, \u043a\u043e\u0433\u0434\u0430 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u043b\u043e\u0433\u0438\u043d\u0430 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430.<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u0432\u0448\u0430\u044f\u0441\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0447\u0430\u0441\u0442\u0435\u0439: \u0432\u0435\u0440\u0441\u0438\u044e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430, cost, \u0441\u043e\u043b\u044c \u0438 \u0445\u044d\u0448. \u042d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043a\u0430\u043a-\u0442\u043e \u0442\u0430\u043a:<code>$2b$10$N9qo8uLOickgx2ZMRZoMy...<\/code>. \u041f\u0440\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0435 \u043f\u0430\u0440\u043e\u043b\u044f bcrypt \u0434\u043e\u0441\u0442\u0430\u0451\u0442 \u0441\u043e\u043b\u044c \u0438 cost \u0438\u0437 \u043f\u0430\u0440\u043e\u043b\u044f \u043f\u0440\u044f\u043c\u043e \u0438\u0437 \u044d\u0442\u043e \u0441\u0442\u0440\u043e\u043a\u0438, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442.<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u043e\u0447\u0435\u043c\u0443 \u043b\u0443\u0447\u0448\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e BCrypt, \u0430 \u043d\u0435 SHA-256:<\/p>\n<p><code>SHA-256<\/code> &#8212; \u044d\u0442\u043e \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u0445\u044d\u0448 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0441 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c\u044e, \u0438 \u043e\u043d\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0430 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0446\u0435\u043b\u043e\u0441\u0442\u043d\u043e\u0441\u0442\u0438 \u0444\u0430\u0439\u043b\u043e\u0432. \u041d\u043e \u0434\u043b\u044f \u043f\u0430\u0440\u043e\u043b\u0435\u0439 \u044d\u0442\u043e \u043c\u0438\u043d\u0443\u0441, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432\u0438\u0434\u0435\u043e\u043a\u0430\u0440\u0442\u0430 \u043c\u043e\u0436\u0435\u0442 \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0442\u044c \u0430\u0444\u0438\u0433\u0435\u0442\u044c \u043a\u0430\u043a \u043c\u043d\u043e\u0433\u043e SHA-256 \u0445\u044d\u0448\u0435\u0439 \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443, \u0447\u0435\u0440\u0435\u0437 brute-force.<br \/><code>BCrypt<\/code> &#8212; \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0439 \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u043e\u0434\u043c\u0435\u0448\u0438\u0432\u0430\u0435\u0442 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u0443\u044e \u0441\u043e\u043b\u044c, \u043e\u043d \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0434\u043e\u043b\u0433\u0438\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0445\u044d\u0448, \u0442\u0430\u043a \u0447\u0442\u043e \u0431\u0440\u0443\u0442\u0444\u043e\u0440\u0441 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0442\u044f\u0436\u0451\u043b\u044b\u043c. \u0414\u0430\u0436\u0435 \u0441 \u043c\u043e\u0449\u043d\u044b\u043c \u0436\u0435\u043b\u0435\u0437\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043b\u0438\u0448\u044c \u0442\u044b\u0441\u044f\u0447\u0438 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443, \u0430 \u043d\u0435 \u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434\u044b. \u0412 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441\u043e\u043b\u044c \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0433\u043e \u043f\u0430\u0440\u043e\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0445\u044d\u0448\u0438 \u0434\u043b\u044f \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u043f\u0430\u0440\u043e\u043b\u0435\u0439, \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u0445\u0430\u0446\u043a\u0435\u0440 \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e \u0442\u0430\u043a \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0445\u044d\u0448\u0438 \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439 \u0438 \u043f\u043e\u043d\u044f\u0442\u044c \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0442 \u043b\u0438 \u043f\u0430\u0440\u043e\u043b\u0438 \u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439, \u0438 \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u043f\u0440\u043e\u0441\u0447\u0438\u0442\u0430\u043d\u043d\u044b\u0435 <code>rainbow tables<\/code> \u0434\u043b\u044f \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u0432\u0437\u043b\u043e\u043c\u0430.<\/p>\n<p>\u041a\u0430\u043a \u0442\u0435\u043f\u0435\u0440\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c PasswordEncoder? \u0410 \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u044d\u0442\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e!<\/p>\n<pre><code class=\"java\">@Configurationpublic class PasswordEncoderConfig{  @Bean  public PasswordEncoder passwordEncoder(){    return new BCryptPasswordEncoder(12);  }  }<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:87px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0444\u0438\u0433 \u043b\u0443\u0447\u0448\u0435 \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e, \u0434\u043b\u044f \u0438\u0437\u0431\u0435\u0436\u0430\u043d\u0438\u044f \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439. <br \/>\u0414\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0431\u0438\u043d \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430 \u043f\u0430\u0440\u043e\u043b\u0435\u0439 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 BCrypt \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 PasswordEncoder \u0441 <code>cost<\/code> = 12, \u0442\u043e \u0435\u0441\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0443\u043d\u0434\u043e\u0432 \u0445\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = 2^12. \u0427\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0435 cost, \u0442\u0435\u043c \u043c\u0435\u043b\u0435\u043d\u043d\u0435\u0435 \u0431\u0440\u0443\u0442\u0444\u043e\u0440\u0441, \u043d\u043e \u0442\u0435\u043c \u043d\u0435 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435 \u0441\u0430\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438. \u041f\u043e \u044d\u0442\u043e\u043c\u0443 10-12  \u043c\u043e\u0436\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u043c \u0431\u0430\u043b\u0430\u043d\u0441\u043e\u043c.<\/p>\n<h2>Session + Cookie \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/h2>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0437\u043d\u0430\u043a\u043e\u043c\u044b \u0441 \u0442\u0430\u043a\u0438\u043c\u0438 \u043f\u043e\u043d\u044f\u0442\u0438\u044f\u043c\u0438 \u043a\u0430\u043a \u0441\u0435\u0441\u0441\u0438\u044f \u0438 \u043a\u0443\u043a\u0438, \u0442\u043e \u0441\u043c\u0435\u043b\u043e \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043f\u0443\u043d\u043a\u0442 \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u044f \u0447\u0442\u043e \u044d\u0442\u043e \u0442\u0430\u043a\u043e\u0435.<\/p>\n<p><strong>\u0421\u0435\u0441\u0441\u0438\u044f (\u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430)<\/strong> &#8212; \u044d\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 id \u0441\u0435\u0441\u0441\u0438\u0438, \u0430 \u0441\u0430\u043c \u0441\u0435\u0440\u0432\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u044d\u0442\u043e\u0442 id \u043a\u0430\u043a \u043a\u043b\u044e\u0447 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438. <br \/>\u0415\u0441\u043b\u0438 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u044e \u0441 \u0433\u0430\u0440\u0434\u0435\u0440\u043e\u0431\u043e\u043c \u0432 \u0442\u0435\u0430\u0442\u0440\u0435. \u0412\u044b \u043f\u0440\u0438\u0448\u043b\u0438 \u0432 \u0442\u0435\u0430\u0442\u0440, \u0441\u043d\u044f\u043b\u0438 \u043a\u0443\u0440\u0442\u043a\u0443, \u0441\u0434\u0430\u043b\u0438 \u0432 \u0433\u0430\u0440\u0434\u0435\u0440\u043e\u0431 \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0441\u0432\u043e\u0439 \u043d\u043e\u043c\u0435\u0440\u043e\u043a (\u043e\u043d \u0438 \u0431\u0443\u0434\u0435\u0442 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u043c id \u0441\u0435\u0441\u0441\u0438\u0438), \u043f\u043e\u043a\u0430 \u043a\u0443\u0440\u0442\u043a\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0433\u0430\u0440\u0434\u0435\u0440\u043e\u0431\u0435 &#8212; \u044d\u0442\u043e \u0438 \u0435\u0441\u0442\u044c \u0441\u0435\u0441\u0441\u0438\u044f.<br \/><strong>Cookie (\u043f\u0435\u0447\u0435\u043d\u044c\u043a\u0430)<\/strong> &#8212; \u044d\u0442\u043e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043a\u0443\u0441\u043e\u0447\u0435\u043a \u0442\u0435\u043a\u0441\u0442\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043a\u0430\u043a \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447=\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0435\u0440\u0432\u0435\u0440 \u043f\u0440\u043e\u0441\u0438\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0443 \u0441\u0435\u0431\u044f \u0438 \u043f\u0440\u0438\u0441\u044b\u043b\u0430\u0442\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u043e, \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043a \u044d\u0442\u043e\u043c\u0443 \u0436\u0435 \u0441\u0430\u0439\u0442\u0443. <br \/>\u041e\u043f\u044f\u0442\u044c \u0442\u0430\u043a\u0438, \u0435\u0441\u043b\u0438 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0442\u043e \u0441\u043d\u043e\u0432\u0430 \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441 \u044d\u0442\u0438\u043c \u0436\u0435 \u0433\u0430\u0440\u0434\u0435\u0440\u043e\u0431\u043e\u043c \u0438 \u0442\u0435\u0430\u0442\u0440\u043e\u043c: \u043a\u043e\u0433\u0434\u0430 \u0432\u044b\u0434\u0430\u043b\u0438 \u043d\u043e\u043c\u0435\u0440\u043e\u043a \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432\u0438\u0441\u0438\u0442 \u043a\u0443\u0440\u0442\u043a\u0430 &#8212; \u044d\u0442\u043e cookie, \u0430 \u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u0440\u0438 \u043b\u044e\u0431\u044b\u0445 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0445 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f\u0445 \u0432 \u0442\u0435\u0430\u0442\u0440\u0435 \u043f\u0440\u0438 \u043f\u0440\u043e\u0441\u044c\u0431\u0435 \u043f\u0440\u0435\u0434\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u043c\u0435\u0440\u043a\u0430, \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0435 \u044d\u0442\u043e\u0442 \u043d\u043e\u043c\u0435\u0440\u043e\u043a (cookie) \u0438 \u043d\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c. \u0422\u043e\u043b\u044c\u043a\u043e \u0431\u0440\u0430\u0443\u0437\u0435\u0440 <code>\u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438<\/code> \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043a \u0441\u0430\u0439\u0442\u0443 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 cookie \u0441 id \u0441\u0435\u0441\u0441\u0438\u0438 (\u043d\u043e\u043c\u0435\u0440\u043a\u043e\u043c \u043a\u0443\u0440\u0442\u043a\u0438 \u0432 \u0433\u0430\u0440\u0434\u0435\u0440\u043e\u0431\u0435).<\/p>\n<p>\u041a\u0430\u043a \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c REST \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f:<\/p>\n<ol>\n<li>\n<p>POST \/api\/auth\/login  { \u201cusername\u201d: \u201c\u2026\u201d, \u201cpassword\u201d: \u201c\u2026\u201d }  \u2192 JSON<\/p>\n<\/li>\n<li>\n<p>Spring \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0447\u0435\u0440\u0435\u0437 AuthenticationManager<\/p>\n<\/li>\n<li>\n<p>SecurityContext \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u0432 HttpSession \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435<\/p>\n<\/li>\n<li>\n<p>\u0411\u0440\u0430\u0443\u0437\u0435\u0440\u0443 \u043e\u0442\u0434\u0430\u0451\u0442\u0441\u044f Set-Cookie: JSESSIONID=abc123 (\u0444\u0440\u043e\u043d\u0442\u0435\u043d\u0434 \u0435\u0433\u043e \u043d\u0435 \u0447\u0438\u0442\u0430\u0435\u0442, \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 fetch \u0441 credentials: \u201cinclude\u201d)<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u0445 Spring \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 SecurityContext \u0438\u0437 \u0441\u0435\u0441\u0441\u0438\u0438<\/p>\n<\/li>\n<li>\n<p>\u041b\u044e\u0431\u0430\u044f \u043e\u0448\u0438\u0431\u043a\u0430 (401\/403) \u2014 \u044d\u0442\u043e JSON-\u0442\u0435\u043b\u043e \u0432\u0438\u0434\u0430 { \u201cstatus\u201d: 401, \u201cmessage\u201d: \u201c\u2026\u201d }, \u0430 \u043d\u0435 HTML-\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0438 \u043d\u0435 \u0440\u0435\u0434\u0438\u0440\u0435\u043a\u0442<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c \u043a\u0430\u043a \u043d\u0430\u0447\u0430\u0442\u044c \u043f\u0438\u0441\u0430\u0442\u044c SecurityConfig, \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c <code>ApiResponse<\/code> \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u044b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e, <code>RestAccessDeniedHandler<\/code> \u0438 <code>RestAuthenticationEntryPoint<\/code> \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0431\u0443\u0434\u0443\u0442 \u043d\u0443\u0436\u043d\u044b \u0432 SecurityConfig \u0432 \u0432\u0438\u0434\u0435 JSON-\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u043e\u0448\u0438\u0431\u043e\u043a \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<p>\u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c record ApiResponse:<\/p>\n<pre><code class=\"java\">public record ApiResponse(  int status,  String error,  String message){}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041d\u0438\u0447\u0435\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0433\u043e \u0442\u0443\u0442 \u043d\u0435\u0442, \u0432 API \u043e\u0442\u0432\u0435\u0442\u0435 \u0431\u0443\u0434\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0430\u0442\u0443\u0441, \u043e\u0448\u0438\u0431\u043a\u0430 \u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438, \u043d\u0438\u0447\u0435\u0433\u043e \u0431\u043e\u043b\u0435\u0435.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c RestAccessDeniedHandler:<\/p>\n<pre><code class=\"java\">@Componentpublic class  implements AccessDeniedHandler {    private final ObjectMapper objectMapper = new ObjectMapper();    @Override    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {        response.setStatus(HttpStatus.FORBIDDEN.value());        response.setContentType(MediaType.APPLICATION_JSON_VALUE);        ApiError body = new ApiError(403, \"Forbidden\", accessDeniedException.getMessage());        objectMapper.writeValue(response.getWriter(), body);    }}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u0437\u0430 \u0441\u043b\u0443\u0447\u0430\u0439, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d, \u043d\u043e \u043f\u0440\u0430\u0432 \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043e\u043d \u043f\u043e\u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u0439\u0442\u0438 \u043d\u0430 \u0430\u0434\u043c\u0438\u043d\u0441\u043a\u0438\u0435 url \u0438\u043b\u0438 \u0447\u0443\u0436\u043e\u0439 <code>@PreAuthorize<\/code> \u0438\u0437 ProfileService (\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043d\u043e\u0432\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c). \u0412 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0451\u043d JSON \u0441 \u043a\u043e\u0434\u043e\u043c 403 \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u043e\u0448\u0438\u0431\u043a\u0438.<br \/>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u0435 \u0432 \u043e\u0442\u0432\u0435\u0442 \u0441\u0442\u0430\u0432\u0438\u043c \u0441\u0442\u0430\u0442\u0443\u0441 403 \u0447\u0435\u0440\u0435\u0437 <code>HttpStatus.<em>FORBIDDEN<\/em>.value()<\/code>, \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0434\u043b\u044f \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u0438 \u0441\u0442\u0438\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"http:\/\/HttpServletResponse.SC\" rel=\"noopener noreferrer nofollow\"><code>HttpServletResponse.S<\/code><\/a><code>C_FORBIDDEN<\/code>, \u043d\u043e \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u044b \u043b\u0443\u0447\u0448\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u043e.<br \/>\u0414\u0430\u043b\u0435\u0435 \u0441\u0442\u0430\u0432\u0438\u043c \u0444\u043e\u0440\u043c\u0430\u0442 \u043e\u0442\u0432\u0435\u0442\u0430 JSON \u0447\u0435\u0440\u0435\u0437 <code>MediaType.APPLICATION_JSON_VALUE<\/code>. \u041f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u043d\u0430\u0448 reord \u0441 \u0442\u0435\u043b\u043e\u043c \u043e\u0442\u0432\u0435\u0442\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>response.getWriter()<\/code> (\u043f\u043e\u0442\u043e\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0438\u0448\u0435\u0442 \u043f\u0440\u044f\u043c\u043e \u0432 \u0442\u0435\u043b\u043e HTTP-\u043e\u0442\u0432\u0435\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0443), \u0438 \u0447\u0435\u0440\u0435\u0437 <code>objectMapper<\/code> \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0442\u0435\u043b\u043e \u043e\u0442\u0432\u0435\u0442\u0430 \u0432 JSON.<\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u043f\u043e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0438 \u043d\u0443\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c <code>RestAuthenticationEntryPoint<\/code>:<\/p>\n<pre><code class=\"java\">@Componentpublic class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {    private final ObjectMapper objectMapper = new ObjectMapper();    @Override    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {        response.setStatus(HttpStatus.UNAUTHORIZED.value());        response.setContentType(MediaType.APPLICATION_JSON_VALUE);        ApiError body = new ApiError(401, \"Unauthorized\", authException.getMessage());        objectMapper.writeValue(response.getWriter(), body);    }}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0421\u0443\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u044b \u0443 \u043d\u0435\u0433\u043e \u0442\u0430\u043a\u0430\u044f \u0436\u0435 \u043a\u0430\u043a \u0438 \u0443 AccessDeniedHandler, \u043d\u043e \u043e\u043d \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u044f, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043d\u0435 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u0438\u0446\u0440\u043e\u0432\u0430\u043d, \u0430 \u0441\u0430\u043c\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u0430\u044f, \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0430\u0442\u0443\u0441\u044b \u043e\u0442\u0432\u0435\u0442\u043e\u0432. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f 401, \u0430 \u0432 \u043f\u0440\u043e\u0448\u043b\u043e\u043c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f 403.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u043a SecurityConfig:<\/p>\n<pre><code class=\"java\">@Configuration@EnableWebSecurity@RequiredArgsConstructorpublic class SecurityConfig {    private final UserDetailsServiceImpl userDetailsService;    private final PasswordEncoder passwordEncoder;    private final RestAuthenticationEntryPoint authenticationEntryPoint;    private final RestAccessDeniedHandler accessDeniedHandler;    @Bean    public AuthenticationProvider authenticationProvider(){        DaoAuthenticationProvider provider = new DaoAuthenticationProvider(userDetailsService);        provider.setPasswordEncoder(passwordEncoder);        return provider;    }    @Bean    public AuthenticationManager authenticationManager(AuthenticationConfiguration config){        return config.getAuthenticationManager();    }    @Bean    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{        http.authorizeHttpRequests(auth -&gt;                auth.requestMatchers(\"\/api\/auth\/**\").permitAll()                        .requestMatchers(\"\/api\/admin\/**\").hasRole(\"ADMIN\")                        .anyRequest().authenticated())                \/\/\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0435 \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0437\u0430\u0449\u0438\u0442\u044b \u043e\u0442 csrf \u0430\u0442\u0430\u043a\u0438                .csrf(csrf -&gt; csrf.disable())                \/\/ \u041b\u043e\u0433\u0438\u043d \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 SessionAuthController, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0438 \u043e\u0442\u0434\u0430\u0451\u0442 JSON'\u044b (\u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e\u0437\u0436\u0435)                .sessionManagement(session -&gt;                        session.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)                                .sessionFixation().migrateSession()                                .maximumSessions(1)                )                .authenticationProvider(authenticationProvider())                .exceptionHandling(ex -&gt;                        ex.authenticationEntryPoint(authenticationEntryPoint)                                .accessDeniedHandler(accessDeniedHandler));        return http.build();    }}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041d\u0430\u0447\u043d\u0451\u043c \u0440\u0430\u0437\u0431\u043e\u0440 \u043a\u043e\u0434\u0430 \u0441 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 <code>@EnableWebSecurity<\/code>, \u043e\u043d\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430 \u0434\u043b\u044f \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 HTTP-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432. \u0415\u0441\u043b\u0438 \u043a\u043e\u0440\u043e\u0442\u043a\u043e, \u0442\u043e \u043e\u043d\u0430 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438, \u0438 \u0431\u0435\u0437 \u043d\u0435\u0451 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c. \u041e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u043d\u0435 \u0432\u0438\u0436\u0443 \u0441\u043c\u044b\u0441\u043b\u0430 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d\u0438 \u0441\u0430\u043c\u044b\u0435 \u0431\u0430\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u043a\u0430\u0436\u0434\u044b\u0439 \u0431\u0438\u043d \u043f\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438:<\/p>\n<p>1) AuthenticationProvider &#8212; \u044d\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0438\u0437 Spring Security \u043a\u043e\u0442\u0440\u044b \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442: &#171;\u0412\u0435\u0440\u043d\u044b \u043b\u0438 \u043f\u0440\u0435\u0434\u044a\u044f\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0443\u0447\u0451\u0442\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435?&#187;. \u0423 \u044d\u0442\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u0435\u0441\u0442\u044c \u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434:<\/p>\n<pre><code class=\"java\">@Nullable Authentication authenticate(Authentication authentication) throws AuthenticationException;<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0412 \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u0432\u0451\u043b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c, \u0430 \u043c\u0435\u0442\u043e\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043b\u0438\u0431\u043e \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0451\u043d\u043d\u044b\u0439 <code>Authentication<\/code>, \u043b\u0438\u0431\u043e \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435  <code>BadCredentialsException<\/code> \u043f\u0440\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0438, \u0435\u0441\u043b\u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u043d\u0435 \u0432\u0435\u0440\u043d\u044b\u0439.<br \/><code>DaoAuthenticationProdiver<\/code> &#8212; \u044d\u0442\u043e \u043f\u043e \u0441\u0443\u0442\u0438 <strong><em>\u043e\u0434\u043d\u0430 \u0438\u0437 \u0433\u043e\u0442\u043e\u0432\u044b\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439 AuthenticatoinProvider<\/em><\/strong>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u0432 Spring Security. \u0412 \u0435\u0433\u043e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f userDetailsService \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 &#171;\u0433\u0434\u0435 \u0438\u0441\u043a\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f&#187;, \u0430 \u0437\u0430\u0442\u0435\u043c \u0447\u0435\u0440\u0435\u0437 \u0441\u0435\u0442\u0442\u0435\u0440 \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f passwordEncoder \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438 \u0431\u0443\u0434\u0435\u0442 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u043f\u0430\u0440\u043e\u043b\u0438. \u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u044d\u0442\u043e\u0442 <\/p>\n<p>2) AuthenticationManager &#8212; \u044d\u0442\u043e \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u0430\u044f \u0448\u0442\u0443\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u0430\u043c\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0432 AuthService.login(&#8230;) \u043a\u043e\u0433\u0434\u0430 \u043d\u0430\u0434\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043b\u043e\u0433\u0438\u043d+\u043f\u0430\u0440\u043e\u043b\u044c (\u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043d\u0438\u0436\u0435).  \u0412\u043d\u0443\u0442\u0440\u0438 \u0441\u0435\u0431\u044f <code>AuthenticationManager<\/code> \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u0442 \u0432\u0441\u0435 \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 <code>AuthenticationProvider<\/code> \u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 &#8212; \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439, <code>DaoAuthenticationProvider<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0442\u0443\u0442 \u0438 \u0441\u043e\u0437\u0434\u0430\u043b\u0438. <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043f\u0440\u043e \u0431\u0438\u043d SecurityFilterChain<br \/>\u041a\u0430\u043a \u043d\u0435 \u0442\u0440\u0443\u0434\u043d\u043e \u0434\u043e\u0433\u0430\u0434\u0430\u0442\u044c\u0441\u044f, \u044d\u0442\u043e\u0442 \u0431\u0438\u043d &#8212; \u044d\u0442\u043e \u0446\u0435\u043f\u043e\u0447\u043a\u0430 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u043a\u0430\u0436\u0434\u044b\u0439 HTTP \u0437\u0430\u043f\u0440\u043e\u0441. <br \/><code>HttpSecurity<\/code> &#8212; \u044d\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442-\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 Spring Security \u0441\u0430\u043c \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442 \u0432 \u043c\u0435\u0442\u043e\u0434. \u041e\u043d \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u0440\u0443\u043a\u0430\u043c\u0438, \u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0433\u043e\u0442\u043e\u0432\u044b\u043c \u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0435, \u0430 \u043d\u0430\u0448\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 &#8212; \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043d\u0430 \u043d\u0451\u043c \u0446\u0435\u043f\u043e\u0447\u043a\u0443 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432. \u0410 \u0443\u0436\u0435 \u0432 \u043a\u043e\u043d\u0446\u0435 \u0432\u044b\u0437\u0432\u0430\u0442\u044c <code>.builder()<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0431\u0435\u0440\u0451\u0442 \u0432\u0441\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0432 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 SecurityFilterChain.<\/p>\n<p><code>authorizeHttpRequests(auth -&gt; ...)<\/code> \u0434\u0430\u043d\u043d\u0430\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441 &#171;\u043a\u0430\u0438\u043a\u0435 \u043f\u0440\u0430\u0432\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043d\u0443\u0436\u043d\u044b \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e url&#187;. \u0412\u043d\u0443\u0442\u0440\u044c \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u043b\u044f\u043c\u0431\u0434\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 auth &#8212; \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0442\u043e\u0440 \u043f\u0440\u0430\u0432\u0438\u043b, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u043f\u043e \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437, \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0432\u043e\u0435 \u0441\u043e\u0432\u043f\u0430\u0432\u0448\u0435\u0435. <\/p>\n<p><code>.requestMatchers(\"\/api\/auth\/**\").permitAll()<\/code> &#8212; \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0447\u0442\u043e \u043f\u0443\u0442\u0438 \/api\/auth\/** (** &#8212; \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043b\u044e\u0431\u0443\u044e \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0435\u0439, \u0442\u0438\u043f\u0430 \u0442\u0430\u043a\u0438\u0445 \u043f\u0443\u0442\u0435\u0439 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043c\u043d\u043e\u0433\u043e) \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e, \u0438 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u043d\u0435 \u043d\u0443\u0436\u043d\u0430. <\/p>\n<p><code>.requestMatchers(\"\/api\/admin\/**\").hasRole(\"ADMIN\")<\/code> &#8212;  \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0447\u0442\u043e \u043f\u043e url \/api\/admin\/** \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0441 \u0440\u043e\u043b\u044c\u044e ADMIN \u0438 \u043d\u0438\u043a\u0442\u043e \u0431\u043e\u043b\u044c\u0448\u0435. \u041d\u043e \u0435\u0441\u0442\u044c \u0432\u0430\u0436\u043d\u044b\u0439 \u043d\u044e\u0430\u043d\u0441, \u043c\u0435\u0442\u043e\u0434 hasRole(&#171;ADMIN&#187;) \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c \u0438\u0449\u0435\u0442 <code>GrandedAuthirity<\/code> \u0441 \u0438\u043c\u0435\u043d\u0435\u043c &#171;ROLE_ADMIN&#187; &#8212; \u043f\u0440\u0435\u0444\u0438\u043a\u0441 &#171;ROLE_&#187; \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438.<\/p>\n<p><code>.anyRequest().authenticated()<\/code> &#8212; \u044d\u0442\u043e \u0437\u0430\u0433\u043b\u0443\u0448\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c <strong>\u0441\u0442\u0440\u043e\u0433\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439<\/strong>, \u044d\u0442\u0430 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e url \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043d\u0435 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u043b\u0438 \u0432\u044b\u0448\u0435(\u043f\u043e\u0434\u043e\u0439\u0434\u0451\u0442 \u043b\u044e\u0431\u0430\u044f \u0432\u0430\u043b\u0438\u0434\u043d\u0430\u044f \u0440\u043e\u043b\u044c). \u042f \u0441\u0435\u0440\u044c\u0451\u0437\u043d\u043e, \u044d\u0442\u043e \u0432\u0430\u0436\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u0434\u0430\u043d\u043d\u0430\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f \u0431\u044b\u043b\u0430 \u0441\u0430\u043c\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e, \u0435\u0441\u043b\u0438 \u0431\u044b \u043e\u043d\u0430 \u0441\u0442\u043e\u044f\u043b\u0430 \u043f\u0435\u0440\u0432\u043e\u0439, \u0442\u043e \u043e\u043d\u0430 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u043b\u0430 \u0431\u044b \u0437\u0430\u043f\u0440\u043e\u0441 <code>\/api\/auth\/register<\/code> (\u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443) \u0438 \u0442\u043e\u0433\u0434\u0430 \u0434\u043b\u044f \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438, \u0442\u044b \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u043b \u0431\u044b \u0431\u044b\u0442\u044c \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d, \u0430 \u044d\u0442\u043e \u043a\u0440\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043e\u0448\u0438\u0431\u043a\u0430!<\/p>\n<p><code>.sessionManagment(session -&gt; ...)<\/code> &#8212; \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442 \u043a\u0430 \u043a\u0438\u043c\u0435\u043d\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 HTTP-\u0441\u0435\u0441\u0441\u0438\u0438 \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p><code>.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)<\/code> &#8212; \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0441\u0435\u0441\u0441\u0438\u044e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u043e\u043d\u0430 \u0440\u0435\u0430\u043b\u044c\u043d\u043e \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u0430\u0441\u044c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u043f\u043e\u0437\u0436\u0435 \u0432 <code>SecurityContext<\/code> \u043f\u043e\u043b\u043e\u0436\u0438\u043c <code>SecurityContextHolder.createEmptyContext();<\/code>.<br \/>IF_REQUIRED \u043d\u0435 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0435\u0441\u0442\u044c \u0435\u0449\u0451 3: ALWAYS &#8212; \u0441\u0435\u0441\u0441\u0438\u044f \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u0432\u0441\u0435\u0433\u0434\u0430 (\u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u043d\u0435 \u043d\u0443\u0436\u043d\u0430), NEVER &#8212; Spring \u0441\u0430\u043c \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u0442, \u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0443\u0435\u0442 \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u0441\u0435\u0441\u0441\u0438\u044e \u0435\u0441\u043b\u0438 \u043e\u043d\u0430 \u0435\u0441\u0442\u044c, STATELESS &#8212; \u0441\u0435\u0441\u0441\u0438\u044f \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432\u043e\u043e\u0431\u0449\u0435 (\u0434\u0430\u043b\u044c\u0448\u0435 \u043e\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f JWT)<\/p>\n<p><code>.sessionFixation().migrateSession()<\/code> &#8212; \u044d\u0442\u043e \u0437\u0430\u0449\u0438\u0442\u0430 \u043e\u0442 \u0430\u0442\u0430\u043a\u0438 <strong>\u0444\u0438\u043a\u0441\u0430\u0446\u0438\u0438 \u0441\u0435\u0441\u0441\u0438\u0438<\/strong>, \u0442\u043e \u0435\u0441\u0442\u044c \u0445\u0430\u0446\u043a\u0435\u0440 \u043c\u043e\u0433 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0434\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u043d\u0443\u0436\u043d\u044b\u0439 \u0445\u0430\u0446\u043a\u0435\u0440\u0443 id \u0441\u0435\u0441\u0441\u0438\u0438 (\u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443 \u0447\u0435\u0440\u0435\u0437 RequestParam), \u0438 \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043d\u0438 \u043e \u0447\u0451\u043c \u043d\u0435 \u043f\u043e\u0434\u043e\u0437\u0440\u0435\u0432\u0430\u044e\u0449\u0438\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0437\u0430\u043b\u043e\u0433\u0438\u043d\u0438\u0442\u0441\u044f, \u0445\u0430\u0446\u043a\u0435\u0440 \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c id \u0441\u0435\u0441\u0441\u0438\u0438 \u043f\u043e\u043b\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0447\u0442\u043e\u0431\u044b \u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043e\u0442 \u0438\u043c\u0435\u043d\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. <br \/>\u0412\u0435\u0440\u043d\u0451\u043c\u0441\u044f \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443 \u0441 \u0433\u0430\u0440\u0434\u0435\u0440\u043e\u0431\u043e\u043c, \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u0432\u044b \u043f\u0440\u0438\u0434\u0451\u0442\u0435 \u0432 \u0442\u0435\u0430\u0442\u0440 \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 \u0441\u0432\u043e\u0439 \u043d\u043e\u043c\u0435\u0440\u043e\u043a, \u0443 \u0445\u0430\u0446\u043a\u0435\u0440\u0430 \u0431\u0443\u0434\u0435\u0442 \u0437\u043d\u0430\u0442\u044c \u043a\u0430\u043a\u043e\u0439 \u0443 \u0432\u0430\u0441 \u043d\u043e\u043c\u0435\u0440\u043e\u043a, \u0438 \u043e\u043d \u0441\u043c\u043e\u0436\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u0432 \u0442\u043e\u043c \u0436\u0435 \u0442\u0435\u0430\u0442\u0440\u0435 \u0432\u0441\u0451 \u0447\u0442\u043e \u0443\u0433\u043e\u0434\u043d\u043e \u043e\u0442 \u0432\u0430\u0448\u0435\u0433\u043e \u0438\u043c\u0435\u043d\u0438. <br \/>\u0422\u0430\u043a \u0432\u043e\u0442 \u0434\u0430\u043d\u043d\u0430\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f \u043a\u0430\u043a \u0440\u0430\u0437 \u0437\u0430\u0449\u0438\u0449\u0430\u0435\u0442 \u043e\u0442 \u0442\u0430\u043a\u0438\u0445 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0439 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>.migrateSession()<\/code> \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0433\u043e\u0432\u043e\u0440\u0438\u0442, \u0447\u0442\u043e \u043f\u0440\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u043c \u043b\u043e\u0433\u0438\u043d\u0435 \u0441\u043e\u0437\u0430\u0442\u044c <strong>\u043d\u043e\u0432\u0443\u044e<\/strong> \u0441\u0435\u0441\u0441\u0438\u044e \u0441 \u043d\u043e\u0432\u044b\u043c id, \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u044f \u0442\u0443\u0434\u0430 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0441\u0442\u0430\u0440\u043e\u0439 \u0441\u0435\u0441\u0441\u0438\u0438, \u0430 \u0441\u0442\u0430\u0440\u0443\u044e \u0441\u0435\u0441\u0441\u0438\u044e \u043d\u0443\u0436\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c.<\/p>\n<p><code>.maximumSessions(1)<\/code> &#8212; \u044d\u0442\u0430 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0433\u043e\u0432\u043e\u0440\u0438\u0442, \u0447\u0442\u043e \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0435\u0441\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0430\u0441\u0435\u0441\u0441\u0438\u044f, \u0438 \u0435\u0441\u043b\u0438 \u043e\u043d \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u0442 \u0437\u0430\u043b\u043e\u0433\u0438\u043d\u0438\u0442\u044c\u0441\u044f \u0441 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\/\u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430, \u0442\u043e \u0441\u0442\u0430\u0440\u0430\u044f \u0441\u0435\u0441\u0441\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u0430. <\/p>\n<p><code>.authenticationProvider(authenticationProvider())<\/code> &#8212; \u0442\u0443\u0442 \u043c\u044b \u0432\u043d\u0435\u0434\u0440\u044f\u0435\u043c \u0431\u0438\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b\u043b \u043e\u043f\u0438\u0441\u0430\u043d \u0432\u044b\u0448\u0435. \u042d\u0442\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u0430\u0436\u043d\u043e, \u0432\u0435\u0434\u044c \u0431\u0435\u0437 \u044d\u0442\u043e\u0439 \u0441\u0442\u0440\u043e\u0447\u043a\u0438  <code>AuthenticationManager<\/code>, \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0439 \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0432 <code>SessionAuthController.login(...)<\/code>, \u043d\u0435 \u0437\u043d\u0430\u043b \u0431\u044b \u043a\u0430\u043a\u043e\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0438\u0441\u043f\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c. <\/p>\n<p><code>.authenticationEntryPoint(authenticationEntryPoint)<\/code> &#8212; \u0432\u043d\u0435\u0434\u0440\u044f\u0435\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0442\u0430\u043a\u0436\u0435 \u0431\u044b\u043b \u043e\u043f\u0438\u0441\u0430\u043d \u0432\u044b\u0448\u0435. \u041e\u043d \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043d\u0435 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d, \u0438 \u0432\u043c\u0435\u0441\u0442\u043e \u0440\u0435\u0434\u0438\u0440\u0435\u043a\u0442\u0430 \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u043b\u043e\u0433\u0438\u043d\u0430, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f JSON \u0441 401 \u043a\u043e\u0434\u043e\u043c.<\/p>\n<p><code>.accessDeniedHandler(accessDeniedHandler)<\/code> &#8212; \u0432\u043d\u0435\u0434\u0440\u044f\u0435\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u044f\u0442\u044c \u0442\u0430\u043a\u0438 \u0442\u0430\u043a\u0436\u0435 \u0431\u044b\u043b \u043e\u043f\u0438\u0441\u0430\u043d \u0432\u044b\u0448\u0435. \u041e\u043d \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0430\u0443\u0442\u043d\u0435\u0442\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d, \u043d\u043e \u043f\u0440\u0430\u0432 \u0435\u043c\u0443 \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442, \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0437\u0430\u0435\u0442\u0441\u044f JSON \u0441  403 \u043a\u043e\u0434\u043e\u043c.<\/p>\n<p><code>return http.builder();<\/code> &#8212; \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0431\u0435\u0440\u0451\u0442 \u0432\u0441\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0432\u043e \u0432\u0441\u0435\u0445 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u0430\u0445, \u0438 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 \u0438\u0437 \u043d\u0438\u0445 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 <code>SecurityFilterChain<\/code> &#8212; \u0438\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u043e\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>FilterChainProxy<\/code> \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0432\u0445\u043e\u0434\u044f\u0449\u0435\u0433\u043e HTTP-\u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<h2>SessionAuthController &#8212; \u043b\u043e\u0433\u0438\u043d \u0438 \u043b\u043e\u0433\u0430\u0443\u0442 \u0447\u0435\u0440\u0435\u0437 JSON<\/h2>\n<p>\u041f\u0435\u0440\u0435\u0434 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c \u0441\u0430\u043c\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u0430\u0440\u0443 \u043d\u043e\u0432\u044b\u0445 record&#8217;\u043e\u0432 \u043e\u0434\u0438\u043d \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c \u0432 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440, \u0430 \u0432\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c\u0441\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u043f\u043e\u0441\u043b\u0435 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438.<\/p>\n<pre><code class=\"java\">public record LoginRequest(        String username,        String password) {}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<pre><code class=\"java\">public record LoginResponse(        String username) {}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044f REST-\u0437\u0430\u043c\u0435\u043d\u0430 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u043c\u0443 <code>formLogin<\/code>. \u041a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 JSON, \u0441\u0430\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 <code>AuthenticationManager<\/code> \u0438 \u0441\u0430\u043c \u043a\u043b\u0430\u0434\u0451\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432 \u0441\u0435\u0441\u0441\u0438\u044e \u2014 \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a \u0434\u0435\u043b\u0430\u044e\u0442 REST API \u043f\u043e\u0432\u0435\u0440\u0445 \u0441\u0435\u0441\u0441\u0438\u043e\u043d\u043d\u043e\u0439 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438. <\/p>\n<pre><code class=\"java\">@RestController@RequestMapping(\"\/api\/auth\")@RequiredArgsConstructorpublic class SessionAuthController {    \/\/\u0413\u043b\u0430\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 Spring Security, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c    private final AuthenticationManager authenticationManager;    \/\/\u041e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435 \u0432 HTTP-\u0441\u0435\u0441\u0441\u0438\u0438    private final SecurityContextRepository securityContextRepository = new HttpSessionSecurityContextRepository();    @PostMapping(\"\/login\")    public ResponseEntity&lt;LoginResponse&gt; login(            @RequestBody LoginRequest request,            HttpServletRequest httpRequest,            HttpServletResponse httpResponse){        \/\/\u0421\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0443\u0436\u0435 \u043b\u0435\u0436\u0430\u0442 \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c, \u043d\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0435\u0449\u0451 \u043d\u0435 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d        Authentication authRequest = new UsernamePasswordAuthenticationToken(request.username(), request.password());        \/\/\u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0431\u0435\u0440\u0451\u0442 \u043d\u0430\u0448 authRequest \u0438 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0438\u0441\u043a\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f         \/\/\u0432 UserDetailsServiceImpl (\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043f\u0438\u0441\u0430\u043b\u0438 \u0440\u0430\u043d\u0435\u0435)        Authentication authResult = authenticationManager.authenticate(authRequest);        \/\/\u0421\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u043f\u0443\u0441\u0442\u043e\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435        SecurityContext context =  SecurityContextHolder.createEmptyContext();        \/\/\u0417\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043f\u043e\u043b\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442        context.setAuthentication(authResult);        SecurityContextHolder.setContext(context);        \/\/ \u042f\u0432\u043d\u043e\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0432 \u0441\u0435\u0441\u0441\u0438\u044e        securityContextRepository.saveContext(context, httpRequest, httpResponse);        \/\/\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u0443 JSON \u0441 \u0435\u0433\u043e username        return ResponseEntity.ok(new LoginResponse(authResult.getName()));    }    @PostMapping(\"\/logout\")    public ResponseEntity&lt;Void&gt; logout(HttpServletRequest request){        \/\/\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u0435\u0441\u0441\u0438\u044e \u0438\u0437 \u0437\u0430\u043f\u0440\u043e\u0441\u0430        HttpSession session = request.getSession();                \/\/\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u043f\u0443\u0441\u0442\u043e\u0442\u0443        if(session != null){            \/\/\u0415\u0441\u043b\u0438 \u0441\u0435\u0441\u0441\u0438\u044f \u043d\u0435 \u043f\u0443\u0441\u0442\u0430, \u0442\u043e \u0438\u043d\u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c (\u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0443\u0434\u0430\u043b\u044f\u0435\u043c)            session.invalidate();        }        \/\/\u041e\u0447\u0438\u0449\u0430\u0435\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435 \u0438\u0437 \u043f\u0430\u043c\u044f\u0442\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430        SecurityContextHolder.clearContext();        return ResponseEntity.noContent().build();    }}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0447\u0438\u0442\u0430\u043b\u0438 \u043a\u043e\u0434 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0430, \u0442\u043e \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0437\u0430\u043c\u0435\u0442\u0438\u043b\u0438, \u0447\u0442\u043e <code>SecurityContextHolder.setContext(context);<\/code> \u044f \u043e\u0441\u0442\u0430\u0432\u0438\u043b \u0431\u0435\u0437 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043b\u0451\u0433\u043a\u043e\u0433\u043e \u0438 \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u044f \u043d\u0435\u0442, \u0442\u0430\u043a \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0443\u0433\u043b\u0443\u0431\u0438\u0442\u044c\u0441\u044f \u043f\u043e \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435.<\/p>\n<p>Spring \u0445\u0440\u0430\u043d\u0438\u0442 Security Context \u0432 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435 &#8212; <code>SecurityContextHolder<\/code>. \u0418 \u0443\u0441\u043b\u043e\u0432\u043d\u043e \u0432 \u043b\u044e\u0431\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f (\u0442\u0430\u043a \u043d\u0435 \u043d\u0430\u0434\u043e) \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c <code>SecurityContextHolder.getContext()<\/code> (\u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c .getAuthenticated() ) \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u043c\u044b \u043d\u0430\u0445\u043e\u0434\u0438\u043c\u0441\u044f \u0432 \u0434\u0440\u0443\u0433\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435. \u041d\u043e \u044d\u0442\u043e\u0433\u043e \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d, \u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0430 \u043f\u0440\u0438 \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0430\u0442\u0441\u044f, context \u0438 authRequesr \u0438\u0441\u0447\u0435\u0437\u043d\u0443\u0442&#8230; \u041f\u043e \u044d\u0442\u043e\u043c\u0443 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0434\u0435\u043b\u043e\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f saveContext \u043d\u0430 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438, \u0447\u0442\u043e\u0431\u044b Spring \u0432\u0437\u044f\u043b SecurityContext \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u043b \u0435\u0433\u043e \u0432 HTTP-\u0441\u0435\u0441\u0441\u0438\u044e \u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435 \u0431\u044b\u043b\u0430 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430, \u043d\u043e \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u043b\u0430\u0441\u044c \u043c\u0435\u0436\u0434\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c\u0438. <\/p>\n<figure class=\"\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/fad\/cb5\/ce0\/fadcb5ce076ceaf412b02503ce5a43db.png\" alt=\"\u041f\u0430\u043f\u043e\u0447\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0441 \u043a\u043b\u0430\u0441\u0441\u0430\u043c\u0438\" title=\"\u041f\u0430\u043f\u043e\u0447\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0441 \u043a\u043b\u0430\u0441\u0441\u0430\u043c\u0438\" width=\"400\" height=\"956\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/fad\/cb5\/ce0\/fadcb5ce076ceaf412b02503ce5a43db.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/fad\/cb5\/ce0\/fadcb5ce076ceaf412b02503ce5a43db.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u041f\u0430\u043f\u043e\u0447\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0441 \u043a\u043b\u0430\u0441\u0441\u0430\u043c\u0438<\/figcaption><\/div>\n<\/figure>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432 \u043f\u0430\u043a\u0435\u0442\u0435 config \u043d\u043e\u0432\u044b\u0439 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 DataInitializer \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"java\">@Configuration@RequiredArgsConstructorpublic class DataInitializer {        private final UserRepository userRepository;    private final PasswordEncoder passwordEncoder;        @Bean    public CommandLineRunner initTestUser(){        return args -&gt; {            if(userRepository.existsByUsername(\"test\")){                return;            }            User user = new User();            user.setUsername(\"test\");            user.setPassword(passwordEncoder.encode(\"test123\"));            user.setRoles(Set.of(Role.USER));            System.out.println(\"\\n\u0422\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0441\u043e\u0437\u0434\u0430\u043d\\n\");        };    }}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043c\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043b\u0438 \u0434\u043e\u043a\u0435 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u0434\u043e\u043b\u0436\u043d\u043e \u0432\u044b\u0432\u0435\u0441\u0442\u0438 &#171;\u0422\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0441\u043e\u0437\u0434\u0430\u043d&#187; <br \/>\u0414\u0430\u043b\u0435\u0435 \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u0439\u0442\u0438 \u0432 Postman \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c POST-\u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u043e url: <a href=\"http:\/\/localhost:8080\/api\/auth\/login\" rel=\"noopener noreferrer nofollow\"><code>http:\/\/localhost:8080\/api\/auth\/login<\/code><\/a><\/p>\n<p>*\u0412\u0430\u0436\u043d\u043e\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435, \u0432 application.properties \u043f\u043e\u0440\u0442 \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043d\u0430 5433 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u043e\u0434\u0438\u043d \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u0432\u044b\u0432\u043e\u0434\u0438\u043b\u0438\u0441\u044c \u0432\u0441\u0435 sql-\u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u0430 \u0432 docker-compose.yml \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u043f\u043e\u0440\u0442 \u043f\u043e\u0441\u0442\u0433\u0440\u0435\u0441\u0430 \u043f\u043e\u043c\u0435\u043d\u044f\u043b \u043d\u0430 5433, \u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u044d\u0442\u043e \u0441\u043b\u0443\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"yaml\">services:  db:    image: postgres:latest    environment:      POSTGRES_PASSWORD: postgres      POSTGRES_USER: postgres      POSTGRES_DB: security    ports:      #\u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u043f\u043e\u0440\u0442\u0430      - \"5433:5432\"<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<pre><code>spring.application.name=SecurityCourseserver.port=8080spring.datasource.driver-class-name=org.postgresql.Driverspring.datasource.password=${POSTGRES_PASSWORD:postgres}spring.datasource.username=${POSTGRES_USER:postgres}#\u0418\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435spring.datasource.url=jdbc:postgresql:\/\/localhost:5433\/${POSTGRES_DB:security}spring.jpa.hibernate.ddl-auto=create-dropspring.jpa.show-sql=true<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 POST \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 username \u0438 password (\u043d\u0435 \u0445\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439), \u043d\u0430\u043c \u0432\u0435\u0440\u043d\u0451\u0442\u0441\u044f username \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u043a\u043e\u0434 200<\/p>\n<figure class=\"full-width \"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/0c5\/19d\/86a\/0c519d86a477f143e28ec154ee52fd11.png\" alt=\"\u041e\u0442\u0432\u0435\u0442 \u0432 \u043f\u043e\u0441\u0442\u043c\u0430\u043d\u0435\" title=\"\u041e\u0442\u0432\u0435\u0442 \u0432 \u043f\u043e\u0441\u0442\u043c\u0430\u043d\u0435\" width=\"769\" height=\"606\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/0c5\/19d\/86a\/0c519d86a477f143e28ec154ee52fd11.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/0c5\/19d\/86a\/0c519d86a477f143e28ec154ee52fd11.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u041e\u0442\u0432\u0435\u0442 \u0432 \u043f\u043e\u0441\u0442\u043c\u0430\u043d\u0435<\/figcaption><\/div>\n<\/figure>\n<p>\u042d\u0442\u043e \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0447\u0442\u043e \u0432\u0441\u0451 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e! <br \/>\u0414\u0430\u043b\u0435\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0437\u0430\u043b\u043e\u0433\u0438\u043d\u0435\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c POST \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u043e url: <a href=\"http:\/\/localhost:8080\/api\/auth\/logout\" rel=\"noopener noreferrer nofollow\">http:\/\/localhost:8080\/api\/auth\/logout<\/a> \u0438 \u0432 \u043e\u0442\u0432\u0435\u0442\u0435 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043d\u0438\u0447\u0435\u0433\u043e \u043a\u0440\u043e\u043c\u0435 \u043a\u043e\u0434\u0430 204<\/p>\n<figure class=\"full-width \"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/dd9\/124\/f50\/dd9124f50d9352955f4c574988c6a6e2.png\" alt=\"\u0412\u044b\u0445\u043e\u0434 \u0438\u0437 \u043b\u043e\u0433\u0438\u043d\u0430\" title=\"\u0412\u044b\u0445\u043e\u0434 \u0438\u0437 \u043b\u043e\u0433\u0438\u043d\u0430\" width=\"775\" height=\"584\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/dd9\/124\/f50\/dd9124f50d9352955f4c574988c6a6e2.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/dd9\/124\/f50\/dd9124f50d9352955f4c574988c6a6e2.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>\u0412\u044b\u0445\u043e\u0434 \u0438\u0437 \u043b\u043e\u0433\u0438\u043d\u0430<\/figcaption><\/div>\n<\/figure>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e. \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0432\u0441\u0435\u043c \u043a\u0442\u043e \u0434\u043e\u0447\u0438\u0442\u0430\u043b \u0438 \u0443\u0437\u043d\u0430\u043b \u0434\u043b\u044f \u0441\u0435\u0431\u044f \u0447\u0442\u043e-\u0442\u043e \u043d\u043e\u0432\u043e\u0435 \u0438 \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435 \ud83d\ude42 \u041a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u043e \u043e\u0442\u043a\u0440\u044b\u0442 \u043a \u043a\u0440\u0438\u0442\u0438\u043a\u0435 \u0438 \u0431\u0443\u0434\u0443 \u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0434 \u0435\u0441\u043b\u0438 \u043e\u043d\u0430 \u0431\u0443\u0434\u0435\u0442 \ud83d\ude42<\/p>\n<\/div>\n<p>\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/1056696\/\">https:\/\/habr.com\/ru\/articles\/1056696\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0414\u043e\u0431\u0440\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u0443\u0442\u043e\u043a! \u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0447\u0435\u0440\u0435\u0437 REST. \u041d\u043e \u043f\u0435\u0440\u0435\u0434 \u0441\u0430\u043c\u043e\u0439 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0435\u0439, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0437\u043d\u0430\u0442\u044c \u043e\u0431 \u043e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u043e\u0439 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u0439 &#8212; PasswordEncoder.PasswordEncoderPasswordEncoder &#8212; \u044d\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 Spring Security, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u043d\u0430 \u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u043f\u0430\u0440\u043e\u043b\u044c \u0432 \u043d\u0435\u0447\u0438\u0442\u0430\u0435\u043c\u0443\u044e \u043a\u0430\u0448\u0443, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u043e, \u0434\u0430\u0436\u0435 \u043f\u0440\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0438, \u0435\u0441\u043b\u0438 \u043a\u0442\u043e-\u0442\u043e \u0441\u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043b\u0435\u0437\u0442\u044c \u0432 \u0431\u0434.\u0410 \u0437\u0430\u0447\u0435\u043c \u043e\u043d \u043d\u0443\u0436\u0435\u043d? \u0414\u043b\u044f \u0445\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u0440\u043e\u043b\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0436\u0435 \u0438 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c\u0441\u044f \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0435\u0441\u043b\u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f \u0432 \u043e\u0431\u044b\u0447\u043d\u043e\u043c \u0432\u0438\u0434\u0435 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 &#171;password123&#187;, \u0442\u043e \u0435\u0441\u043b\u0438 \u0445\u0430\u0446\u043a\u0435\u0440\u044b \u0441\u043c\u043e\u0433\u0443\u0442 \u0437\u0430\u043b\u0435\u0437\u0442\u044c \u0432 \u0431\u0430\u0437\u0443 \u0438 \u0443\u043a\u0440\u0430\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435, \u0442\u043e \u043e\u043d\u0438 \u0441\u043c\u043e\u0433\u0443\u0442 \u0437\u0430\u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u044b \u0432\u043b\u0430\u0434\u0435\u043b\u044c\u0446\u0435\u0432 \u044d\u0442\u0438\u0445 \u0436\u0435 \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u043e\u0432, \u0430 \u0435\u0441\u043b\u0438 \u0445\u0430\u0446\u043a\u0435\u0440\u044b \u0441\u043c\u043e\u0433\u0443\u0442 \u0437\u0430\u043b\u0435\u0437\u0442\u044c \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u0430\u0440\u043e\u043b\u0438 \u0431\u0443\u0434\u0443\u0442 \u0437\u0430\u0445\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u044b, \u0442\u043e \u043e\u043d\u0438 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u0430\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439. \u041c\u044b \u043f\u043e\u043d\u044f\u043b\u0438 \u0447\u0442\u043e \u043d\u0430\u0434\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0445\u044d\u0448, \u043d\u043e \u0447\u0442\u043e \u044d\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0438 \u043a\u0430\u043a \u043e\u043d \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f?&#8230;\u0425\u044d\u0448 &#8212; \u044d\u0442\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u043d\u0430\u0437\u0430\u0434. (\u041f\u043e \u0443\u043c\u043d\u043e\u043c\u0443 \u044d\u0442\u043e \u043a\u0440\u0438\u043f\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0445\u044d\u0448, \u043d\u043e \u0431\u0443\u0434\u0435\u043c \u044d\u0442\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e \u0445\u044d\u0448)\u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0445\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u0440\u043e\u043b\u044f \u0447\u0435\u0440\u0435\u0437 bcrypt*\u0412\u0430\u0436\u043d\u043e\u0435 \u0443\u0442\u043e\u0447\u043d\u0435\u043d\u0438\u0435, \u0447\u0442\u043e cost &#8212; \u044d\u0442\u043e \u0447\u0438\u0441\u043b\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0437\u0430\u0434\u0430\u0451\u0442 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438. \u041e\u0431\u044b\u0447\u043d\u043e \u044d\u0442\u043e \u0447\u0438\u0441\u043b\u043e \u043e\u0442 4 \u0434\u043e 31, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u041f\u0440\u0438\u043c\u0435\u0440 \u0431\u0443\u0434\u0435\u0442 \u043d\u0438\u0436\u0435). \u0410 \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0440\u0430\u0443\u043d\u0434 &#8212; \u044d\u0442\u043e \u043e\u0434\u0438\u043d \u043f\u0440\u043e\u0445\u043e\u0434 \u0447\u0435\u0440\u0435\u0437 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043a \u0434\u0430\u043d\u043d\u044b\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437 \u043f\u043e\u0434\u0440\u044f\u0434.\u041a\u0430\u043a \u0432\u0441\u0451 \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442?\u0421\u043e\u043b\u044c \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e (\u0432 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435 \u043d\u0435\u0442 \u043d\u0438\u0447\u0435\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0433\u043e, \u043d\u043e \u043d\u0435 \u043e\u0431 \u044d\u0442\u043e\u043c) \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438\u0437 16 \u0431\u0430\u0439\u0442, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u0430 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u0430\u0440\u043e\u043b\u044f. \u042d\u0442\u043e \u0437\u0430\u0449\u0438\u0449\u0430\u0435\u0442 \u043e\u0442 rainbow-\u0442\u0430\u0431\u043b\u0438\u0446 (\u0434\u0430\u0436\u0435 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0435 \u043f\u0430\u0440\u043e\u043b\u0438 \u0434\u0430\u044e\u0442 \u0440\u0430\u0437\u043d\u044b\u0439 \u0445\u044d\u0448)\u041f\u0430\u0440\u043e\u043b\u044c \u0438 \u0441\u043e\u043b\u044c \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u044e\u0442\u0441\u044f \u0438 \u043f\u0440\u043e\u0445\u043e\u0434\u044f\u0442 \u0447\u0435\u0440\u0435\u0437 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c Blowfish \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0438\u0432\u0430\u0435\u0442 \u0441\u043e\u043b\u044c \u0438 \u043f\u0430\u0440\u043e\u043b\u044c. Cost-\u0444\u0430\u043a\u0442\u043e\u0440 \u0437\u0430\u0434\u0430\u0451\u0442 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0443\u043d\u0434\u043e\u0432 \u043a\u0430\u043a 2^cost. \u0427\u0435\u043c \u0432\u044b\u0448\u0435 cost, \u0442\u0435\u043c \u0434\u043e\u043b\u044c\u0448\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 &#8212; \u044d\u0442\u043e \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0437\u0430\u043c\u0435\u0434\u043b\u044f\u0435\u0442 brute-force, \u043a\u043e\u0433\u0434\u0430 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u043b\u043e\u0433\u0438\u043d\u0430 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430.\u041f\u043e\u043b\u0443\u0447\u0438\u0432\u0448\u0430\u044f\u0441\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0447\u0430\u0441\u0442\u0435\u0439: \u0432\u0435\u0440\u0441\u0438\u044e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430, cost, \u0441\u043e\u043b\u044c \u0438 \u0445\u044d\u0448. \u042d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043a\u0430\u043a-\u0442\u043e \u0442\u0430\u043a:$2b$10$N9qo8uLOickgx2ZMRZoMy&#8230;. \u041f\u0440\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0435 \u043f\u0430\u0440\u043e\u043b\u044f bcrypt \u0434\u043e\u0441\u0442\u0430\u0451\u0442 \u0441\u043e\u043b\u044c \u0438 cost \u0438\u0437 \u043f\u0430\u0440\u043e\u043b\u044f \u043f\u0440\u044f\u043c\u043e \u0438\u0437 \u044d\u0442\u043e \u0441\u0442\u0440\u043e\u043a\u0438, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442.\u041f\u043e\u0447\u0435\u043c\u0443 \u043b\u0443\u0447\u0448\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e BCrypt, \u0430 \u043d\u0435 SHA-256:SHA-256 &#8212; \u044d\u0442\u043e \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u0445\u044d\u0448 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0441 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c\u044e, \u0438 \u043e\u043d\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0430 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0446\u0435\u043b\u043e\u0441\u0442\u043d\u043e\u0441\u0442\u0438 \u0444\u0430\u0439\u043b\u043e\u0432. \u041d\u043e \u0434\u043b\u044f \u043f\u0430\u0440\u043e\u043b\u0435\u0439 \u044d\u0442\u043e \u043c\u0438\u043d\u0443\u0441, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432\u0438\u0434\u0435\u043e\u043a\u0430\u0440\u0442\u0430 \u043c\u043e\u0436\u0435\u0442 \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0442\u044c \u0430\u0444\u0438\u0433\u0435\u0442\u044c \u043a\u0430\u043a \u043c\u043d\u043e\u0433\u043e SHA-256 \u0445\u044d\u0448\u0435\u0439 \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443, \u0447\u0435\u0440\u0435\u0437 brute-force.BCrypt &#8212; \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0439 \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u043e\u0434\u043c\u0435\u0448\u0438\u0432\u0430\u0435\u0442 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u0443\u044e \u0441\u043e\u043b\u044c, \u043e\u043d \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0434\u043e\u043b\u0433\u0438\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0445\u044d\u0448, \u0442\u0430\u043a \u0447\u0442\u043e \u0431\u0440\u0443\u0442\u0444\u043e\u0440\u0441 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0442\u044f\u0436\u0451\u043b\u044b\u043c. \u0414\u0430\u0436\u0435 \u0441 \u043c\u043e\u0449\u043d\u044b\u043c \u0436\u0435\u043b\u0435\u0437\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043b\u0438\u0448\u044c \u0442\u044b\u0441\u044f\u0447\u0438 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443, \u0430 \u043d\u0435 \u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434\u044b. \u0412 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441\u043e\u043b\u044c \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0433\u043e \u043f\u0430\u0440\u043e\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0445\u044d\u0448\u0438 \u0434\u043b\u044f \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u043f\u0430\u0440\u043e\u043b\u0435\u0439, \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u0445\u0430\u0446\u043a\u0435\u0440 \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e \u0442\u0430\u043a \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0445\u044d\u0448\u0438 \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439 \u0438 \u043f\u043e\u043d\u044f\u0442\u044c \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0442 \u043b\u0438 \u043f\u0430\u0440\u043e\u043b\u0438 \u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439, \u0438 \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u043f\u0440\u043e\u0441\u0447\u0438\u0442\u0430\u043d\u043d\u044b\u0435 rainbow tables \u0434\u043b\u044f \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u0432\u0437\u043b\u043e\u043c\u0430.\u041a\u0430\u043a \u0442\u0435\u043f\u0435\u0440\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c PasswordEncoder? \u0410 \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u044d\u0442\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e!@Configurationpublic class PasswordEncoderConfig{  @Bean  public PasswordEncoder passwordEncoder(){    return new BCryptPasswordEncoder(12);  }  }\u0414\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0444\u0438\u0433 \u043b\u0443\u0447\u0448\u0435 \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e, \u0434\u043b\u044f \u0438\u0437\u0431\u0435\u0436\u0430\u043d\u0438\u044f \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439. \u0414\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0431\u0438\u043d \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430 \u043f\u0430\u0440\u043e\u043b\u0435\u0439 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 BCrypt \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 PasswordEncoder \u0441 cost = 12, \u0442\u043e \u0435\u0441\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0443\u043d\u0434\u043e\u0432 \u0445\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = 2^12. \u0427\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0435 cost, \u0442\u0435\u043c \u043c\u0435\u043b\u0435\u043d\u043d\u0435\u0435 \u0431\u0440\u0443\u0442\u0444\u043e\u0440\u0441, \u043d\u043e \u0442\u0435\u043c \u043d\u0435 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435 \u0441\u0430\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438. \u041f\u043e \u044d\u0442\u043e\u043c\u0443 10-12  \u043c\u043e\u0436\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u043c \u0431\u0430\u043b\u0430\u043d\u0441\u043e\u043c.Session + Cookie \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f\u0415\u0441\u043b\u0438 \u0432\u044b \u0437\u043d\u0430\u043a\u043e\u043c\u044b \u0441 \u0442\u0430\u043a\u0438\u043c\u0438 \u043f\u043e\u043d\u044f\u0442\u0438\u044f\u043c\u0438 \u043a\u0430\u043a \u0441\u0435\u0441\u0441\u0438\u044f \u0438 \u043a\u0443\u043a\u0438, \u0442\u043e \u0441\u043c\u0435\u043b\u043e \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043f\u0443\u043d\u043a\u0442 \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u044f \u0447\u0442\u043e \u044d\u0442\u043e \u0442\u0430\u043a\u043e\u0435.\u0421\u0435\u0441\u0441\u0438\u044f (\u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430) &#8212; \u044d\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 id \u0441\u0435\u0441\u0441\u0438\u0438, \u0430 \u0441\u0430\u043c \u0441\u0435\u0440\u0432\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u044d\u0442\u043e\u0442 id \u043a\u0430\u043a \u043a\u043b\u044e\u0447 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438. \u0415\u0441\u043b\u0438 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u044e \u0441 \u0433\u0430\u0440\u0434\u0435\u0440\u043e\u0431\u043e\u043c \u0432 \u0442\u0435\u0430\u0442\u0440\u0435. \u0412\u044b \u043f\u0440\u0438\u0448\u043b\u0438 \u0432 \u0442\u0435\u0430\u0442\u0440, \u0441\u043d\u044f\u043b\u0438 \u043a\u0443\u0440\u0442\u043a\u0443, \u0441\u0434\u0430\u043b\u0438 \u0432 \u0433\u0430\u0440\u0434\u0435\u0440\u043e\u0431 \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0441\u0432\u043e\u0439 \u043d\u043e\u043c\u0435\u0440\u043e\u043a (\u043e\u043d \u0438 \u0431\u0443\u0434\u0435\u0442 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u043c id \u0441\u0435\u0441\u0441\u0438\u0438), \u043f\u043e\u043a\u0430 \u043a\u0443\u0440\u0442\u043a\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0433\u0430\u0440\u0434\u0435\u0440\u043e\u0431\u0435 &#8212; \u044d\u0442\u043e \u0438 \u0435\u0441\u0442\u044c \u0441\u0435\u0441\u0441\u0438\u044f.Cookie (\u043f\u0435\u0447\u0435\u043d\u044c\u043a\u0430) &#8212; \u044d\u0442\u043e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043a\u0443\u0441\u043e\u0447\u0435\u043a \u0442\u0435\u043a\u0441\u0442\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043a\u0430\u043a \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447=\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0435\u0440\u0432\u0435\u0440 \u043f\u0440\u043e\u0441\u0438\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0443 \u0441\u0435\u0431\u044f \u0438 \u043f\u0440\u0438\u0441\u044b\u043b\u0430\u0442\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u043e, \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043a \u044d\u0442\u043e\u043c\u0443 \u0436\u0435 \u0441\u0430\u0439\u0442\u0443. \u041e\u043f\u044f\u0442\u044c \u0442\u0430\u043a\u0438, \u0435\u0441\u043b\u0438 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0442\u043e \u0441\u043d\u043e\u0432\u0430 \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441 \u044d\u0442\u0438\u043c \u0436\u0435 \u0433\u0430\u0440\u0434\u0435\u0440\u043e\u0431\u043e\u043c \u0438 \u0442\u0435\u0430\u0442\u0440\u043e\u043c: \u043a\u043e\u0433\u0434\u0430 \u0432\u044b\u0434\u0430\u043b\u0438 \u043d\u043e\u043c\u0435\u0440\u043e\u043a \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432\u0438\u0441\u0438\u0442 \u043a\u0443\u0440\u0442\u043a\u0430 &#8212; \u044d\u0442\u043e cookie, \u0430 \u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u0440\u0438 \u043b\u044e\u0431\u044b\u0445 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0445 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f\u0445 \u0432 \u0442\u0435\u0430\u0442\u0440\u0435 \u043f\u0440\u0438 \u043f\u0440\u043e\u0441\u044c\u0431\u0435 \u043f\u0440\u0435\u0434\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u043c\u0435\u0440\u043a\u0430, \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0435 \u044d\u0442\u043e\u0442 \u043d\u043e\u043c\u0435\u0440\u043e\u043a (cookie) \u0438 \u043d\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c. \u0422\u043e\u043b\u044c\u043a\u043e \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043a \u0441\u0430\u0439\u0442\u0443 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 cookie \u0441 id \u0441\u0435\u0441\u0441\u0438\u0438 (\u043d\u043e\u043c\u0435\u0440\u043a\u043e\u043c \u043a\u0443\u0440\u0442\u043a\u0438 \u0432 \u0433\u0430\u0440\u0434\u0435\u0440\u043e\u0431\u0435).\u041a\u0430\u043a \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c REST \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f:POST \/api\/auth\/login  { \u201cusername\u201d: \u201c\u2026\u201d, \u201cpassword\u201d: \u201c\u2026\u201d }  \u2192 JSONSpring \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0447\u0435\u0440\u0435\u0437 AuthenticationManagerSecurityContext \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u0432 HttpSession \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435\u0411\u0440\u0430\u0443\u0437\u0435\u0440\u0443 \u043e\u0442\u0434\u0430\u0451\u0442\u0441\u044f Set-Cookie: JSESSIONID=abc123 (\u0444\u0440\u043e\u043d\u0442\u0435\u043d\u0434 \u0435\u0433\u043e \u043d\u0435 \u0447\u0438\u0442\u0430\u0435\u0442, \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 fetch \u0441 credentials: \u201cinclude\u201d)\u041f\u0440\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u0445 Spring \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 SecurityContext \u0438\u0437 \u0441\u0435\u0441\u0441\u0438\u0438\u041b\u044e\u0431\u0430\u044f \u043e\u0448\u0438\u0431\u043a\u0430 (401\/403) \u2014 \u044d\u0442\u043e JSON-\u0442\u0435\u043b\u043e \u0432\u0438\u0434\u0430 { \u201cstatus\u201d: 401, \u201cmessage\u201d: \u201c\u2026\u201d }, \u0430 \u043d\u0435 HTML-\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0438 \u043d\u0435 \u0440\u0435\u0434\u0438\u0440\u0435\u043a\u0442\u041f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c \u043a\u0430\u043a \u043d\u0430\u0447\u0430\u0442\u044c \u043f\u0438\u0441\u0430\u0442\u044c SecurityConfig, \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c ApiResponse \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u044b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e, RestAccessDeniedHandler \u0438 RestAuthenticationEntryPoint \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0431\u0443\u0434\u0443\u0442 \u043d\u0443\u0436\u043d\u044b \u0432 SecurityConfig \u0432 \u0432\u0438\u0434\u0435 JSON-\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u043e\u0448\u0438\u0431\u043e\u043a \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438.\u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c record ApiResponse:public record ApiResponse(  int status,  String error,  String message){}\u041d\u0438\u0447\u0435\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0433\u043e \u0442\u0443\u0442 \u043d\u0435\u0442, \u0432 API \u043e\u0442\u0432\u0435\u0442\u0435 \u0431\u0443\u0434\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0430\u0442\u0443\u0441, \u043e\u0448\u0438\u0431\u043a\u0430 \u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438, \u043d\u0438\u0447\u0435\u0433\u043e \u0431\u043e\u043b\u0435\u0435.\u0414\u0430\u043b\u0435\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c RestAccessDeniedHandler:@Componentpublic class  implements AccessDeniedHandler {    private final ObjectMapper objectMapper = new ObjectMapper();    @Override    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {        response.setStatus(HttpStatus.FORBIDDEN.value());        response.setContentType(MediaType.APPLICATION_JSON_VALUE);        ApiError body = new ApiError(403, &#171;Forbidden&#187;, accessDeniedException.getMessage());        objectMapper.writeValue(response.getWriter(), body);    }}\u0414\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u0437\u0430 \u0441\u043b\u0443\u0447\u0430\u0439, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d, \u043d\u043e \u043f\u0440\u0430\u0432 \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043e\u043d \u043f\u043e\u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u0439\u0442\u0438 \u043d\u0430 \u0430\u0434\u043c\u0438\u043d\u0441\u043a\u0438\u0435 url \u0438\u043b\u0438 \u0447\u0443\u0436\u043e\u0439 @PreAuthorize \u0438\u0437 ProfileService (\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043d\u043e\u0432\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c). \u0412 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0451\u043d JSON \u0441 \u043a\u043e\u0434\u043e\u043c 403 \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u043e\u0448\u0438\u0431\u043a\u0438.\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u0435 \u0432 \u043e\u0442\u0432\u0435\u0442 \u0441\u0442\u0430\u0432\u0438\u043c \u0441\u0442\u0430\u0442\u0443\u0441 403 \u0447\u0435\u0440\u0435\u0437 HttpStatus.FORBIDDEN.value(), \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0434\u043b\u044f \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u0438 \u0441\u0442\u0438\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c HttpServletResponse.SC_FORBIDDEN, \u043d\u043e \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u044b \u043b\u0443\u0447\u0448\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u043e.\u0414\u0430\u043b\u0435\u0435 \u0441\u0442\u0430\u0432\u0438\u043c \u0444\u043e\u0440\u043c\u0430\u0442 \u043e\u0442\u0432\u0435\u0442\u0430 JSON \u0447\u0435\u0440\u0435\u0437 MediaType.APPLICATION_JSON_VALUE. \u041f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u043d\u0430\u0448 reord \u0441 \u0442\u0435\u043b\u043e\u043c \u043e\u0442\u0432\u0435\u0442\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e response.getWriter() (\u043f\u043e\u0442\u043e\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0438\u0448\u0435\u0442 \u043f\u0440\u044f\u043c\u043e \u0432 \u0442\u0435\u043b\u043e HTTP-\u043e\u0442\u0432\u0435\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0443), \u0438 \u0447\u0435\u0440\u0435\u0437 objectMapper \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0442\u0435\u043b\u043e \u043e\u0442\u0432\u0435\u0442\u0430 \u0432 JSON.\u0417\u0430\u0442\u0435\u043c \u043f\u043e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0438 \u043d\u0443\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c RestAuthenticationEntryPoint:@Componentpublic class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {    private final ObjectMapper objectMapper = new ObjectMapper();    @Override    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {        response.setStatus(HttpStatus.UNAUTHORIZED.value());        response.setContentType(MediaType.APPLICATION_JSON_VALUE);        ApiError body = new ApiError(401, &#171;Unauthorized&#187;, authException.getMessage());        objectMapper.writeValue(response.getWriter(), body);    }}\u0421\u0443\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u044b \u0443 \u043d\u0435\u0433\u043e \u0442\u0430\u043a\u0430\u044f \u0436\u0435 \u043a\u0430\u043a \u0438 \u0443 AccessDeniedHandler, \u043d\u043e \u043e\u043d \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u044f, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043d\u0435 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u0438\u0446\u0440\u043e\u0432\u0430\u043d, \u0430 \u0441\u0430\u043c\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u0430\u044f, \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0430\u0442\u0443\u0441\u044b \u043e\u0442\u0432\u0435\u0442\u043e\u0432. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f 401, \u0430 \u0432 \u043f\u0440\u043e\u0448\u043b\u043e\u043c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f 403.\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u043a SecurityConfig:@Configuration@EnableWebSecurity@RequiredArgsConstructorpublic class SecurityConfig {    private final UserDetailsServiceImpl userDetailsService;    private final PasswordEncoder passwordEncoder;    private final RestAuthenticationEntryPoint authenticationEntryPoint;    private final RestAccessDeniedHandler accessDeniedHandler;    @Bean    public AuthenticationProvider authenticationProvider(){        DaoAuthenticationProvider provider = new DaoAuthenticationProvider(userDetailsService);        provider.setPasswordEncoder(passwordEncoder);        return provider;    }    @Bean    public AuthenticationManager authenticationManager(AuthenticationConfiguration config){        return config.getAuthenticationManager();    }    @Bean    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{        http.authorizeHttpRequests(auth -&gt;                auth.requestMatchers(&#171;\/api\/auth\/**&#187;).permitAll()                        .requestMatchers(&#171;\/api\/admin\/**&#187;).hasRole(&#171;ADMIN&#187;)                        .anyRequest().authenticated())                \/\/\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0435 \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0437\u0430\u0449\u0438\u0442\u044b \u043e\u0442 csrf \u0430\u0442\u0430\u043a\u0438                .csrf(csrf -&gt; csrf.disable())                \/\/ \u041b\u043e\u0433\u0438\u043d \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 SessionAuthController, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0438 \u043e\u0442\u0434\u0430\u0451\u0442 JSON&#8217;\u044b (\u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e\u0437\u0436\u0435)                .sessionManagement(session -&gt;                        session.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)                                .sessionFixation().migrateSession()                                .maximumSessions(1)                )                .authenticationProvider(authenticationProvider())                .exceptionHandling(ex -&gt;                   &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-486527","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/486527","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=486527"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/486527\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=486527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=486527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=486527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}