title: register new user with spring and hibernate
body: <p>I have a question about adding new user using spring and hibernate. I am  using  spring security for authentication and I managed to configure it using oracle db. The next thing I would like to implement is registering new user. What is the usual approach in this case? I read some materials but most of them were designed and implemented with jsp and my client side is written in angularjs. 
  What I have so far is a controller with two endpoints <code>/user</code> and <code>/register</code></p>
  
  <pre><code>@Controller
  public class UserController {
  
  @Autowired
  private RegisterService registerService;
  
  @RequestMapping("/user")
  @ResponseBody
  public Principal user(Principal user) {
      return user;
  }
  
  @RequestMapping(value = "/register", method = RequestMethod.POST)
  @ResponseBody
  public void registerUser() {
      User user = new User();
      registerService.save(user);
  }
  }
  </code></pre>
  
  <p>I believe I need also some service. In my case this is <code>RegisterService</code> and implementation of that service RegisterServiceImpl:</p>
  
  <pre><code>@Service
  public class RegisterServiceImpl implements RegisterService {
  
  @Autowired
  private UserDao userDao;
  
  @Autowired
  private SessionFactory sessionFactory;
  
  
  @Transactional
  @Override
  public void save(User user) {
      // what should be the implementation of this method?
  
      Session session = sessionFactory.getCurrentSession();
      session.save(user);
  }
  }
  </code></pre>
  
  <p>Here is my <code>UserDao</code> entity class:</p>
  
  <pre><code>@Entity
  @Table(name = "users")
  public class User {
  
  @Id
  @Column(name = "id")
  private int id;
  
  @Column(name = "username")
  private String userName;
  
  @Column(name = "password")
  private String password;
  
  @Column(name = "email")
  private String email;
  
  @Column(name = "is_enabled")
  private boolean isEnabled;
  
  @ManyToOne
  @JoinColumn(name = "department_id")
  private Department department;
  
  @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
  private Set&lt;UserRole&gt; userRole = new HashSet&lt;UserRole&gt;(0);
  
  public User() {
  
  }
  
  public User(String username, String password, Boolean isEnabled,             Set&lt;UserRole&gt; userRole) { 
      this.userName = username;
      this.password = password;
      this.isEnabled = isEnabled;
      this.userRole = userRole;
  }
  
  public int getId() {
      return id;
  }
  
  public void setId(int id) {
      this.id = id;
  }
  
  public String getUsername() {
      return userName;
  }
  
  public void setUsername(String userName) {
      this.userName = userName;
  }
  
  public String getPassword() {
      return password;
  }
  
  public void setPassword(String password) {
      this.password = password;
  }
  
  public String getEmail() {
      return email;
  }
  
  public void setEmail(String email) {
      this.email = email;
  }
  
  public boolean isEnabled() {
      return isEnabled;
  }
  
  public void setEnabled(boolean isEnabled) {
      this.isEnabled = isEnabled;
  }
  
  public Department getDepartment() {
      return department;
  }
  
  public void setDepartment(Department department) {
      this.department = department;
  }
  
  public Set&lt;UserRole&gt; getUserRole() {
      return this.userRole;
  }
  
  public void setUserRole(Set&lt;UserRole&gt; userRole) {
      this.userRole = userRole;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>My security config looks like:</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
  @Autowired
  UserDetailsService userDetailsService;
  
  @Autowired
  LogoutSuccess logoutSuccess;
  
  @Autowired
  public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
      auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
  }
  
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      .httpBasic()
    .and()
      .authorizeRequests()
        .antMatchers("/profile", "/logout", "/home").permitAll()
        .anyRequest().authenticated()
     .and()
     .addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class)
              .csrf().disable();
  }
  
  
  @Override
  public void configure(WebSecurity web) throws Exception {
      web.ignoring().antMatchers("/app/**");
  }
  
  private CsrfTokenRepository csrfTokenRepository() {
        HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
        repository.setHeaderName("X-XSRF-TOKEN");
        return repository;
      }
  
  @Bean
  public PasswordEncoder passwordEncoder() {
      PasswordEncoder encoder = new BCryptPasswordEncoder();
      return encoder;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>On user registration I would like to set all of the class members in <code>User</code> but I am not sure how to achieve that. I dont think creating <code>@PathVariable</code> for every class member would be appropriate because I will send sensitive data username and password. Many thanks for your answers!</p>