{"id":261224,"date":"2015-07-12T19:14:02","date_gmt":"2015-07-12T15:14:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=261224"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=261224","title":{"rendered":"Spring + Java EE + Persistence, \u0431\u0435\u0437 XML. \u0427\u0430\u0441\u0442\u044c 2"},"content":{"rendered":"<p>     \t\u0418 \u0441\u043d\u043e\u0432\u0430 \u0434\u0435\u043d\u044c \u0434\u043e\u0431\u0440\u044b\u0439.<br \/>  \u041f\u043e\u0441\u0442 \u0432 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 <a href=\"http:\/\/habrahabr.ru\/post\/262323\/\">Spring + Java EE + Persistence, \u0431\u0435\u0437 XML. \u0427\u0430\u0441\u0442\u044c 1<\/a><\/p>\n<p>  <a name=\"habracut\"><\/a>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435<\/b><\/p>\n<div class=\"spoiler_text\">\n<h3>1. \u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h3>\n<p>  <\/p>\n<h4>1.1 \u041f\u043e\u0434\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442<\/h4>\n<p>  <\/p>\n<h4>1.2 \u0427\u0442\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u0432 \u044d\u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438?<\/h4>\n<p>  <\/p>\n<h3>2. \u0424\u0438\u043a\u0441\u0438\u043c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0440\u043e\u043b\u0435\u0439 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438<\/h3>\n<p>  <\/p>\n<h4>2.1 \u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0431\u0430\u0437\u043e\u0439<\/h4>\n<p>  <\/p>\n<h4>2.2 \u041f\u0438\u0448\u0435\u043c \u043a\u043e\u0434<\/h4>\n<p>  <\/p>\n<h3>3. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 UsersController<\/h3>\n<p>  <\/p>\n<h4>3.1 \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f<\/h4>\n<p>  <\/p>\n<h4>3.2 \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c<\/h4>\n<p>  <\/p>\n<h3>4. \u0414\u043b\u044f \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0445 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442<\/h3>\n<p>  <\/p>\n<h3>5 \u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<h3><a name=\"1\"><\/a>1. \u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h3>\n<p>  <\/p>\n<h4><a name=\"11\"><\/a>1.1 \u041f\u043e\u0434\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442<\/h4>\n<p>  \u0415\u0441\u043b\u0438 \u0412\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0441 \u044d\u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u043d\u0430\u0447\u0430\u0442\u044c, \u043b\u0438\u0431\u043e \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0441\u0434\u0435\u043b\u0430\u043d\u043d\u043e\u0433\u043e \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438, \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u0435\u0433\u043e \u0441 github.<br \/>  \u0421\u0445\u0435\u043c\u0430 \u043f\u0440\u043e\u0441\u0442\u0430\u044f:  <\/p>\n<ul>\n<li> \u0417\u0430\u0445\u043e\u0434\u0438\u0442\u0435 \u0438\u0437 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u0432 \u043f\u0430\u043f\u043a\u0443 \u0441 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u043c\u0438 IDEA<\/li>\n<li> git clone <a href=\"https:\/\/github.com\/MaxPovver\/ForHabrahabr.git\">github.com\/MaxPovver\/ForHabrahabr.git<\/a><\/li>\n<li> cd ForHabrahabr\/<\/li>\n<li> git checkout withauth<\/li>\n<li> \u0413\u043e\u0442\u043e\u0432\u043e, \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u0435\u0442\u0435 \u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442 \u0432 \u0441\u0442\u0443\u0434\u0438\u044e \u0442\u0430\u043a \u0436\u0435 \u043a\u0430\u043a \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0432 \u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438.<\/li>\n<\/ul>\n<p>  <\/p>\n<h4><a name=\"12\"><\/a>1.2 \u0427\u0442\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u0432 \u044d\u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438?<\/h4>\n<p>  \u0412 \u044d\u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \u043c\u043d\u043e\u0433\u0438\u0435-\u043a\u043e-\u043c\u043d\u043e\u0433\u0438\u043c \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439;<br \/>  \u0434\u043e\u0434\u0435\u043b\u0430\u0435\u043c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0440\u0430\u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c;<br \/>  \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u0439 REST-controller;<br \/>  \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044e \u043d\u043e\u0432\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439(\u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0430\u0434\u043c\u0438\u043d\u0430);<br \/>  \u0438 \u0432\u0441\u0435 \u044d\u0442\u043e \u0431\u0435\u0437 XML \ud83d\ude42<\/p>\n<h3><a name=\"2\"><\/a>2 \u0424\u0438\u043a\u0441\u0438\u043c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0440\u043e\u043b\u0435\u0439 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438<\/h3>\n<p>  \u041a\u0430\u043a \u043a\u0442\u043e-\u0442\u043e \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0437\u0430\u043c\u0435\u0442\u0438\u043b, \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0443 \u043d\u0430\u0441 \u0432\u043c\u0435\u0441\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043f\u0440\u0430\u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438\u0437 \u0431\u0430\u0437\u044b \u043f\u0440\u0438\u0434\u0435\u043b\u0430\u043d\u0430 \u0436\u0443\u0442\u043a\u043e\u0432\u0430\u0442\u043e\u0433\u043e \u0432\u0438\u0434\u0430 \u0437\u0430\u0433\u043b\u0443\u0448\u043a\u0430, \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u043d\u0435 \u0433\u0438\u0431\u043a\u0430\u044f \u043f\u0440\u0438\u0447\u0435\u043c.<br \/>  \u0427\u0442\u043e \u0436\u0435 \u043d\u0443\u0436\u043d\u043e \u0447\u0442\u043e\u0431\u044b \u044d\u0442\u0430 \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c? \u0412\u0432\u0435\u0441\u0442\u0438 \u0442\u0430\u043a\u0443\u044e \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u043a\u0430\u043a \u00ab\u0440\u043e\u043b\u044c\u00bb. \u0423 \u043e\u0434\u043d\u043e\u0433\u043e \u044e\u0437\u0435\u0440\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u043e\u043b\u0435\u0439, \u043f\u0440\u0438\u0447\u0435\u043c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u043e\u0434\u043d\u0443 \u0438 \u0442\u0443 \u0436\u0435 \u0440\u043e\u043b\u044c. \u0422.\u0435. \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u0435 <b>Many-To-Many<\/b>.  <\/p>\n<h4><a name=\"21\"><\/a>2.1 \u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0431\u0430\u0437\u043e\u0439<\/h4>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0437\u0430\u0432\u0435\u0434\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0447\u043a\u0443 roles (id, role), \u043d\u0435 \u0437\u0430\u0431\u044b\u0432 \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0447\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 role \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u043c\u0438. <br \/>  \u0422\u0430\u043a\u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 users_roles(user_id, role_id).<br \/>  \u0418 \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0440\u043e\u043b\u0438 ADMIN, USER, GUEST.<br \/>  \u041d\u0443 \u0438 \u0441\u0440\u0430\u0437\u0443 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0434\u043b\u044f \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0432\u043d\u0435\u0448\u043d\u0438\u0435 \u043a\u043b\u044e\u0447\u0438 user_id -&gt; user.id, role_id -&gt; role.id <br \/>  \u0412\u0441\u0435 \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0440\u0430\u0437\u0443, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0432 \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u0441\u043a\u0440\u0438\u043f\u0442:   <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \ud83d\ude42<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\"># \u0414\u0430\u043c\u043f \u0442\u0430\u0431\u043b\u0438\u0446\u044b roles # ------------------------------------------------------------  DROP TABLE IF EXISTS `roles`;  CREATE TABLE `roles` (   `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,   `role` varchar(250) DEFAULT NULL,   PRIMARY KEY (`id`),   UNIQUE KEY `id` (`id`),   UNIQUE KEY `role` (`role`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;   INSERT INTO `roles` (`id`, `role`) VALUES \t(1,'ADMIN'), \t(3,'GUEST'), \t(2,'USER');  # \u0414\u0430\u043c\u043f \u0442\u0430\u0431\u043b\u0438\u0446\u044b users # ------------------------------------------------------------  DROP TABLE IF EXISTS `users`;  CREATE TABLE `users` (   `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,   `username` varchar(250) DEFAULT NULL,   `password` varchar(250) DEFAULT NULL,   PRIMARY KEY (`id`),   UNIQUE KEY `id` (`id`),   UNIQUE KEY `username` (`username`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  INSERT INTO `users` (`id`, `username`, `password`) VALUES \t(1,'user','user');  # \u0414\u0430\u043c\u043f \u0442\u0430\u0431\u043b\u0438\u0446\u044b users_roles # ------------------------------------------------------------  DROP TABLE IF EXISTS `users_roles`;  CREATE TABLE `users_roles` (   `user_id` bigint(20) unsigned DEFAULT NULL,   `role_id` bigint(20) unsigned DEFAULT NULL,   KEY `hasuser` (`user_id`),   KEY `hasrole` (`role_id`),   CONSTRAINT `hasrole` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,   CONSTRAINT `hasuser` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<h4><a name=\"22\"><\/a>2.2 \u041f\u0438\u0448\u0435\u043c \u043a\u043e\u0434<\/h4>\n<p>  \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0437\u0430\u0439\u0434\u0435\u043c Application.class \u0438 \u0443\u0442\u043e\u0447\u043d\u0438\u043c \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 Jpa \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u0432:  <\/p>\n<pre><code class=\"java\">@EnableJpaRepositories(basePackages = {&quot;habraspring.repositories&quot;}) <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0432 entities\/ \u043a\u043b\u0430\u0441\u0441 Role \u0437\u0430\u043c\u0430\u043f\u043b\u0435\u043d\u043d\u044b\u0439 \u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u0431\u0430\u0437\u0435:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">Role.java<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"java\">@Entity @Table(name=&quot;roles&quot;) public class Role {     @Id     @GeneratedValue(strategy = GenerationType.AUTO)     private long id;     private String role;      public long getId() {         return id;     }      public void setId(long id) {         this.id = id;     }      public String getRole() {         return role;     }      public void setRole(String role) {         this.role = role;     }      protected Role(){}     public Role(String name)     {         role = name;     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041e\u043a, \u0430 \u043a\u0430\u043a \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u0432\u0437\u0430\u0442\u044c \u0438\u0445 \u0441 \u043a\u043b\u0430\u0441\u0441\u043e\u043c User? \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 User \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434:  <\/p>\n<pre><code class=\"java\">    @ManyToMany     @JoinTable(name = &quot;users_roles&quot;,             joinColumns = {@JoinColumn(name = &quot;user_id&quot;)},             inverseJoinColumns = @JoinColumn(name = &quot;role_id&quot;))     private Set&lt;Role&gt; roles;      public Set&lt;Role&gt; getRoles() {         return roles;     }      public void setRoles(Set&lt;Role&gt; roles) {         this.roles = roles;     } <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0441\u0432\u044f\u0437\u0438 \u0434\u0432\u0443\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439, \u043a\u0430\u043a\u0430\u044f \u043a\u043e\u043b\u043e\u043d\u043a\u0430 \u0432 \u044d\u0442\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0430\u0448\u0435\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438( joinColumns = { @JoinColumn(name = \u00abuser_id\u00bb)}), \u0430 \u043a\u0430\u043a\u0430\u044f \u2014 \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u043c\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438: inverseJoinColumns =  @JoinColumn(name = \u00abrole_id\u00bb).<\/p>\n<p>  \u0412 \u043a\u043b\u0430\u0441\u0441\u0435 Role \u0432\u0441\u0435 \u043f\u0440\u043e\u0449\u0435:  <\/p>\n<pre><code class=\"java\">    @ManyToMany(mappedBy = &quot;roles&quot;)     Set&lt;User&gt; users;      public Set&lt;User&gt; getUsers() {         return users;     }      public void setUsers(Set&lt;User&gt; users) {         this.users = users;     } <\/code><\/pre>\n<p>  \u0427\u0442\u043e\u0431\u044b Spring \u0443\u0441\u0442\u0440\u043e\u0438\u043b\u0430 \u043d\u0430\u0448\u0430 \u0440\u043e\u043b\u044c \u043a\u0430\u043a Authorithy, \u043d\u0430\u0434\u043e \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 Role \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 GrantedAuthority:  <\/p>\n<pre><code class=\"java\">public class Role implements GrantedAuthority { ...      @Override     public String getAuthority() {         return getRole();     } } <\/code><\/pre>\n<p>  \u0413\u043e\u0442\u043e\u0432\u043e! \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c MySQLUserDetailsService:  <\/p>\n<pre><code class=\"java\">@Service public class MySQLUserDetailsService implements UserDetailsService {     @Autowired     UsersRepository users;     @Override     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {         UserDetails loadedUser;          try {             User client = users.findByUsername(username);             loadedUser = new org.springframework.security.core.userdetails.User(                     client.getUsername(), client.getPassword(),                     client.getRoles());         } catch (Exception repositoryProblem) {             throw new InternalAuthenticationServiceException(repositoryProblem.getMessage(), repositoryProblem);         }         return loadedUser;     } } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0433\u0440\u0443\u0437\u0438\u043c authorities \u0447\u0435\u0440\u0435\u0437 user.getRoles(), \u0430 \u043d\u0435 \u043c\u0443\u0441\u043e\u0440\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441, \u0442\u0430\u043a \u0447\u0442\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435 \u0440\u043e\u043b\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438\u0441\u0432\u043e\u0435\u043d\u044b \u0435\u043c\u0443 \u0432 \u0431\u0430\u0437\u0435.<br \/>  \u041d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043c\u044b \u044d\u0442\u043e \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c, \u043d\u043e \u0447\u0443\u0442\u044c \u043f\u043e\u0437\u0436\u0435 \u0432\u044b \u0443\u0432\u0438\u0434\u0438\u0442\u0435, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0440\u043e\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.<\/p>\n<p>  \u0418\u0442\u0430\u043a, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u0438\u0439 rest controller \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0439 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0441 \u043f\u0440\u0430\u0432\u0430\u043c\u0438 \u0430\u0434\u043c\u0438\u043d\u0430.<\/p>\n<h3><a name=\"3\"><\/a>3. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 UsersController<\/h3>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0430\u043f\u043a\u0443 controllers, \u0430 \u0432 \u043d\u0435\u0439 \u2014 UsersContoller  <\/p>\n<pre><code class=\"java\">package habraspring.controllers;  import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;  @RestController @RequestMapping(&quot;\/users&quot;) public class UsersController { } <\/code><\/pre>\n<p>  \u0418\u0442\u0430\u043a, \u0432\u043e \u043f\u0435\u0440\u0432\u044b\u0445 \u043c\u044b \u043e\u0442\u043c\u0435\u0442\u0438\u043b\u0438 \u0447\u0442\u043e \u044d\u0442\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 rest, \u0447\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442 \u0447\u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u043d\u0435 \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u044b\u0435 html \u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043a\u0438, \u0430 \u0441\u044b\u0440\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 Json.  @RequestMapping(&quot;\/users&quot;) \u2014 \u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0441\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u043d\u0430 \u0437\u043f\u0440\u043e\u0441 \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432\u0438\u0434\u0430 \u00ab<a href=\"http:\/\/yoursite\/users\">yoursite\/users<\/a>\u00bb. \u041d\u043e \u043c\u044b \u0442\u0443\u0442 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c\u0441\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c\u0438 \u0440\u0443\u043b\u0438\u0442\u044c, \u0432 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a \u043e\u0442\u043a\u0440\u044b\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u043c\u043e\u0436\u0435\u0442 \u043b\u044e\u0431\u043e\u0439 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u043b\u044c! \u0422\u0430\u043a \u0447\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u043e\u043b\u0448\u0435\u0431\u043d\u0443\u044e \u0441\u0442\u0440\u043e\u0447\u043a\u0443:  <\/p>\n<pre><code class=\"java\">@PreAuthorize(&quot;hasRole('ADMIN')&quot;) <\/code><\/pre>\n<p>  \u0412\u043d\u0443\u0442\u0440\u0438 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043f\u0440\u0430\u0432 \u0442\u0435\u043f\u0435\u0440\u044c \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u0430, \u0442\u0443\u0434\u0430 \u043f\u043e\u043f\u0430\u0441\u0442\u044c \u0441\u043c\u043e\u0436\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0442 \u0443 \u043a\u043e\u0433\u043e \u0443\u0436\u0435 \u0440\u043e\u043b\u044c ADMIN \u0438\u043c\u0435\u0435\u0442\u0441\u044f.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432\u044b\u0432\u043e\u0434 \u0432\u0441\u0435\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439:  <\/p>\n<pre><code class=\"java\">    @Autowired     UsersRepository users;      @RequestMapping(method = RequestMethod.GET)     public List&lt;User&gt; getUsers()     {         List&lt;User&gt; result = new ArrayList&lt;&gt;();         users.findAll().forEach(result::add);         return result;     } <\/code><\/pre>\n<p>  \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0439\u0442\u0435 \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 <a href=\"http:\/\/localhost:8080\/users\">http:\/\/localhost:8080\/users<\/a> \u0435\u0441\u043b\u0438 \u0432\u0441\u0435 \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e, \u0431\u0443\u0434\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0430 403. \u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0430\u0434\u043c\u0438\u043d\u0441\u043a\u0438\u0435 \u043f\u0440\u0430\u0432\u0430, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u0434\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 users_roles \u0437\u0430\u043f\u0438\u0441\u044c (1,1), \u0435\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0442\u0430\u043a\u0438\u0435 \u0436\u0435 id \u044e\u0437\u0435\u0440\u0430 \u0438 \u0440\u043e\u043b\u0438 ADMIN \u043a\u0430\u043a \u0443 \u043c\u0435\u043d\u044f. \u041f\u043e\u0441\u043b\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0438\u0434\u0435\u043c \u0432 <a href=\"http:\/\/localhost:8080\/secret\">http:\/\/localhost:8080\/secret<\/a> \u0438 \u0436\u043c\u0435\u043c \u0442\u0430\u043c logout \u0447\u0442\u043e\u0431\u044b \u043f\u0435\u0440\u0435\u0437\u0430\u0439\u0442\u0438 \u0437\u0430\u043d\u043e\u0432\u043e(\u0434\u043b\u044f \u043f\u043e\u0434\u0433\u0440\u0443\u0437\u043a\u0438 \u043d\u043e\u0432\u044b\u0445 \u043f\u0440\u0430\u0432). \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043e\u0442\u043a\u0440\u044b\u0442\u044c <a href=\"http:\/\/localhost:8080\/users\">http:\/\/localhost:8080\/users<\/a>. \u0414\u043e\u043b\u0436\u043d\u043e \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0447\u0442\u043e-\u0442\u043e \u0442\u0430\u043a\u043e\u0435:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041c\u043d\u043e\u0433\u043e \u0431\u0443\u043a\u0432<\/b><\/p>\n<div class=\"spoiler_text\">[{\u00abid\u00bb:1,\u00abusername\u00bb:\u00abuser\u00bb,\u00abpassword\u00bb:\u00abuser\u00bb,\u00abroles\u00bb:[{\u00abid\u00bb:1,\u00abrole\u00bb:\u00abADMIN\u00bb,\u00abusers\u00bb:[{\u00abid\u00bb:1,\u00abusername\u00bb:\u00abuser\u00bb,\u00abpassword\u00bb:\u00abuser\u00bb,\u00abroles\u00bb:[{\u00abid\u00bb:1,\u00abrole\u00bb:\u00abADMIN\u00bb,\u00abusers\u00bb:[{\u00abid\u00bb:1,\u00abusername\u00bb:\u00abuser\u00bb,\u00abpassword\u00bb:\u00abuser\u00bb,\u00abroles\u00bb:[{\u00abid\u00bb:1,\u00abrole\u00bb:\u00abADMIN\u00bb,\u00abusers\u00bb:[{\u00abid\u00bb:1,\u00abusername\u00bb:\u00abuser\u00bb,\u00abpassword\u00bb:\u00abuser\u00bb,\u00abroles\u00bb:[{\u00abid\u00bb:1,\u00abrole\u00bb:\u00abADMIN\u00bb,\u00abusers\u00bb:[{\u00abid\u00bb:1,\u00abusername\u00bb:\u00abuser\u00bb,\u00abpassword\u00bb:\u00abuser\u00bb,\u00abroles\u00bb:[{\u00abid\u00bb:1,\u00abrole\u00bb:\u00abADMIN\u00bb,\u00abusers\u00bb:[{\u00abid\u00bb:1,\u00abusername\u00bb:\u00abuser\u00bb,\u00abpassword\u00bb:\u00abuser\u00bb,\u00abroles\u00bb:[{\u00abid\u00bb:1,\u00abrole\u00bb:\u00abADMIN\u00bb,\u00abusers\u00bb:[{\u00abid\u00bb:1,\u00abusername\u00bb:\u00abuser\u00bb,\u00abpassword\u00bb:\u00abuser\u00bb,\u00abroles\u00bb:[{\u00abid\u00bb:1,\u00abrole\u00bb:\u00abADMIN\u00bb,\u00abusers\u00bb:[{\u00abid\u00bb:1,\u00abusername\u00bb:\u00abuser\u00bb,\u00abpassword\u00bb:\u00abuser\u00bb,\u00abroles\u00bb:[{\u00abid\u00bb:1,\u00abrole\u00bb:\u00abADMIN\u00bb,\u00abusers\u00bb:[{\u00abid\u00bb:1,\u00abusername\u00bb:\u00abuser\u00bb,\u00abpassword\u00bb:\u00abuser\u00bb,\u00abroles\u00bb:[{\u00abid\u00bb:1,\u00abrole\u00bb:\u00abADMIN\u00bb,\u00abusers\u00bb:[{\u00abid\u00bb:1,\u00abusername\u00bb:\u00abuser\u00bb,\u00abpassword\u00bb:\u00abuser\u00bb,\u00abroles\u00bb:[{\u00abid\u00bb:1,\u00abrole\u00bb:\u00abADMIN\u00bb,\u00abusers\u00bb:[{\u00abid\u00bb:1,\u00abusername\u00bb:\u00abuser\u00bb,\u00abpassword\u00bb:\u00abuser\u00bb,\u00abroles\u00bb:[{\u00abid\u00bb:1,\u00abrole\u00bb:\u00abADMIN\u00bb,\u00abusers\u00bb:[{\u00abid\u00bb:1,\u00abusername\u00bb:\u00abuser\u00bb,\u00abpassword\u00bb:\u00abuser\u00bb,\u00abroles\u00bb:[{\u00abid\u00bb:1,\u00abrole\u00bb:\u00abADMIN\u00bb,\u00abusers\u00bb:[{\u00abid\u00bb:1,\u00abusername\u00bb:\u00abuser\u00bb,\u00abpassword\u00bb:\u00abuser\u00bb,\u00abroles\u00bb:[{\u00abid\u00bb:1,\u00abrole\u00bb:\u00abADMIN\u00bb,\u00abusers\u00bb:[{\u00abid\u00bb:1,\u00abusername\u00bb:\u00abuser\u00bb,\u00abpassword\u00bb:\u00abuser\u00bb,\u00abroles\u00bb:[{\u00abid\u00bb:1,\u00abrole\u00bb:\u00abADMIN\u00bb,\u00abusers\u00bb:[{\u00abid\u00bb:1,\u00abusername\u00bb:\u00abuser\u00bb,\u00abpassword\u00bb:\u00abuser\u00bb,\u00abroles\u00bb:[{\u00abid\u00bb:1,\u00abrole\u00bb:\u00abADMIN\u00bb,\u00abusers\u00bb:  <\/div>\n<\/div>\n<p>  \u0412 \u0447\u0435\u043c \u0436\u0435 \u0434\u0435\u043b\u043e, \u0443 \u043d\u0430 \u0432\u0435\u0434\u044c \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u0438\u043d \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c? \u0422\u0443\u0442 \u0432\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u2014 \u0432 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u0432\u044b\u0432\u043e\u0434\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043a\u0430\u043a json \u0432\u044b\u0432\u043e\u0434\u044f\u0442\u0441\u044f \u0438 \u0432\u0441\u0435 \u0435\u0433\u043e \u043f\u043e\u043b\u044f, \u0432 \u0438\u0442\u043e\u0433\u0435 \u0435\u0441\u043b\u0438 \u0432 \u043e\u0434\u043d\u043e\u043c \u0438\u0437 \u043d\u0438\u0445 \u0435\u0441\u0442\u044c \u043e\u043d \u0436\u0435, \u044d\u0442\u043e \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0446\u0438\u043a\u043b. \u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0434\u043e\u0441\u0430\u0434\u043d\u0443\u044e \u043e\u043f\u043b\u043e\u0448\u043d\u043e\u0441\u0442\u044c, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u043e\u043b\u044e users \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 Role \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e JsonIgnore  <\/p>\n<pre><code class=\"java\">import com.fasterxml.jackson.annotation.JsonIgnore; ...     @JsonIgnore     @ManyToMany(mappedBy = &quot;roles&quot;)     Set&lt;User&gt; users; <\/code><\/pre>\n<p>  \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043f\u0435\u0440\u0435\u0437\u0430\u0445\u043e\u0434\u0438\u043c \u0432 <a href=\"http:\/\/localhost:8080\/users\">http:\/\/localhost:8080\/users<\/a> \u0438 \u0432\u0438\u0434\u0438\u043c \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0432\u044b\u0432\u043e\u0434:  <\/p>\n<pre><code class=\"javascript\">[{&quot;id&quot;:1,&quot;username&quot;:&quot;user&quot;,&quot;password&quot;:&quot;user&quot;,&quot;roles&quot;:[{&quot;id&quot;:1,&quot;role&quot;:&quot;ADMIN&quot;,&quot;authority&quot;:&quot;ADMIN&quot;}]}] <\/code><\/pre>\n<p>  \u041e\u043a, \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0435\u0449\u0435 \u043f\u0430\u0440\u043e\u0447\u043a\u0443 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0438 \u00abuser-friendly\u00bb \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439.<\/p>\n<h4><a name=\"31\"><\/a>3.1 \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f<\/h4>\n<p>  \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043c\u0435\u0442\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e POST \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u0434\u043e\u0431\u0430\u0432\u0438\u0442 \u043d\u043e\u0432\u0443\u044e \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c:  <\/p>\n<pre><code class=\"java\">     @RequestMapping(method = RequestMethod.POST)     public User addUser(String username, String password, String password_confirm)     {         \/\/no empty fields allowed         if (username.isEmpty() || password.isEmpty() || password_confirm.isEmpty())             return null;         \/\/passwords should match         if (!password.equals(password_confirm))             return null;         return users.save(new User(username, password));     } <\/code><\/pre>\n<p>  \u0423\u0436\u0435 \u043d\u0435\u043f\u043b\u043e\u0445\u043e, \u0438\u0437 \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0433\u043e api \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c \u044e\u0437\u0435\u0440\u043e\u0432 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c, \u0430 \u043a\u0430\u043a \u0432 \u0441\u0430\u043c\u043e\u043c \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c?<br \/>  \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0440\u0448\u0440\u0443\u0442 \/add, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0443 GET \/users\/add:<\/p>\n<pre><code class=\"java\">    @RequestMapping(value = &quot;\/add&quot;,method = RequestMethod.GET)     public ModelAndView getUserForm()     {         return new ModelAndView(&quot;add&quot;);     } <\/code><\/pre>\n<p>  \u0438 \u0432 resources\/templates\/ \u0434\u043e\u0431\u0430\u0432\u0438\u043c add.html:  <\/p>\n<pre><code class=\"html\">&lt;!DOCTYPE html&gt; &lt;html xmlns=&quot;http:\/\/www.w3.org\/1999\/xhtml&quot; xmlns:th=&quot;http:\/\/www.thymeleaf.org&quot;       xmlns:sec=&quot;http:\/\/www.thymeleaf.org\/thymeleaf-extras-springsecurity3&quot;&gt; &lt;head&gt;     &lt;title&gt;Add user page&lt;\/title&gt; &lt;\/head&gt; &lt;body&gt; &lt;form th:action=&quot;@{\/users}&quot; method=&quot;post&quot;&gt;     &lt;div&gt;&lt;label&gt; User Name : &lt;input type=&quot;text&quot; name=&quot;username&quot;\/&gt; &lt;\/label&gt;&lt;\/div&gt;     &lt;div&gt;&lt;label&gt; Password: &lt;input type=&quot;password&quot; name=&quot;password&quot;\/&gt; &lt;\/label&gt;&lt;\/div&gt;     &lt;div&gt;&lt;label&gt; Password confirm: &lt;input type=&quot;password&quot; name=&quot;password_confirm&quot;\/&gt; &lt;\/label&gt;&lt;\/div&gt;     &lt;div&gt;&lt;input type=&quot;submit&quot; value=&quot;Sign In&quot;\/&gt;&lt;\/div&gt; &lt;\/form&gt; &lt;\/body&gt; &lt;\/html&gt; <\/code><\/pre>\n<p>  \u041f\u043e\u043b\u044f \u0444\u043e\u0440\u043c\u044b \u0431\u0443\u0434\u0443\u0442 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0441 \u0442\u0430\u043a\u0438\u043c \u0436\u0435 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c, \u043a\u0440\u0430\u0439\u043d\u0435 \u0443\u0434\u043e\u0431\u043d\u043e \u043f\u043e-\u043c\u043e\u0435\u043c\u0443 \ud83d\ude42<\/p>\n<p>  \u041d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0448\u0430\u0431\u043b\u043e\u043d \u0432  @RestController \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c, \u0442\u0430\u043a \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 ModelAndView \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 view(\u0431\u0435\u0437.html).<\/p>\n<p>  \u0413\u043e\u0442\u043e\u0432\u043e, \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043d\u0430 \u0441\u0430\u0439\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e rest \u043b\u0438\u0431\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u043e\u0440\u043c\u044b \u0432 \/users\/add<\/p>\n<h4><a name=\"32\"><\/a>3.2 \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c<\/h4>\n<p>  \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0434\u0432\u0430 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u0434\u0430\u044e\u0442\/\u0443\u0434\u0430\u043b\u044f\u044e\u0442 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f(\u0437\u0430\u043f\u0440\u043e\u0441 \u0442\u0438\u043f\u0430 GET\/DELETE \/users\/2):  <\/p>\n<pre><code class=\"java\">    @RequestMapping(value = &quot;\/{id}&quot;, method = RequestMethod.DELETE)     public void delete(@PathVariable(&quot;id&quot;) Long id)     {         users.delete(id);     }      @RequestMapping(value = &quot;\/{id}&quot;, method = RequestMethod.GET)     public User getUser(@PathVariable(&quot;id&quot;) Long id)     {         return users.findOne(id);     } <\/code><\/pre>\n<p>  \u0414\u0430\u043d\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043f\u043e-\u043c\u043e\u0435\u043c\u0443 \u0441\u0430\u043c\u043e\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u0435\u043c\u044b. \u0410\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f PathVariable(\u00ab\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u00bb) \u0432\u044b\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0435\u0442 \u0438\u0437 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0442\u043e, \u0447\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432 \u043d\u0435\u043c \u0432\u043c\u0435\u0441\u0442\u043e \u0448\u0430\u0431\u043b\u043e\u043d\u0430 {\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435}(\u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u2014 \u0446\u0438\u0444\u0440\u0430)<\/p>\n<h3><a name=\"4\"><\/a>4. \u0414\u043b\u044f \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0445 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442<\/h3>\n<p>   \u0412\u0435\u0442\u043a\u0430 <a href=\"https:\/\/github.com\/MaxPovver\/ForHabrahabr\/tree\/withcontroller\">https:\/\/github.com\/MaxPovver\/ForHabrahabr\/tree\/withcontroller<\/a> \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u0441\u0435 \u043d\u0443\u0436\u043d\u043e\u0435, \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u0434\u043e \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c import_me.sql \u0432 \u0432\u0430\u0448\u0435\u0439 \u0411\u0414.(\u043f\u043e\u0441\u043b\u0435 \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044f\/\u043a\u043b\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c checkout)<\/p>\n<h3><a name=\"5\"><\/a>5. \u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>  \u0425\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0443\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0441\u0438\u043b\u044c\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0435, \u043d\u0430 \u043e\u043d\u0430 \u043f\u043e-\u043c\u043e\u0435\u043c\u0443 \u0438 \u0442\u0430\u043a \u0443\u0436\u0435 \u0441\u043b\u0435\u0433\u043a\u0430 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0435\u043d\u0430, \u0430 \u044f \u0435\u0449\u0435 \u0434\u0430\u0436\u0435 \u0434\u043e \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u044b \u043d\u0435 \u0434\u043e\u0448\u0435\u043b =) \u0422\u0430\u043a \u0447\u0442\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435, OneToMany \u0438 \u0435\u0449\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0445 \u0432\u0435\u0449\u0435\u0439 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e, \u0435\u0441\u043b\u0438, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441 \u043a \u0442\u0435\u043c\u0435.<\/p>\n<p>  \u0423\u0434\u0430\u0447\u0438!   \t\t\t\t<\/p>\n<div class=\"polling\">\n<form action=\"\/json\/polling\/\" class=\"poll\" method=\"post\">\n<div class=\"poll_title\">\u0421\u0442\u0430\u0442\u044c\u044f<\/div>\n<p>  \t\t<input type=\"hidden\" name=\"post_id\" value=\"262361\"\/> \t\t<input type=\"hidden\" name=\"polling_question_id\" value=\"13787\"\/>  \t\t<\/p>\n<table class=\"answer\">\n<tr>\n<td class=\"input\"> \t\t\t\t\t<input type=\"radio\" id=\"vv67923\" \t\t\t\t\t\tclass=\"radio js-field-data\" \t\t\t\t\t\tname=\"variant[]\" \t\t\t\t\t\tvalue=\"67923\" \/> \t\t\t\t<\/td>\n<td class=\"label\"> \t\t\t\t\t<label for=\"vv67923\">\u041f\u043e\u043b\u0435\u0437\u043d\u0430\u044f, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0434\u0435\u043b\u0430\u0442\u044c<\/label> \t\t\t\t<\/td>\n<\/tr>\n<tr>\n<td class=\"input\"> \t\t\t\t\t<input type=\"radio\" id=\"vv67925\" \t\t\t\t\t\tclass=\"radio js-field-data\" \t\t\t\t\t\tname=\"variant[]\" \t\t\t\t\t\tvalue=\"67925\" \/> \t\t\t\t<\/td>\n<td class=\"label\"> \t\t\t\t\t<label for=\"vv67925\">\u041f\u043e\u043b\u0435\u0437\u043d\u0430\u044f, \u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c<\/label> \t\t\t\t<\/td>\n<\/tr>\n<tr>\n<td class=\"input\"> \t\t\t\t\t<input type=\"radio\" id=\"vv67927\" \t\t\t\t\t\tclass=\"radio js-field-data\" \t\t\t\t\t\tname=\"variant[]\" \t\t\t\t\t\tvalue=\"67927\" \/> \t\t\t\t<\/td>\n<td class=\"label\"> \t\t\t\t\t<label for=\"vv67927\">\u041d\u0435 \u043d\u0443\u0436\u043d\u0430<\/label> \t\t\t\t<\/td>\n<\/tr>\n<\/table>\n<p class=\"total\">\u041d\u0438\u043a\u0442\u043e \u0435\u0449\u0451 \u043d\u0435 \u0433\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043b. \u0412\u043e\u0437\u0434\u0435\u0440\u0436\u0430\u043b\u0441\u044f 1 \u0447\u0435\u043b\u043e\u0432\u0435\u043a.<\/p>\n<\/p><\/form>\n<p class=\"for_users_only_msg\">\u0422\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u043c\u043e\u0433\u0443\u0442 \u0443\u0447\u0430\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0432 \u043e\u043f\u0440\u043e\u0441\u0435. <a href=\"https:\/\/habrahabr.ru\/auth\/login\/\">\u0412\u043e\u0439\u0434\u0438\u0442\u0435<\/a>, \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430.<\/p>\n<\/p><\/div>\n<div class=\"clear\"><\/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=\"http:\/\/habrahabr.ru\/post\/262361\/\"> http:\/\/habrahabr.ru\/post\/262361\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>     \t\u0418 \u0441\u043d\u043e\u0432\u0430 \u0434\u0435\u043d\u044c \u0434\u043e\u0431\u0440\u044b\u0439.<br \/>  \u041f\u043e\u0441\u0442 \u0432 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 <a href=\"http:\/\/habrahabr.ru\/post\/262323\/\">Spring + Java EE + Persistence, \u0431\u0435\u0437 XML. \u0427\u0430\u0441\u0442\u044c 1<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-261224","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/261224","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=261224"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/261224\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=261224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=261224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=261224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}