title: How to create role user login?
body: <p>I want to create a role user to login. So I am still confused how to create user role and login with role user. I have made some source code as below :</p>
  
  <p>User.java :</p>
  
  <pre><code>package com.practice.login.entity;
  
  import javax.persistence.*;
  import java.util.Set;
  
  @Entity
  @Table(name = "user")
  public class User {
      private Long id;
      private String username;
      private String password;
      private String passwordConfirm;
      private Set&lt;Role&gt; roles;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      public Long getId() {
          return id;
      }
  
      public void setId(Long idUser) {
          this.id = idUser;
      }
  
      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;
      }
  
      @Transient
      public String getPasswordConfirm() {
          return passwordConfirm;
      }
  
      public void setPasswordConfirm(String passwordConfirm) {
          this.passwordConfirm = passwordConfirm;
      }
  
      @ManyToMany
      @JoinTable(
              name = "user_role", 
              joinColumns = @JoinColumn(
                      name = "user_id"), 
              inverseJoinColumns = @JoinColumn(
                      name = "role_id"))
      public Set&lt;Role&gt; getRoles() {
          return roles;
      }
  
      public void setRoles(Set&lt;Role&gt; roles) {
          this.roles = roles;
      }
  }
  </code></pre>
  
  <p>Role.java</p>
  
  <pre><code>package com.practice.login.entity;
  
  import javax.persistence.*;
  import java.util.Set;
  
  @Entity
  @Table(name = "role")
  public class Role {
      private Long id;
      private String name;
      private Set&lt;User&gt; users;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      @ManyToMany(mappedBy = "roles")
      public Set&lt;User&gt; getUsers() {
          return users;
      }
  
      public void setUsers(Set&lt;User&gt; users) {
          this.users = users;
      }
  }
  </code></pre>
  
  <p>UserService.java :</p>
  
  <pre><code>package com.practice.login.service;
  
  import com.practice.login.entity.User;
  
  public interface UserService {
  
      void save(User user);
  
      User findByUsername(String username);
  
  }
  </code></pre>
  
  <p>UserServiceImpl.java</p>
  
  <pre><code>package com.practice.login.service;
  
  import com.practice.login.entity.User;
  import com.practice.login.repository.RoleRepository;
  import com.practice.login.repository.UserRepository;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  import org.springframework.stereotype.Service;
  
  import java.util.HashSet;
  
  @Service
  public class UserServiceImpl implements UserService {
      @Autowired
      private UserRepository userRepository;
      @Autowired
      private RoleRepository roleRepository;
      @Autowired
      private BCryptPasswordEncoder bCryptPasswordEncoder;
  
      @Override
      public void save(User user) {
          user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
          user.setRoles(new HashSet&lt;&gt;(roleRepository.findAll()));
          userRepository.save(user);
      }
  
      @Override
      public User findByUsername(String username) {
          return userRepository.findByUsername(username);
      }
  }
  </code></pre>
  
  <p>UserDetailServiceImpl.java</p>
  
  <pre><code>package com.practice.login.service;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.security.core.GrantedAuthority;
  import org.springframework.security.core.authority.SimpleGrantedAuthority;
  import org.springframework.security.core.userdetails.UserDetails;
  import org.springframework.security.core.userdetails.UserDetailsService;
  import org.springframework.security.core.userdetails.UsernameNotFoundException;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import com.practice.login.entity.Role;
  import com.practice.login.entity.User;
  import com.practice.login.repository.UserRepository;
  
  import java.util.HashSet;
  import java.util.Set;
  
  @Service
  public class UserDetailsServiceImpl implements UserDetailsService {
      @Autowired
      private UserRepository userRepository;
  
      @Override
      @Transactional(readOnly = true)
      public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
          User user = userRepository.findByUsername(username);
          Set&lt;GrantedAuthority&gt; grantedAuthorities = new HashSet&lt;&gt;();
          for (Role role : user.getRoles()) {
              grantedAuthorities.add(new SimpleGrantedAuthority(role.getName()));
          }
  
          return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(),
                  grantedAuthorities);
      }
  }
  </code></pre>
  
  <p>UserRepository.java</p>
  
  <pre><code>package com.practice.login.repository;
  
  import org.springframework.data.jpa.repository.JpaRepository;
  import org.springframework.stereotype.Repository;
  
  import com.practice.login.entity.User;
  
  @Repository
  public interface UserRepository extends JpaRepository&lt;User, Long&gt; {
      User findByUsername(String username);
  }
  </code></pre>
  
  <p>RoleRepository.java</p>
  
  <pre><code>package com.practice.login.repository;
  
  import org.springframework.data.jpa.repository.JpaRepository;
  import org.springframework.stereotype.Repository;
  
  import com.practice.login.entity.Role;
  
  @Repository
  public interface RoleRepository extends JpaRepository&lt;Role, Long&gt; {
  }
  </code></pre>
  
  <p>How to login with role privilages?</p>