{"id":321443,"date":"2021-04-14T15:00:58","date_gmt":"2021-04-14T15:00:58","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=321443"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=321443","title":{"rendered":"\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 Keycloak \u043a Spring Boot \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442 \u0425\u0430\u0431\u0440!<\/p>\n<p>\u041a\u0430\u043a \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, spring OAuth2.0.x \u043f\u0435\u0440\u0435\u0432\u0435\u0434\u0435\u043d \u0432 \u0440\u0435\u0436\u0438\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u0443\u0436\u0435 \u043f\u043e\u0447\u0442\u0438 \u043a\u0430\u043a 2 \u0433\u043e\u0434\u0430 <a href=\"https:\/\/spring.io\/blog\/2019\/11\/14\/spring-security-oauth-2-4-0-2-3-8-released\"><u>\u043d\u0430\u0437\u0430\u0434<\/u><\/a> , \u0430 \u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0435\u0433\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430 \u0432 spring-security (<a href=\"https:\/\/github.com\/spring-projects\/spring-security\/wiki\/OAuth-2.0-Features-Matrix#frequently-asked-questions\"><u>\u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438<\/u><\/a>\u044f). \u0412 spring-security \u043e\u0442\u043a\u0430\u0437\u0430\u043b\u0438\u0441\u044c \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442\u044c Authorization service (<a href=\"https:\/\/spring.io\/blog\/2019\/11\/14\/spring-security-oauth-2-0-roadmap-update\"><u>roadmap<\/u><\/a>) \u0438 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u043e \u043d\u0435\u0433\u043e \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0435 \u0438\u043b\u0438 \u043f\u043b\u0430\u0442\u043d\u044b\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u0438, \u0432 \u0447\u0430\u0441\u0442\u043d\u043e\u0441\u0442\u0438 <a href=\"https:\/\/www.keycloak.org\/\"><u>keycloak<\/u><\/a>. \u0412 \u044d\u0442\u043e\u043c \u043f\u043e\u0441\u0442\u0435 \u043c\u044b \u0445\u043e\u0442\u0435\u043b\u0438 \u0431\u044b \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u0441\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430\u043c\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f keycloak \u043a \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c spring-boot.<\/p>\n<h3>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435<\/h3>\n<ul>\n<li>\n<p><a href=\"#about_keycloak\">\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043e Keycloak<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#run_keycloak\">\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0438 \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c keycloak<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#connect_via_adapter\">\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c Keycloak \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0430\u0434\u0430\u043f\u0442\u0435\u0440\u0430<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#using_oauth_client\">\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c OAuth2 Client \u0438\u0437 spring-security<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#connect_as_resource_server\">\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043a\u0430\u043a ResourceService<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#service_to_service\">\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c keycloak<\/a><\/p>\n<\/li>\n<\/ul>\n<p><a class=\"anchor\" name=\"about_keycloak\" id=\"about_keycloak\"><\/a><\/p>\n<h3>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043e Keycloak<\/h3>\n<p>\u042d\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f SSO (Single sign on) \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u043c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u043c \u043a\u043e\u0434\u043e\u043c \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0435\u0439 \u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u043e\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439.<\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u0439 \u0432 Keycloak:<\/p>\n<ul>\n<li>\n<p>Single-Sign On and Single-Sign Out.<\/p>\n<\/li>\n<li>\n<p>OpenID\/OAuth 2.0\/SAML.<\/p>\n<\/li>\n<li>\n<p>Identity Brokering \u2013 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0432\u043d\u0435\u0448\u043d\u0438\u0445 OpenID Connect \u0438\u043b\u0438 SAML.<\/p>\n<\/li>\n<li>\n<p>Social Login \u2013 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 Google, GitHub, Facebook, Twitter.<\/p>\n<\/li>\n<li>\n<p>User Federation \u2013 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438\u0437 LDAP \u0438 Active Directory \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p>Kerberos bridge \u2013 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 Kerberos \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439.<\/p>\n<\/li>\n<li>\n<p>\u0413\u0438\u0431\u043a\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0430\u043c\u0438 \u0447\u0435\u0440\u0435\u0437 realm.<\/p>\n<\/li>\n<li>\n<p>\u0410\u0434\u0430\u043f\u0442\u0435\u0440\u044b \u0434\u043b\u044f JavaScript, WildFly, JBoss EAP, Fuse, Tomcat, Jetty, Spring.<\/p>\n<\/li>\n<li>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043f\u043b\u0430\u0433\u0438\u043d\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p>\u0418 \u043c\u043d\u043e\u0433\u043e\u0435-\u043c\u043d\u043e\u0433\u043e\u0435 \u0434\u0440\u0443\u0433\u043e\u0435&#8230;<\/p>\n<\/li>\n<\/ul>\n<p><a class=\"anchor\" name=\"run_keycloak\" id=\"run_keycloak\"><\/a><\/p>\n<h3>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0438 \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c keycloak<\/h3>\n<p>\u0414\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 keycloak \u043d\u0430 \u043c\u0430\u0448\u0438\u043d\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0443\u0434\u043e\u0431\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c docker-compose. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432 \u0440\u0430\u0437\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0434\u043b\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u0441\u0435\u0440\u0432\u0438\u0441 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438, \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0438\u0437\u0431\u0430\u0432\u043b\u044f\u044f \u0441\u0435\u0431\u044f \u043e\u0442 \u043a\u0443\u0447\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0435\u0439 \u043f\u043e\u0434 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043e\u0434\u0438\u043d \u0438\u0437 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 docker-compose \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 standalone \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445 postgres:<\/p>\n<details class=\"spoiler\">\n<summary>docker-compose.yml<\/summary>\n<div class=\"spoiler__content\">\n<pre><code>version: \"3.8\"  services:   postgres:     container_name: postgres     image: library\/postgres     environment:       POSTGRES_USER: ${POSTGRES_USER:-postgres}       POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}       POSTGRES_DB: keycloak_db     ports:       - \"5432:5432\"     restart: unless-stopped    keycloak:     image: jboss\/keycloak     container_name: keycloak     environment:       DB_VENDOR: POSTGRES       DB_ADDR: postgres       DB_DATABASE: keycloak_db       DB_USER: ${POSTGRES_USER:-postgres}       DB_PASSWORD: ${POSTGRES_PASSWORD:-postgres}       KEYCLOAK_USER: admin       KEYCLOAK_PASSWORD: admin_password     ports:       - \"8484:8080\"     depends_on:       - postgres     links:       - \"postgres:postgres\"<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 realm, \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432, \u0440\u043e\u043b\u0435\u0439 \u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439.<\/p>\n<p>\u041f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c realm &#171;my_realm&#187;:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/165\/a2e\/b22\/165a2eb22e3403e0931510220dc90072.png\" width=\"2136\" height=\"726\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0438\u0435\u043d\u0442 <code>\"my_client\"<\/code>, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u044c \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 (\u043e\u0441\u0442\u0430\u0432\u0438\u043c \u0432\u0441\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043f\u043e-\u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e):<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b19\/a8b\/b02\/b19a8bb027e5adea60f17820b3742c98.png\" width=\"2458\" height=\"1486\"><figcaption><\/figcaption><\/figure>\n<p>\u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c <code>redirect_url<\/code>. \u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u0435\u043d: http:\/\/localhost:8080\/*<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0440\u043e\u043b\u0438 \u0434\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043d\u0430\u0448\u0435\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b &#8212; <code>\"ADMIN\", \"USER\"<\/code>:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b40\/ffc\/5e4\/b40ffc5e45fd0f6aac703c993ea8c40c.png\" width=\"1546\" height=\"806\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 <code>\"admin\"<\/code> \u0441 \u0440\u043e\u043b\u044c\u044e <code>\"ADMIN\"<\/code>:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/267\/d29\/fc7\/267d29fc799363487c23bb962b01524c.png\" width=\"2450\" height=\"1004\"><figcaption><\/figcaption><\/figure>\n<p>\u0418 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f <code>\"user\"<\/code> \u0441 \u0440\u043e\u043b\u044c\u044e <code>\"USER\"<\/code>. \u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u043f\u0430\u0440\u043e\u043b\u0438 \u043d\u0430 \u0432\u043a\u043b\u0430\u0434\u043a\u0435 <code>\"Credentials\"<\/code>:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e72\/c5e\/c3f\/e72c5ec3f54a190ab98d9a65c0d72802.png\" width=\"2820\" height=\"1172\"><figcaption><\/figcaption><\/figure>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0437\u0430\u043a\u043e\u043d\u0447\u0435\u043d\u0430, \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043a \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044e spring boot \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439.<\/p>\n<p><a class=\"anchor\" name=\"connect_via_adapter\" id=\"connect_via_adapter\"><\/a><\/p>\n<h3>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c Keycloak \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0430\u0434\u0430\u043f\u0442\u0435\u0440\u0430<\/h3>\n<p>\u0412 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043a keycloak \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0433\u043e\u0442\u043e\u0432\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 &#8212; \u0430\u0434\u0430\u043f\u0442\u0435\u0440\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u0430\u044e\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 boilerplate \u043a\u043e\u0434\u0430 \u0438 \u0438\u0437\u043b\u0438\u0448\u043d\u0435\u0433\u043e \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0415\u0441\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0430 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u043e\u0432 \u0438 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u043e\u0432 (<a href=\"https:\/\/www.keycloak.org\/docs\/latest\/securing_apps\/index.html#supported-platforms\"><u>supported-platforms<\/u><\/a>). \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Spring Boot Adapter.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0435, \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 spring-boot (\u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 <a href=\"https:\/\/github.com\/kazakovav\/spring-boot-keycloak\/tree\/main\/demo-keycloak-adapter\">\u0437\u0434\u0435\u0441\u044c<\/a>) \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u043a \u043d\u0435\u043c\u0443 Keycloak Spring Boot \u0430\u0434\u0430\u043f\u0442\u0435\u0440. \u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b maven \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<details class=\"spoiler\">\n<summary>pom.xml<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt; &lt;project xmlns=\"&lt;http:\/\/maven.apache.org\/POM\/4.0.0&gt;\" xmlns:xsi=\"&lt;http:\/\/www.w3.org\/2001\/XMLSchema-instance&gt;\" xsi:schemaLocation=\"&lt;http:\/\/maven.apache.org\/POM\/4.0.0&gt; &lt;https:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd&gt;\"&gt; \t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt; \t&lt;parent&gt; \t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t&lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt; \t\t&lt;version&gt;2.3.9.RELEASE&lt;\/version&gt; \t\t&lt;relativePath \/&gt; &lt;!-- lookup parent from repository --&gt; \t&lt;\/parent&gt; \t&lt;groupId&gt;org.akazakov.keycloak&lt;\/groupId&gt; \t&lt;artifactId&gt;demo-keycloak-adapter&lt;\/artifactId&gt; \t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt; \t&lt;name&gt;Demo Keycloak Adapter&lt;\/name&gt; \t&lt;description&gt;Demo project for Spring Boot and Keycloak&lt;\/description&gt; \t&lt;properties&gt; \t\t&lt;java.version&gt;11&lt;\/java.version&gt; \t&lt;\/properties&gt; \t&lt;dependencyManagement&gt; \t\t&lt;dependencies&gt; \t\t\t&lt;dependency&gt; \t\t\t\t&lt;groupId&gt;org.keycloak.bom&lt;\/groupId&gt; \t\t\t\t&lt;artifactId&gt;keycloak-adapter-bom&lt;\/artifactId&gt; \t\t\t\t&lt;version&gt;12.0.3&lt;\/version&gt; \t\t\t\t&lt;type&gt;pom&lt;\/type&gt; \t\t\t\t&lt;scope&gt;import&lt;\/scope&gt; \t\t\t&lt;\/dependency&gt; \t\t&lt;\/dependencies&gt; \t&lt;\/dependencyManagement&gt; \t&lt;dependencies&gt; \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-boot-starter-security&lt;\/artifactId&gt; \t\t&lt;\/dependency&gt; \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt; \t\t&lt;\/dependency&gt; \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.keycloak&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;keycloak-spring-boot-starter&lt;\/artifactId&gt; \t\t&lt;\/dependency&gt; \t\t  \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-boot-starter-test&lt;\/artifactId&gt; \t\t\t&lt;scope&gt;test&lt;\/scope&gt; \t\t&lt;\/dependency&gt; \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.security&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-security-test&lt;\/artifactId&gt; \t\t\t&lt;scope&gt;test&lt;\/scope&gt; \t\t&lt;\/dependency&gt;  \t&lt;\/dependencies&gt; &lt;\/project&gt;<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u0414\u043b\u044f \u0446\u0435\u043b\u0435\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0440\u043e\u043b\u0435\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435 (\u044d\u0442\u043e\u0442 \u0436\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u043d\u0438\u0436\u0435):<\/p>\n<pre><code class=\"java\">@RestController @RequestMapping(\"\/api\") public class SampleController {      @GetMapping(\"\/anonymous\")     public String getAnonymousInfo() {         return \"Anonymous\";     }      @GetMapping(\"\/user\")     @PreAuthorize(\"hasRole('USER')\")     public String getUserInfo() {         return \"user info\";     }      @GetMapping(\"\/admin\")     @PreAuthorize(\"hasRole('ADMIN')\")     public String getAdminInfo() {         return \"admin info\";     }      @GetMapping(\"\/service\")     @PreAuthorize(\"hasRole('SERVICE')\")     public String getServiceInfo() {         return \"service info\";     }      @GetMapping(\"\/me\")     public Object getMe() {         final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();         return authentication.getName();     } } <\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043b\u043e\u0441\u044c \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043b\u043e\u0441\u044c \u043a keycloak, \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e. \u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043c\u044b \u0441\u0434\u0435\u043b\u0430\u0435\u043c, \u044d\u0442\u043e \u0432 application.yml \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<pre><code>server:   port: ${SERVER_PORT:8080} spring:   application.name: ${APPLICATION_NAME:spring-security-keycloak} keycloak:   auth-server-url: http:\/\/localhost:8484\/auth   realm: my_realm   resource: my_client   public-client: true<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e spring-security, \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c <code>KeycloakWebSecurityConfigurerAdapter<\/code>, \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0439 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0430\u0434\u0430\u043f\u0442\u0435\u0440\u043e\u043c:<\/p>\n<pre><code class=\"java\">@KeycloakConfiguration @EnableGlobalMethodSecurity(prePostEnabled = true) public class WebSecurityConfig extends KeycloakWebSecurityConfigurerAdapter {      @Override     protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {         return new NullAuthenticatedSessionStrategy();     }      @Autowired     public void configureGlobal(AuthenticationManagerBuilder authManagerBuilder) {         KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();         keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());         authManagerBuilder.authenticationProvider(keycloakAuthenticationProvider);     }      @Bean     public KeycloakConfigResolver keycloakConfigResolver() {         return new KeycloakSpringBootConfigResolver();     }      @Override     protected void configure(HttpSecurity http) throws Exception {         super.configure(http);         http                 .authorizeRequests()                 .antMatchers(\"\/api\/anonymous\/**\").permitAll()                 .anyRequest().fullyAuthenticated();     } }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0440\u0430\u0431\u043e\u0442\u0443 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0417\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0437\u0430\u0439\u0442\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c \u043d\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 url. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440: <code>http:\/\/localhost:8080\/api\/admin<\/code>. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435, \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u0438\u0442 \u043d\u0430\u0441 \u043d\u0430 \u043e\u043a\u043d\u043e \u043b\u043e\u0433\u0438\u043d\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4b9\/033\/e79\/4b9033e796a68b49de59f1ce0ccc8d6e.png\" width=\"1468\" height=\"1108\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0432\u0432\u043e\u0434\u0430 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0433\u043e \u0438\u043c\u0435\u043d\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u043f\u0430\u0440\u043e\u043b\u044f, \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u0438\u0442 \u043d\u0430\u0441 \u043d\u0430 \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u0430\u0434\u0440\u0435\u0441. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/2cc\/e95\/ee8\/2cce95ee881e1d57164a13fc81aeddc1.png\" width=\"1336\" height=\"1126\"><figcaption><\/figcaption><\/figure>\n<p>\u0415\u0441\u043b\u0438 \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435 (<code>http:\/\/localhost:8080\/api\/me<\/code>), \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 uuid \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432 keycloak:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7a2\/e7f\/2c7\/7a2e7f2c72cd1dd40af80a148b9ca60f.png\" width=\"1336\" height=\"468\"><figcaption><\/figcaption><\/figure>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u0435\u0440\u0432\u0438\u0441 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u043b \u0442\u043e\u043a\u0435\u043d \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u0438 \u043d\u0435 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0443 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c <code>bearer-only: true<\/code> \u0432 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f:<\/p>\n<pre><code>keycloak:   auth-server-url: http:\/\/localhost:8484\/auth   realm: my_realm   resource: my_client   public-client: true   bearer-only: true<\/code><\/pre>\n<p><a class=\"anchor\" name=\"using_oauth_client\" id=\"using_oauth_client\"><\/a><\/p>\n<h3>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c OAuth2 Client \u0438\u0437 spring-security<\/h3>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 keycloak \u0430\u0434\u0430\u043f\u0442\u0435\u0440\u0430 \u0438\u0437\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0430\u0441 \u043e\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043a\u0443\u0447\u0438 boilerplate \u043a\u043e\u0434\u0430. \u041d\u043e \u0432 \u0442\u043e \u0436\u0435 \u0432\u0440\u0435\u043c\u044f \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u043c \u043e\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u0412 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043d\u0435 \u0441\u0442\u043e\u0438\u0442 \u0437\u0430\u0432\u044f\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u0441\u0435\u0440\u0432\u0438\u0441 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438, \u044d\u0442\u043e \u0434\u0430\u0441\u0442 \u043d\u0430\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0433\u0438\u0431\u043a\u043e\u0441\u0442\u0438 \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0439 \u044d\u043a\u0441\u043f\u043b\u0443\u0430\u0442\u0430\u0446\u0438\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u044b.<\/p>\n<p>\u041e\u0434\u043d\u043e\u0439 \u0438\u0437 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439 spring security 5 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u043e\u0432 OAuth2 \u0438 OIDC. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c OAuth2 \u043a\u043b\u0438\u0435\u043d\u0442 \u0438\u0437 \u043f\u0430\u043a\u0435\u0442\u0430 spring-security \u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0438 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c keycloak.<\/p>\n<p>\u0418\u0442\u0430\u043a, \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 (<a href=\"https:\/\/github.com\/kazakovav\/spring-boot-keycloak\/tree\/main\/demo-keycloak-oauth\">\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u0440\u0438\u043c\u0435\u0440\u0430<\/a>). \u041f\u043e\u043b\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442 <code>pom.xml<\/code>:<\/p>\n<details class=\"spoiler\">\n<summary>pom.xml<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt; &lt;project xmlns=\"&lt;http:\/\/maven.apache.org\/POM\/4.0.0&gt;\" xmlns:xsi=\"&lt;http:\/\/www.w3.org\/2001\/XMLSchema-instance&gt;\"          xsi:schemaLocation=\"&lt;http:\/\/maven.apache.org\/POM\/4.0.0&gt; &lt;https:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd&gt;\"&gt;     &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;     &lt;parent&gt;         &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;         &lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt;         &lt;version&gt;2.3.9.RELEASE&lt;\/version&gt;         &lt;relativePath\/&gt; &lt;!-- lookup parent from repository --&gt;     &lt;\/parent&gt;     &lt;groupId&gt;org.akazakov.keycloak&lt;\/groupId&gt;     &lt;artifactId&gt;demo-keycloak-oauth&lt;\/artifactId&gt;     &lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;     &lt;name&gt;demo-keycloak-oauth&lt;\/name&gt;     &lt;description&gt;Demo project for Spring Boot OAuth and Keycloak&lt;\/description&gt;     &lt;properties&gt;         &lt;java.version&gt;11&lt;\/java.version&gt;     &lt;\/properties&gt;     &lt;dependencies&gt;         &lt;dependency&gt;             &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;             &lt;artifactId&gt;spring-boot-starter-oauth2-client&lt;\/artifactId&gt;         &lt;\/dependency&gt;         &lt;dependency&gt;             &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;             &lt;artifactId&gt;spring-boot-starter-security&lt;\/artifactId&gt;         &lt;\/dependency&gt;         &lt;dependency&gt;             &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;             &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;         &lt;\/dependency&gt;          &lt;dependency&gt;             &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;             &lt;artifactId&gt;spring-boot-starter-test&lt;\/artifactId&gt;             &lt;scope&gt;test&lt;\/scope&gt;         &lt;\/dependency&gt;         &lt;dependency&gt;             &lt;groupId&gt;org.springframework.security&lt;\/groupId&gt;             &lt;artifactId&gt;spring-security-test&lt;\/artifactId&gt;             &lt;scope&gt;test&lt;\/scope&gt;         &lt;\/dependency&gt;     &lt;\/dependencies&gt;      &lt;build&gt;         &lt;plugins&gt;             &lt;plugin&gt;                 &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;                 &lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt;             &lt;\/plugin&gt;         &lt;\/plugins&gt;     &lt;\/build&gt;  &lt;\/project&gt;<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0432 <code>application.yaml<\/code> \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a \u0441\u0435\u0440\u0432\u0438\u0441\u0443 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<pre><code>server:   port: ${SERVER_PORT:8080} spring:   application.name: ${APPLICATION_NAME:spring-security-keycloak-oauth}   security:     oauth2:       client:         provider:           keycloak:             issuer-uri: http:\/\/localhost:8484\/auth\/realms\/my_realm         registration:           keycloak:             client-id: my_client<\/code><\/pre>\n<p>\u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0440\u043e\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c\u0441\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <code>\"scope\"<\/code> \u0432 access token, \u0438 \u043a \u043d\u0438\u043c \u043f\u0440\u0438\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f <code>\"ROLE_USER\"<\/code> \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b. \u041c\u043e\u0436\u043d\u043e \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043a\u0430\u043a \u0435\u0441\u0442\u044c \u0438 \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043d\u0430 \u043c\u043e\u0434\u0435\u043b\u044c scope. \u041d\u043e \u0432 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u043e\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 realm&#8217;\u0430. \u0412\u0441\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e, \u044d\u0442\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c <code>oidcUserService<\/code> \u0438 \u0437\u0430\u0434\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u043c\u0430\u043f\u043f\u0438\u043d\u0433 \u0440\u043e\u043b\u0435\u0439 \u0434\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u041d\u0443\u0436\u043d\u044b\u0435 \u0440\u043e\u043b\u0438 \u043f\u0440\u0438\u0445\u043e\u0434\u044f\u0442 \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u0435 <code>\"groups\"<\/code> \u0442\u043e\u043a\u0435\u043d\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430, \u0435\u0433\u043e \u043c\u044b \u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0440\u043e\u043b\u0435\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435, \u043d\u0430\u0448\u0430 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f \u0434\u043b\u044f spring security \u0441 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c <code>oidcUserService<\/code> \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"java\">@Configuration @EnableGlobalMethodSecurity(prePostEnabled = true) public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {      @Override     protected void configure(HttpSecurity http) throws Exception {         http                 .authorizeRequests(authorizeRequests -&gt; authorizeRequests                         .antMatchers(\"\/api\/anonymous\/**\").permitAll()                         .anyRequest().authenticated())                 .oauth2Login(oauth2Login -&gt; oauth2Login                         .userInfoEndpoint(userInfoEndpoint -&gt; userInfoEndpoint                                 .oidcUserService(this.oidcUserService())                         )                 );      }      @Bean     public OAuth2UserService&lt;OidcUserRequest, OidcUser&gt; oidcUserService() {         final OidcUserService delegate = new OidcUserService();          return (userRequest) -&gt; {             OidcUser oidcUser = delegate.loadUser(userRequest);              final Map&lt;String, Object&gt; claims = oidcUser.getClaims();             final JSONArray groups = (JSONArray) claims.get(\"groups\");              final Set&lt;GrantedAuthority&gt; mappedAuthorities = groups.stream()                     .map(role -&gt; new SimpleGrantedAuthority((\"ROLE_\" + role)))                     .collect(Collectors.toSet());              return new DefaultOidcUser(mappedAuthorities, oidcUser.getIdToken(), oidcUser.getUserInfo());         };     } }<\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0440\u0430\u0431\u043e\u0442\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c keycloak \u0430\u0434\u0430\u043f\u0442\u0435\u0440\u0430.<\/p>\n<p><a class=\"anchor\" name=\"connect_as_resource_server\" id=\"connect_as_resource_server\"><\/a><\/p>\n<h3>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043a\u0430\u043a ResourceService<\/h3>\n<p>\u0414\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0447\u0430\u0441\u0442\u043e \u043d\u0435 \u043d\u0443\u0436\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u043e\u0432\u0430\u043b\u043e \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0414\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043b\u0438\u0448\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u043e\u043c\u0443 \u0442\u043e\u043a\u0435\u043d\u0443 \u0434\u043e\u0441\u0442\u0443\u043f\u0430. \u0412\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u043c \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0441 keycloak \u0431\u0435\u0437 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0430\u0434\u0430\u043f\u0442\u0435\u0440\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043a\u0430\u043a resource server. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0443\u0435\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043f\u043e\u0434\u043f\u0438\u0441\u044c \u0442\u043e\u043a\u0435\u043d\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430. \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438: spring-security-oauth2-resource-server \u0438 spring-security-oauth2-jose (<a href=\"https:\/\/github.com\/kazakovav\/spring-boot-keycloak\/tree\/main\/demo-keycloak-resource\">\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434<\/a>). \u041f\u043e\u043b\u043d\u044b\u0439 \u0444\u0430\u0439\u043b <code>pom.xml<\/code> \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<details class=\"spoiler\">\n<summary>pom.xml<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt; &lt;project xmlns=\"&lt;http:\/\/maven.apache.org\/POM\/4.0.0&gt;\" xmlns:xsi=\"&lt;http:\/\/www.w3.org\/2001\/XMLSchema-instance&gt;\" \txsi:schemaLocation=\"&lt;http:\/\/maven.apache.org\/POM\/4.0.0&gt; &lt;https:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd&gt;\"&gt; \t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt; \t&lt;parent&gt; \t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t&lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt; \t\t&lt;version&gt;2.3.9.RELEASE&lt;\/version&gt; \t\t&lt;relativePath\/&gt; &lt;!-- lookup parent from repository --&gt; \t&lt;\/parent&gt; \t&lt;groupId&gt;org.akazakov.keycloak&lt;\/groupId&gt; \t&lt;artifactId&gt;demo-keycloak-resource&lt;\/artifactId&gt; \t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt; \t&lt;name&gt;demo-keycloak-resource&lt;\/name&gt; \t&lt;description&gt;Demo project for Spring Boot and Spring security and Keycloak&lt;\/description&gt; \t&lt;properties&gt; \t\t&lt;java.version&gt;11&lt;\/java.version&gt; \t&lt;\/properties&gt; \t&lt;dependencies&gt; \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-boot-starter-security&lt;\/artifactId&gt; \t\t&lt;\/dependency&gt; \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.security&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-security-oauth2-resource-server&lt;\/artifactId&gt; \t\t&lt;\/dependency&gt; \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.security&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-security-oauth2-jose&lt;\/artifactId&gt; \t\t&lt;\/dependency&gt; \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt; \t\t&lt;\/dependency&gt; \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-boot-starter-test&lt;\/artifactId&gt; \t\t\t&lt;scope&gt;test&lt;\/scope&gt; \t\t&lt;\/dependency&gt; \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.security&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-security-test&lt;\/artifactId&gt; \t\t\t&lt;scope&gt;test&lt;\/scope&gt; \t\t&lt;\/dependency&gt; \t&lt;\/dependencies&gt;  \t&lt;build&gt; \t\t&lt;plugins&gt; \t\t\t&lt;plugin&gt; \t\t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t\t\t&lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt; \t\t\t&lt;\/plugin&gt; \t\t&lt;\/plugins&gt; \t&lt;\/build&gt;  &lt;\/project&gt;<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0443\u0442\u044c \u043a JWK (JSON Web Key) \u043d\u0430\u0431\u043e\u0440\u0443 \u043a\u043b\u044e\u0447\u0435\u0439, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0442\u043e\u043a\u0435\u043d\u044b \u0434\u043e\u0441\u0442\u0443\u043f\u0430. \u0412 keycloak \u043e\u043d\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443: <code>http:\/\/${host}\/auth\/realms\/${realm)\/protocol\/openid-connect\/certs<\/code>. \u0412 \u0438\u0442\u043e\u0433\u0435 <code>application.yml<\/code> \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u0434\u044f\u0434\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code>server:   port: ${SERVER_PORT:8080} spring:   application.name: ${APPLICATION_NAME:spring-security-keycloak-resource}   security:     oauth2:       resourceserver:         jwt:           jwk-set-uri: ${KEYCLOAK_REALM_CERT_URL:http:\/\/localhost:8484\/auth\/realms\/my_realm\/protocol\/openid-connect\/certs} <\/code><\/pre>\n<p>\u041a\u0430\u043a \u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441 OAuth2 Client \u043d\u0430\u043c \u0442\u0430\u043a\u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0435\u0440 \u0440\u043e\u043b\u0435\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c <code>jwtAuthenticationConverter<\/code>.<\/p>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442 <code>WebSecurityConfiguration<\/code>:<\/p>\n<pre><code class=\"java\">@Configuration @EnableGlobalMethodSecurity(prePostEnabled = true) public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {      @Override     protected void configure(HttpSecurity http) throws Exception {         http                 .authorizeRequests(authorizeRequests -&gt; authorizeRequests                         .antMatchers(\"\/api\/anonymous\/**\").permitAll()                         .anyRequest().authenticated())                 .oauth2ResourceServer(resourceServerConfigurer -&gt; resourceServerConfigurer                         .jwt(jwtConfigurer -&gt; jwtConfigurer                                 .jwtAuthenticationConverter(jwtAuthenticationConverter()))                 );     }      @Bean     public Converter&lt;Jwt, AbstractAuthenticationToken&gt; jwtAuthenticationConverter() {         JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter();         jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(jwtGrantedAuthoritiesConverter());         return jwtAuthenticationConverter;     }      @Bean     public Converter&lt;Jwt, Collection&lt;GrantedAuthority&gt;&gt; jwtGrantedAuthoritiesConverter() {         JwtGrantedAuthoritiesConverter delegate = new JwtGrantedAuthoritiesConverter();          return new Converter&lt;&gt;() {             @Override             public Collection&lt;GrantedAuthority&gt; convert(Jwt jwt) {                 Collection&lt;GrantedAuthority&gt; grantedAuthorities = delegate.convert(jwt);                  if (jwt.getClaim(\"realm_access\") == null) {                     return grantedAuthorities;                 }                 JSONObject realmAccess = jwt.getClaim(\"realm_access\");                 if (realmAccess.get(\"roles\") == null) {                     return grantedAuthorities;                 }                 JSONArray roles = (JSONArray) realmAccess.get(\"roles\");                  final List&lt;SimpleGrantedAuthority&gt; keycloakAuthorities = roles.stream().map(role -&gt; new SimpleGrantedAuthority(\"ROLE_\" + role)).collect(Collectors.toList());                 grantedAuthorities.addAll(keycloakAuthorities);                  return grantedAuthorities;             }         };     } }<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0435\u0440 (<code>jwtGrantedAuthoritiesConverter<\/code>), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0442\u043e\u043a\u0435\u043d \u0438 \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u0442 \u0438\u0437 \u0441\u0435\u043a\u0446\u0438\u0438 <code>\"realm_access\"<\/code> \u0440\u043e\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0438\u0431\u043e \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445, \u043b\u0438\u0431\u043e, \u043a\u0430\u043a \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u0442\u0441\u044f \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0435\u0440\u043e\u043c \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e.<\/p>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0440\u0430\u0431\u043e\u0442\u0443. \u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u043c \u0432 Intellij idea http \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c, \u043b\u0438\u0431\u043e \u043f\u043b\u0430\u0433\u0438\u043d\u043e\u043c \u043a VSCode &#8212; <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=humao.rest-client\">Rest Client<\/a>. \u0412 \u043d\u0430\u0447\u0430\u043b\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0442\u043e\u043a\u0435\u043d \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043a keycloak, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f:<\/p>\n<pre><code>### POST &lt;http:\/\/localhost:8484\/auth\/realms\/my_realm\/protocol\/openid-connect\/token&gt; Content-Type: application\/x-www-form-urlencoded  client_id=my_client&amp;grant_type=password&amp;scope=openid&amp;username=admin&amp;password=admin  &gt; {% client.global.set(\"auth_token\", response.body.access_token); %}<\/code><\/pre>\n<p>\u041e\u0442\u0432\u0435\u0442 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u044f:<\/p>\n<details class=\"spoiler\">\n<summary>\u041e\u0442\u0432\u0435\u0442<\/summary>\n<div class=\"spoiler__content\">\n<pre><code>POST &lt;http:\/\/localhost:8484\/auth\/realms\/my_realm\/protocol\/openid-connect\/token&gt;  HTTP\/1.1 200 OK ... Content-Type: application\/json  {   \"access_token\": \"eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJlb21qWFY2d3dNek8xVS0tYUdhVllpSHM3eURaZVM1aU96bl9JR3RlS1ZzIn0.eyJleHAiOjE2MTY2NTQzNjEsImlhdCI6MTYxNjY1NDA2MSwianRpIjoiMGQwMjg2YWUtYTlmYy00MzcxLWFmM2ItZjJlNTM5N2I4NzViIiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo4NDg0L2F1dGgvcmVhbG1zL215X3JlYWxtIiwiYXVkIjoiYWNjb3VudCIsInN1YiI6IjkzMGIxMTNmLWI0NzUtNDhkMC05NTQxLWMyYzI2MWZlYmRmZCIsInR5cCI6IkJlYXJlciIsImF6cCI6Im15X2NsaWVudCIsInNlc3Npb25fc3RhdGUiOiI1ZDI5ZDQ2ZS1iOTI2LTRkNTktODlmOC0yNDM2ZWRjYWU0ZjAiLCJhY3IiOiIxIiwicmVhbG1fYWNjZXNzIjp7InJvbGVzIjpbIm9mZmxpbmVfYWNjZXNzIiwiQURNSU4iLCJ1bWFfYXV0aG9yaXphdGlvbiJdfSwicmVzb3VyY2VfYWNjZXNzIjp7ImFjY291bnQiOnsicm9sZXMiOlsibWFuYWdlLWFjY291bnQiLCJtYW5hZ2UtYWNjb3VudC1saW5rcyIsInZpZXctcHJvZmlsZSJdfX0sInNjb3BlIjoib3BlbmlkIHByb2ZpbGUgZW1haWwiLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsInByZWZlcnJlZF91c2VybmFtZSI6ImFkbWluIn0.dvGvYhhhfH8r6EP8k_spFwBS35ulYMTWNL4lcz9PR2e-p4FU-ehre1EQA8xpbkYzYEWRB_elzTya5IhbYR8KArrujplIDNAOlqJ9W6a4Tx-r44QCteM0DW4BNzbZAH2L0Bg7aSstRKUuULceRNYQcdCvSFjEU5DsHk26a6TM5KCrkv0ryGo11pam-pnbs2Z2jOSfSHvOAfMNL9OVJYRBjlTmsEzzgH9dHSa_pT2Q-SvgvfCcwfY0XkgUZkMPUtz85-lqchROb4XpHOiy3Cfn8MgrGNwhf-MsmN5wiAGe0DI_LW2Jxr3boZMLS4AuuNQ7agr65g-JuO9-LhlgndxN8g\",   \"expires_in\": 300,   \"refresh_expires_in\": 1800,   \"refresh_token\": \"eyJhbGciOiJIUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJmNGEwNWQxNy0yNWU4LTRjMjEtOTMyMC0zMzcwODlhNTg5MjQifQ.eyJleHAiOjE2MTY2NTU4NjEsImlhdCI6MTYxNjY1NDA2MSwianRpIjoiMjNmNDBiZWUtNmQ3Ny00ZTIxLTg0NTItNDg1NDc2OTk1ZDUyIiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo4NDg0L2F1dGgvcmVhbG1zL215X3JlYWxtIiwiYXVkIjoiaHR0cDovL2xvY2FsaG9zdDo4NDg0L2F1dGgvcmVhbG1zL215X3JlYWxtIiwic3ViIjoiOTMwYjExM2YtYjQ3NS00OGQwLTk1NDEtYzJjMjYxZmViZGZkIiwidHlwIjoiUmVmcmVzaCIsImF6cCI6Im15X2NsaWVudCIsInNlc3Npb25fc3RhdGUiOiI1ZDI5ZDQ2ZS1iOTI2LTRkNTktODlmOC0yNDM2ZWRjYWU0ZjAiLCJzY29wZSI6Im9wZW5pZCBwcm9maWxlIGVtYWlsIn0.r4BrjwfavKFF8dst3AyRi0LTfymbSVfDKDT9KyMpmzk\",   \"token_type\": \"bearer\",   \"id_token\": \"eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJlb21qWFY2d3dNek8xVS0tYUdhVllpSHM3eURaZVM1aU96bl9JR3RlS1ZzIn0.eyJleHAiOjE2MTY2NTQzNjEsImlhdCI6MTYxNjY1NDA2MSwiYXV0aF90aW1lIjowLCJqdGkiOiJiN2UwNDhmZS01ZTRjLTQxMWYtYTBjMC0xNGExYzhlOGJhYWEiLCJpc3MiOiJodHRwOi8vbG9jYWxob3N0Ojg0ODQvYXV0aC9yZWFsbXMvbXlfcmVhbG0iLCJhdWQiOiJteV9jbGllbnQiLCJzdWIiOiI5MzBiMTEzZi1iNDc1LTQ4ZDAtOTU0MS1jMmMyNjFmZWJkZmQiLCJ0eXAiOiJJRCIsImF6cCI6Im15X2NsaWVudCIsInNlc3Npb25fc3RhdGUiOiI1ZDI5ZDQ2ZS1iOTI2LTRkNTktODlmOC0yNDM2ZWRjYWU0ZjAiLCJhdF9oYXNoIjoiRlh2VzB2Z3pwd3R6N1FabEZtTFhJdyIsImFjciI6IjEiLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsInByZWZlcnJlZF91c2VybmFtZSI6ImFkbWluIn0.ZDeZg4Z-PPmn2fVm7opGLRutzDh6l8uRYqZzbqIX7wk0GhgtMHV1CW8RvDd51AuYw81WyoMyRAD_-T6ne58Rt9f5XNZZfS8xoXzTFV1xH6XigOVQH2jIHN-2VIM1IgJnteo7nuTz9zo4OXIFvEjaFHq4AXDkiq6jhThv0qPS3WrAA-MutyW8G37GM0fsCgANvlGKoWm1_1wKyeTZ0Gfug32Vf6gUikfxA9bmaS4oGYGc6lqFE6EHgtjIn0q9gNUfpEXaqpiL3mCBu9V6sJG5Rp_MOqp-aXrM9NbLTz2JTXevtClHI6qVUIoh8OXXXT98QmKrVr9Cyr9BRUrQyt0Zzg\",   \"not-before-policy\": 0,   \"session_state\": \"5d29d46e-b926-4d59-89f8-2436edcae4f0\",   \"scope\": \"openid profile email\" }  Response code: 200 (OK); Time: 114ms; Content length: 2987 bytes<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0447\u0442\u043e \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0441 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c\u0438 \u043f\u0440\u0430\u0432\u0430\u043c\u0438:<\/p>\n<pre><code>GET &lt;http:\/\/localhost:8080\/api\/admin&gt; Authorization: Bearer {{auth_token}} Content-Type: application\/json<\/code><\/pre>\n<p>\u0412 \u043e\u0442\u0432\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u043c:<\/p>\n<pre><code>GET &lt;http:\/\/localhost:8080\/api\/admin&gt;  HTTP\/1.1 200  ...  admin info  Response code: 200; Time: 34ms; Content length: 10 bytes <\/code><\/pre>\n<p><a class=\"anchor\" name=\"service_to_service\" id=\"service_to_service\"><\/a><\/p>\n<h3>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c keycloak<\/h3>\n<p>\u041f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441&nbsp; \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u043d\u043e\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043e\u0439 \u0438\u043d\u043e\u0433\u0434\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c\u0438. \u0412 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u0438\u043d\u0438\u0446\u0438\u0430\u0442\u043e\u0440\u043e\u043c \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0438\u043b\u0438 \u0441\u043b\u0443\u0436\u0431\u0430, \u043d\u0430\u043c \u0433\u0434\u0435-\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0431\u0440\u0430\u0442\u044c \u0442\u043e\u043a\u0435\u043d \u0434\u043e\u0441\u0442\u0443\u043f\u0430. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0432\u043e\u043f\u0440\u043e\u0441\u0430 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Client Credentials Flow, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0442\u043e\u043a\u0435\u043d \u0438\u0437 keycloak (\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043f\u043e <a href=\"https:\/\/github.com\/kazakovav\/spring-boot-keycloak\/tree\/main\/demo-service-auth\">\u0441\u0441\u044b\u043b\u043a\u0435<\/a>).<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u043e\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0430, \u043f\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0431\u0443\u0434\u0443\u0442 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430\u0448\u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u044b:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/dc6\/a65\/f10\/dc6a65f107b085739080e58ffec4ff19.png\" width=\"1658\" height=\"822\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0442\u0438\u043f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 (<code>\"Access Type\"<\/code>) \u043d\u0430 <code>\"confidential\"<\/code> \u0438 \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0444\u043b\u0430\u0433 <code>\"Service accounts Enabled\"<\/code>. \u0412 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u043c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f \u043d\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/deb\/1e4\/005\/deb1e40057296e914291c0eaf944a522.png\" width=\"2218\" height=\"1398\"><figcaption><\/figcaption><\/figure>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e, \u0447\u0442\u043e\u0431\u044b \u0443 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432, \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u0434 \u0434\u0430\u043d\u043d\u044b\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u043c, \u0431\u044b\u043b\u0430 \u0441\u0432\u043e\u044f \u0440\u043e\u043b\u044c, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0435\u0435 \u0432 \u0440\u043e\u043b\u0438:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/660\/98f\/e01\/66098fe018973521124463865abfe944.png\" width=\"1696\" height=\"822\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u0430\u043b\u0435\u0435 \u044d\u0442\u0443 \u0440\u043e\u043b\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043b\u0438\u0435\u043d\u0442\u0443. \u041d\u0430 \u0432\u043a\u043b\u0430\u0434\u043a\u0435 <code>\"Service Account Roles\"<\/code> \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0443\u044e \u0440\u043e\u043b\u044c &#8212;&nbsp; \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0440\u043e\u043b\u044c <code>\"SERVICE\"<\/code>:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3b1\/e71\/85f\/3b1e7185f73ec5d522ed553f19e9a928.png\" width=\"2346\" height=\"946\"><figcaption><\/figcaption><\/figure>\n<p>\u0421\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c client_id \u0438 client_secret \u0434\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u0445 \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/735\/85f\/42e\/73585f42ec59b7d739b7573771f144a4.png\" width=\"1900\" height=\"780\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0443\u044e \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443<code> http:\/\/localhost:8080\/api\/service<\/code> \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u043d\u0430\u0448 \u0441\u0435\u0440\u0432\u0438\u0441 \u0432 keycloak:<\/p>\n<pre><code class=\"java\">@Component public class KeycloakAuthClient {     private static final Logger log = LoggerFactory             .getLogger(KeycloakAuthClient.class);      private static final String TOKEN_PATH = \"\/token\";     private static final String GRANT_TYPE = \"grant_type\";     private static final String CLIENT_ID = \"client_id\";     private static final String CLIENT_SECRET = \"client_secret\";     public static final String CLIENT_CREDENTIALS = \"client_credentials\";      @Value(\"${app.keycloak.auth-url:http:\/\/localhost:8484\/auth\/realms\/my_realm\/protocol\/openid-connect}\")     private String authUrl;      @Value(\"${app.keycloak.client-id:service_client}\")     private String clientId;      @Value(\"${app.keycloak.client-secret:acb719cf-4afd-42d3-91f2-93a60b3f2023}\")     private String clientSecret;      private final RestTemplate restTemplate;      public KeycloakAuthClient(RestTemplate restTemplate) {         this.restTemplate = restTemplate;     }      public KeycloakAuthResponse authenticate() {         MultiValueMap&lt;String, String&gt; paramMap = new LinkedMultiValueMap&lt;&gt;();         paramMap.add(CLIENT_ID, clientId);         paramMap.add(CLIENT_SECRET, clientSecret);         paramMap.add(GRANT_TYPE, CLIENT_CREDENTIALS);          HttpHeaders headers = new HttpHeaders();         headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);          String url = authUrl + TOKEN_PATH;          HttpEntity&lt;MultiValueMap&lt;String, String&gt;&gt; entity = new HttpEntity&lt;&gt;(paramMap, headers);          log.info(\"Try to authenticate\");          ResponseEntity&lt;KeycloakAuthResponse&gt; response =                 restTemplate.exchange(url,                         HttpMethod.POST,                         entity,                         KeycloakAuthResponse.class);          if (!response.getStatusCode().is2xxSuccessful()) {             log.error(\"Failed to authenticate\");             throw new RuntimeException(\"Failed to authenticate\");         }          log.info(\"Authentication success\");          return response.getBody();     } }<\/code><\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 <code>authenticate<\/code> \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442 \u0432\u044b\u0437\u043e\u0432 \u043a keycloak \u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0433\u043e \u043e\u0442\u0432\u0435\u0442\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 <code>KeycloakAuthResponse<\/code>:<\/p>\n<pre><code class=\"java\">public class KeycloakAuthResponse {     @JsonProperty(\"access_token\")     private String accessToken;      @JsonProperty(\"expires_in\")     private Integer expiresIn;      @JsonProperty(\"refresh_expires_in\")     private Integer refreshExpiresIn;      @JsonProperty(\"refresh_token\")     private String refreshToken;      @JsonProperty(\"token_type\")     private String tokenType;      @JsonProperty(\"id_token\")     private String idToken;      @JsonProperty(\"session_state\")     private String sessionState;      @JsonProperty(\"scope\")     private String scope;      \/\/ Getters and setters or lombok ... }<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u0431\u0435\u0440\u0435\u043c <code>access_token<\/code> \u0438\u0437 \u043e\u0442\u0432\u0435\u0442\u0430 \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0445 \u0432\u044b\u0437\u043e\u0432\u0430\u0445 \u043a \u0437\u0430\u0449\u0438\u0449\u0435\u043d\u043d\u044b\u043c \u043c\u0435\u0442\u043e\u0434\u0430\u043c. \u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b\u0437\u043e\u0432\u0430 \u043a \u0437\u0430\u0449\u0438\u0449\u0435\u043d\u043d\u043e\u043c\u0443 \u043c\u0435\u0442\u043e\u0434\u0443:<\/p>\n<pre><code class=\"java\">@SpringBootApplication public class DemoServiceAuthApplication implements CommandLineRunner {     private static final String BEARER = \"Bearer \";     private static final String SERVICE_INFO_URL = \"http:\/\/localhost:8080\/api\/service\";      private final KeycloakAuthClient keycloakAuthClient;      private final RestTemplate restTemplate;      private static final Logger log = LoggerFactory             .getLogger(DemoServiceAuthApplication.class);      public DemoServiceAuthApplication(KeycloakAuthClient keycloakAuthClient, RestTemplate restTemplate) {         this.keycloakAuthClient = keycloakAuthClient;         this.restTemplate = restTemplate;     }       public static void main(String[] args) {         SpringApplication.run(DemoServiceAuthApplication.class, args);     }      @Override     public void run(String... args) {         final KeycloakAuthResponse authenticate = keycloakAuthClient.authenticate();          HttpHeaders headers = new HttpHeaders();         headers.setContentType(MediaType.APPLICATION_JSON);         headers.setBearerAuth(authenticate.getAccessToken());          log.info(\"Make request to resource server\");          final ResponseEntity&lt;String&gt; responseEntity = restTemplate.exchange(SERVICE_INFO_URL, HttpMethod.GET, new HttpEntity(headers), String.class);          if (!responseEntity.getStatusCode().is2xxSuccessful()) {             log.error(\"Failed to request\");             throw new RuntimeException(\"Failed to request\");         }          log.info(\"Response data: {}\", responseEntity.getBody());     } }<\/code><\/pre>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0443\u0435\u043c \u043d\u0430\u0448 \u0441\u0435\u0440\u0432\u0438\u0441 \u0447\u0435\u0440\u0435\u0437 keycloak,&nbsp; \u043f\u043e\u0442\u043e\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043a \u0437\u0430\u0449\u0438\u0449\u0435\u043d\u043d\u043e\u043c\u0443 \u0440\u0435\u0441\u0443\u0440\u0441\u0443, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u0432 HTTP Headers \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>Authorization: Bearer ...<\/code><\/p>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0437\u0430\u0449\u0438\u0449\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430:<\/p>\n<pre><code class=\"bash\">.   ____          _            __ _ _  \/\\\\\\\\ \/ ___'_ __ _ _(_)_ __  __ _ \\\\ \\\\ \\\\ \\\\ ( ( )\\\\___ | '_ | '_| | '_ \\\\\/ _` | \\\\ \\\\ \\\\ \\\\  \\\\\\\\\/  ___)| |_)| | | | | || (_| |  ) ) ) )   '  |____| .__|_| |_|_| |_\\\\__, | \/ \/ \/ \/  =========|_|==============|___\/=\/_\/_\/_\/  :: Spring Boot ::                (v2.4.4)  2021-04-13 16:04:36.672  INFO 19240 --- [           main] o.a.keycloak.DemoServiceAuthApplication  : Starting DemoServiceAuthApplication using Java 14.0.1 on MacBook-Pro.local with PID 19240 (\/Users\/akazakov\/Projects\/spring-boot-keycloak\/demo-service-auth\/target\/classes started by akazakov in \/Users\/akazakov\/Projects\/spring-boot-keycloak) 2021-04-13 16:04:36.674  INFO 19240 --- [           main] o.a.keycloak.DemoServiceAuthApplication  : No active profile set, falling back to default profiles: default 2021-04-13 16:04:37.199  INFO 19240 --- [           main] o.a.keycloak.DemoServiceAuthApplication  : Started DemoServiceAuthApplication in 0.814 seconds (JVM running for 6.425) 2021-04-13 16:04:37.203  INFO 19240 --- [           main] o.akazakov.keycloak.KeycloakAuthClient   : Try to authenticate 2021-04-13 16:04:53.697  INFO 19240 --- [           main] o.akazakov.keycloak.KeycloakAuthClient   : Authentication success 2021-04-13 16:04:53.697  INFO 19240 --- [           main] o.a.keycloak.DemoServiceAuthApplication  : Make request to resource server 2021-04-13 16:04:54.088  INFO 19240 --- [           main] o.a.keycloak.DemoServiceAuthApplication  : Response data: service info Disconnected from the target VM, address: '127.0.0.1:57479', transport: 'socket'  Process finished with exit code 0 <\/code><\/pre>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b\u0448\u0435 \u0441\u0442\u0440\u043e\u0433\u043e \u0432 \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0446\u0435\u043b\u044f\u0445 <code>KeycloakAuthClient<\/code> \u043d\u0435\u043b\u044c\u0437\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043f\u0440\u043e\u0434\u0443\u043a\u0442\u043e\u0432\u043e\u0439 \u0441\u0440\u0435\u0434\u0435, \u043a\u0430\u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432\u0440\u0435\u043c\u044f, \u0430 \u0435\u0449\u0435 \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043f\u0440\u0438 \u0438\u0441\u0442\u0435\u0447\u0435\u043d\u0438\u0438 \u0435\u0433\u043e \u0441\u0440\u043e\u043a\u0430 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f.<\/p>\n<h3>\u0412\u044b\u0432\u043e\u0434\u044b<\/h3>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 keycloak \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u0430\u0434\u0430\u043f\u0442\u0435\u0440\u0430, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u0438\u0437\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0430\u0441 \u043e\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043a\u043e\u0434\u0430 \u0438 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0439. \u041d\u043e \u0442\u043e\u0433\u0434\u0430 \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0432\u044f\u0437\u0430\u043d\u043e \u043d\u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438. \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0436\u0435 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 spring \u0434\u0430\u0435\u0442 \u043d\u0430\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0433\u0438\u0431\u043a\u043e\u0441\u0442\u0438 \u0432 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0435 \u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u044b\u0431\u043e\u0440\u0430 \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f\u0445. \u041d\u043e \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0442\u0435\u043c \u0437\u0430\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0430\u0441 \u043f\u0438\u0441\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u043a\u043e\u0434\u0430 \u0438 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438, \u0445\u043e\u0442\u044f, \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u043d\u0435 \u043d\u0430\u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0443\u0436 \u043c\u043d\u043e\u0433\u043e. \u0412 \u043b\u044e\u0431\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043f\u0440\u0438 \u0432\u044b\u0431\u043e\u0440\u0435, \u043a\u0430\u043a \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043a \u0441\u0432\u043e\u0435\u043c\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e, \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0438\u0437 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432, \u0433\u043b\u0430\u0432\u043d\u044b\u043c \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0437\u0434\u0440\u0430\u0432\u044b\u0439 \u0441\u043c\u044b\u0441\u043b.<\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435!<\/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\/company\/reksoft\/blog\/552346\/\"> https:\/\/habr.com\/ru\/company\/reksoft\/blog\/552346\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442 \u0425\u0430\u0431\u0440!<\/p>\n<p>\u041a\u0430\u043a \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, spring OAuth2.0.x \u043f\u0435\u0440\u0435\u0432\u0435\u0434\u0435\u043d \u0432 \u0440\u0435\u0436\u0438\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u0443\u0436\u0435 \u043f\u043e\u0447\u0442\u0438 \u043a\u0430\u043a 2 \u0433\u043e\u0434\u0430 <a href=\"https:\/\/spring.io\/blog\/2019\/11\/14\/spring-security-oauth-2-4-0-2-3-8-released\"><u>\u043d\u0430\u0437\u0430\u0434<\/u><\/a> , \u0430 \u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0435\u0433\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430 \u0432 spring-security (<a href=\"https:\/\/github.com\/spring-projects\/spring-security\/wiki\/OAuth-2.0-Features-Matrix#frequently-asked-questions\"><u>\u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438<\/u><\/a>\u044f). \u0412 spring-security \u043e\u0442\u043a\u0430\u0437\u0430\u043b\u0438\u0441\u044c \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442\u044c Authorization service (<a href=\"https:\/\/spring.io\/blog\/2019\/11\/14\/spring-security-oauth-2-0-roadmap-update\"><u>roadmap<\/u><\/a>) \u0438 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u043e \u043d\u0435\u0433\u043e \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0435 \u0438\u043b\u0438 \u043f\u043b\u0430\u0442\u043d\u044b\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u0438, \u0432 \u0447\u0430\u0441\u0442\u043d\u043e\u0441\u0442\u0438 <a href=\"https:\/\/www.keycloak.org\/\"><u>keycloak<\/u><\/a>. \u0412 \u044d\u0442\u043e\u043c \u043f\u043e\u0441\u0442\u0435 \u043c\u044b \u0445\u043e\u0442\u0435\u043b\u0438 \u0431\u044b \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u0441\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430\u043c\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f keycloak \u043a \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c spring-boot.<\/p>\n<h3>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435<\/h3>\n<ul>\n<li>\n<p><a href=\"#about_keycloak\">\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043e Keycloak<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#run_keycloak\">\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0438 \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c keycloak<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#connect_via_adapter\">\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c Keycloak \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0430\u0434\u0430\u043f\u0442\u0435\u0440\u0430<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#using_oauth_client\">\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c OAuth2 Client \u0438\u0437 spring-security<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#connect_as_resource_server\">\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043a\u0430\u043a ResourceService<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#service_to_service\">\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c keycloak<\/a><\/p>\n<\/li>\n<\/ul>\n<p><a class=\"anchor\" name=\"about_keycloak\" id=\"about_keycloak\"><\/a><\/p>\n<h3>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043e Keycloak<\/h3>\n<p>\u042d\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f SSO (Single sign on) \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u043c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u043c \u043a\u043e\u0434\u043e\u043c \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0435\u0439 \u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u043e\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439.<\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u0439 \u0432 Keycloak:<\/p>\n<ul>\n<li>\n<p>Single-Sign On and Single-Sign Out.<\/p>\n<\/li>\n<li>\n<p>OpenID\/OAuth 2.0\/SAML.<\/p>\n<\/li>\n<li>\n<p>Identity Brokering \u2013 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0432\u043d\u0435\u0448\u043d\u0438\u0445 OpenID Connect \u0438\u043b\u0438 SAML.<\/p>\n<\/li>\n<li>\n<p>Social Login \u2013 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 Google, GitHub, Facebook, Twitter.<\/p>\n<\/li>\n<li>\n<p>User Federation \u2013 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438\u0437 LDAP \u0438 Active Directory \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p>Kerberos bridge \u2013 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 Kerberos \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439.<\/p>\n<\/li>\n<li>\n<p>\u0413\u0438\u0431\u043a\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0430\u043c\u0438 \u0447\u0435\u0440\u0435\u0437 realm.<\/p>\n<\/li>\n<li>\n<p>\u0410\u0434\u0430\u043f\u0442\u0435\u0440\u044b \u0434\u043b\u044f JavaScript, WildFly, JBoss EAP, Fuse, Tomcat, Jetty, Spring.<\/p>\n<\/li>\n<li>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043f\u043b\u0430\u0433\u0438\u043d\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p>\u0418 \u043c\u043d\u043e\u0433\u043e\u0435-\u043c\u043d\u043e\u0433\u043e\u0435 \u0434\u0440\u0443\u0433\u043e\u0435&#8230;<\/p>\n<\/li>\n<\/ul>\n<p><a class=\"anchor\" name=\"run_keycloak\" id=\"run_keycloak\"><\/a><\/p>\n<h3>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0438 \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c keycloak<\/h3>\n<p>\u0414\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 keycloak \u043d\u0430 \u043c\u0430\u0448\u0438\u043d\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0443\u0434\u043e\u0431\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c docker-compose. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432 \u0440\u0430\u0437\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0434\u043b\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u0441\u0435\u0440\u0432\u0438\u0441 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438, \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0438\u0437\u0431\u0430\u0432\u043b\u044f\u044f \u0441\u0435\u0431\u044f \u043e\u0442 \u043a\u0443\u0447\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0435\u0439 \u043f\u043e\u0434 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043e\u0434\u0438\u043d \u0438\u0437 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 docker-compose \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 standalone \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445 postgres:<\/p>\n<details class=\"spoiler\">\n<summary>docker-compose.yml<\/summary>\n<div class=\"spoiler__content\">\n<pre><code>version: \"3.8\"  services:   postgres:     container_name: postgres     image: library\/postgres     environment:       POSTGRES_USER: ${POSTGRES_USER:-postgres}       POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}       POSTGRES_DB: keycloak_db     ports:       - \"5432:5432\"     restart: unless-stopped    keycloak:     image: jboss\/keycloak     container_name: keycloak     environment:       DB_VENDOR: POSTGRES       DB_ADDR: postgres       DB_DATABASE: keycloak_db       DB_USER: ${POSTGRES_USER:-postgres}       DB_PASSWORD: ${POSTGRES_PASSWORD:-postgres}       KEYCLOAK_USER: admin       KEYCLOAK_PASSWORD: admin_password     ports:       - \"8484:8080\"     depends_on:       - postgres     links:       - \"postgres:postgres\"<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 realm, \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432, \u0440\u043e\u043b\u0435\u0439 \u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439.<\/p>\n<p>\u041f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c realm &#171;my_realm&#187;:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0438\u0435\u043d\u0442 <code>\"my_client\"<\/code>, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u044c \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 (\u043e\u0441\u0442\u0430\u0432\u0438\u043c \u0432\u0441\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043f\u043e-\u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e):<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c <code>redirect_url<\/code>. \u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u0435\u043d: http:\/\/localhost:8080\/*<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0440\u043e\u043b\u0438 \u0434\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043d\u0430\u0448\u0435\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b &#8212; <code>\"ADMIN\", \"USER\"<\/code>:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 <code>\"admin\"<\/code> \u0441 \u0440\u043e\u043b\u044c\u044e <code>\"ADMIN\"<\/code>:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0418 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f <code>\"user\"<\/code> \u0441 \u0440\u043e\u043b\u044c\u044e <code>\"USER\"<\/code>. \u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u043f\u0430\u0440\u043e\u043b\u0438 \u043d\u0430 \u0432\u043a\u043b\u0430\u0434\u043a\u0435 <code>\"Credentials\"<\/code>:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0437\u0430\u043a\u043e\u043d\u0447\u0435\u043d\u0430, \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043a \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044e spring boot \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439.<\/p>\n<p><a class=\"anchor\" name=\"connect_via_adapter\" id=\"connect_via_adapter\"><\/a><\/p>\n<h3>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c Keycloak \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0430\u0434\u0430\u043f\u0442\u0435\u0440\u0430<\/h3>\n<p>\u0412 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043a keycloak \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0433\u043e\u0442\u043e\u0432\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 &#8212; \u0430\u0434\u0430\u043f\u0442\u0435\u0440\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u0430\u044e\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 boilerplate \u043a\u043e\u0434\u0430 \u0438 \u0438\u0437\u043b\u0438\u0448\u043d\u0435\u0433\u043e \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0415\u0441\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0430 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u043e\u0432 \u0438 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u043e\u0432 (<a href=\"https:\/\/www.keycloak.org\/docs\/latest\/securing_apps\/index.html#supported-platforms\"><u>supported-platforms<\/u><\/a>). \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Spring Boot Adapter.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0435, \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 spring-boot (\u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 <a href=\"https:\/\/github.com\/kazakovav\/spring-boot-keycloak\/tree\/main\/demo-keycloak-adapter\">\u0437\u0434\u0435\u0441\u044c<\/a>) \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u043a \u043d\u0435\u043c\u0443 Keycloak Spring Boot \u0430\u0434\u0430\u043f\u0442\u0435\u0440. \u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b maven \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<details class=\"spoiler\">\n<summary>pom.xml<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt; &lt;project xmlns=\"&lt;http:\/\/maven.apache.org\/POM\/4.0.0&gt;\" xmlns:xsi=\"&lt;http:\/\/www.w3.org\/2001\/XMLSchema-instance&gt;\" xsi:schemaLocation=\"&lt;http:\/\/maven.apache.org\/POM\/4.0.0&gt; &lt;https:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd&gt;\"&gt; \t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt; \t&lt;parent&gt; \t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t&lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt; \t\t&lt;version&gt;2.3.9.RELEASE&lt;\/version&gt; \t\t&lt;relativePath \/&gt; &lt;!-- lookup parent from repository --&gt; \t&lt;\/parent&gt; \t&lt;groupId&gt;org.akazakov.keycloak&lt;\/groupId&gt; \t&lt;artifactId&gt;demo-keycloak-adapter&lt;\/artifactId&gt; \t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt; \t&lt;name&gt;Demo Keycloak Adapter&lt;\/name&gt; \t&lt;description&gt;Demo project for Spring Boot and Keycloak&lt;\/description&gt; \t&lt;properties&gt; \t\t&lt;java.version&gt;11&lt;\/java.version&gt; \t&lt;\/properties&gt; \t&lt;dependencyManagement&gt; \t\t&lt;dependencies&gt; \t\t\t&lt;dependency&gt; \t\t\t\t&lt;groupId&gt;org.keycloak.bom&lt;\/groupId&gt; \t\t\t\t&lt;artifactId&gt;keycloak-adapter-bom&lt;\/artifactId&gt; \t\t\t\t&lt;version&gt;12.0.3&lt;\/version&gt; \t\t\t\t&lt;type&gt;pom&lt;\/type&gt; \t\t\t\t&lt;scope&gt;import&lt;\/scope&gt; \t\t\t&lt;\/dependency&gt; \t\t&lt;\/dependencies&gt; \t&lt;\/dependencyManagement&gt; \t&lt;dependencies&gt; \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-boot-starter-security&lt;\/artifactId&gt; \t\t&lt;\/dependency&gt; \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt; \t\t&lt;\/dependency&gt; \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.keycloak&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;keycloak-spring-boot-starter&lt;\/artifactId&gt; \t\t&lt;\/dependency&gt; \t\t  \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-boot-starter-test&lt;\/artifactId&gt; \t\t\t&lt;scope&gt;test&lt;\/scope&gt; \t\t&lt;\/dependency&gt; \t\t&lt;dependency&gt; \t\t\t&lt;groupId&gt;org.springframework.security&lt;\/groupId&gt; \t\t\t&lt;artifactId&gt;spring-security-test&lt;\/artifactId&gt; \t\t\t&lt;scope&gt;test&lt;\/scope&gt; \t\t&lt;\/dependency&gt;  \t&lt;\/dependencies&gt; &lt;\/project&gt;<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u0414\u043b\u044f \u0446\u0435\u043b\u0435\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0440\u043e\u043b\u0435\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435 (\u044d\u0442\u043e\u0442 \u0436\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u043d\u0438\u0436\u0435):<\/p>\n<pre><code class=\"java\">@RestController @RequestMapping(\"\/api\") public class SampleController {      @GetMapping(\"\/anonymous\")     public String getAnonymousInfo() {         return \"Anonymous\";     }      @GetMapping(\"\/user\")     @PreAuthorize(\"hasRole('USER')\")     public String getUserInfo() {         return \"user info\";     }      @GetMapping(\"\/admin\")     @PreAuthorize(\"hasRole('ADMIN')\")     public String getAdminInfo() {         return \"admin info\";     }      @GetMapping(\"\/service\")     @PreAuthorize(\"hasRole('SERVICE')\")     public String getServiceInfo() {         return \"service info\";     }      @GetMapping(\"\/me\")     public Object getMe() {         final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();         return authentication.getName();     } } <\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043b\u043e\u0441\u044c \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043b\u043e\u0441\u044c \u043a keycloak, \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e. \u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043c\u044b \u0441\u0434\u0435\u043b\u0430\u0435\u043c, \u044d\u0442\u043e \u0432 application.yml \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<pre><code>server:   port: ${SERVER_PORT:8080} spring:   application.name: ${APPLICATION_NAME:spring-security-keycloak} keycloak:   auth-server-url: http:\/\/localhost:8484\/auth   realm: my_realm   resource: my_client   public-client: true<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e spring-security, \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c <code>KeycloakWebSecurityConfigurerAdapter<\/code>, \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0439 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0430\u0434\u0430\u043f\u0442\u0435\u0440\u043e\u043c:<\/p>\n<pre><code class=\"java\">@KeycloakConfiguration @EnableGlobalMethodSecurity(prePostEnabled = true) public class WebSecurityConfig extends KeycloakWebSecurityConfigurerAdapter {      @Override     protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {         return new NullAuthenticatedSessionStrategy();     }      @Autowired     public void configureGlobal(AuthenticationManagerBuilder authManagerBuilder) {         KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();         keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());         authManagerBuilder.authenticationProvider(keycloakAuthenticationProvider);     }      @Bean     public KeycloakConfigResolver keycloakConfigResolver() {         return new KeycloakSpringBootConfigResolver();     }      @Override     protected void configure(HttpSecurity http) throws Exception {         super.configure(http);         http                 .authorizeRequests()                 .antMatchers(\"\/api\/anonymous\/**\").permitAll()                 .anyRequest().fullyAuthenticated();     } }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0440\u0430\u0431\u043e\u0442\u0443 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0417\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0437\u0430\u0439\u0442\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c \u043d\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 url. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440: <code>http:\/\/localhost:8080\/api\/admin<\/code>. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435, \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u0438\u0442 \u043d\u0430\u0441 \u043d\u0430 \u043e\u043a\u043d\u043e \u043b\u043e\u0433\u0438\u043d\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0432\u0432\u043e\u0434\u0430 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0433\u043e \u0438\u043c\u0435\u043d\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u043f\u0430\u0440\u043e\u043b\u044f, \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u0438\u0442 \u043d\u0430\u0441 \u043d\u0430 \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u0430\u0434\u0440\u0435\u0441. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0415\u0441\u043b\u0438 \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435 (<code>http:\/\/localhost:8080\/api\/me<\/code>), \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 uuid \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432 keycloak:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u0435\u0440\u0432\u0438\u0441 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u043b \u0442\u043e\u043a\u0435\u043d \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u0438 \u043d\u0435 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0443 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c <code>bearer-only: true<\/code> \u0432 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f:<\/p>\n<pre><code>keycloak:   auth-server-url: http:\/\/localhost:8484\/auth   realm: my_realm   resource: my_client   public-client: true   bearer-only: true<\/code><\/pre>\n<p><a class=\"anchor\" name=\"using_oauth_client\" id=\"using_oauth_client\"><\/a><\/p>\n<h3>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c OAuth2 Client \u0438\u0437 spring-security<\/h3>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 keycloak \u0430\u0434\u0430\u043f\u0442\u0435\u0440\u0430 \u0438\u0437\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0430\u0441 \u043e\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043a\u0443\u0447\u0438 boilerplate \u043a\u043e\u0434\u0430. \u041d\u043e \u0432 \u0442\u043e \u0436\u0435 \u0432\u0440\u0435\u043c\u044f \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u043c \u043e\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u0412 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043d\u0435 \u0441\u0442\u043e\u0438\u0442 \u0437\u0430\u0432\u044f\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u0441\u0435\u0440\u0432\u0438\u0441 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438, \u044d\u0442\u043e \u0434\u0430\u0441\u0442 \u043d\u0430\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0433\u0438\u0431\u043a\u043e\u0441\u0442\u0438 \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0439 \u044d\u043a\u0441\u043f\u043b\u0443\u0430\u0442\u0430\u0446\u0438\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u044b.<\/p>\n<p>\u041e\u0434\u043d\u043e\u0439 \u0438\u0437 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439 spring security 5 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u043e\u0432 OAuth2 \u0438 OIDC. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c OAuth2 \u043a\u043b\u0438\u0435\u043d\u0442 \u0438\u0437 \u043f\u0430\u043a\u0435\u0442\u0430 spring-security \u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0438 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c keycloak.<\/p>\n<p>\u0418\u0442\u0430\u043a, \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 (<a href=\"https:\/\/github.com\/kazakovav\/spring-boot-keycloak\/tree\/main\/demo-keycloak-oauth\">\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u0440\u0438\u043c\u0435\u0440\u0430<\/a>). \u041f\u043e\u043b\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442 <code>pom.xml<\/code>:<\/p>\n<details class=\"spoiler\">\n<summary>pom.xml<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"xml\">&lt;?xml version=\"1.0\"<\/code><\/pre>\n<\/div>\n<\/details>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-321443","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/321443","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=321443"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/321443\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=321443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=321443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=321443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}