title: How to do a RESTful login API using Spring Security?
body: <p>I just started to work with Spring security and just implemented HTTP basic authentication that works fine.</p>
  <p>Now I would like to create login endpoint and try to get authenticated user.</p>
  <p>I am not sure what do I have to use for that? I did read online lots of stuff but not sure where to start.</p>
  <p>Any tip would be very appreciated!</p>
  <p>This is the code that used for authentication.</p>
  <p><strong>SecurityConfiguration.java</strong></p>
  <pre><code>@Configuration
  @EnableWebSecurity
  @EnableGlobalMethodSecurity(prePostEnabled = true)
  public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private UserDetailsService userDetailsService;
  
     @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth
              .userDetailsService(userDetailsService)
              .passwordEncoder(encodePWD());
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
  
          http
              .sessionManagement()
                  .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
          http
              .csrf().disable();
          http
              .httpBasic()
                  .and()
              .authorizeRequests()
                  .antMatchers(&quot;/rest/**&quot;).permitAll()
                  .and()
              .authorizeRequests()
                  .antMatchers(&quot;/secure/**&quot;).hasAnyRole(&quot;ADMIN&quot;)
                  .anyRequest().authenticated()
                  .and()
              .formLogin()
                  .permitAll();
      }
  
      @Bean
      public BCryptPasswordEncoder encodePWD() {
          return new BCryptPasswordEncoder();
      }
  }
  </code></pre>
  <p><strong>CustomUserDetailsService.java</strong></p>
  <pre><code>@Service
  public class CustomUserDetailsService implements UserDetailsService {
  
      @Autowired
      private UserRepository userRepository;
  
      @Override
      public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
          User user = userRepository.findByUsername(username);
          CustomUserDetails userDetails = null;
  
          if(user != null){
              userDetails = new CustomUserDetails();
              userDetails.setUser(user);
          }else{
              throw new UserNotFoundException(&quot;User doesn't exist with this username: &quot; + username);
          }
          return userDetails;
      }
  }
  </code></pre>
  <p><strong>CustomUserDetails.java</strong></p>
  <pre><code>@Getter
  @Setter
  public class CustomUserDetails implements UserDetails {
  
      private User user;
  
      @Override
      public Collection&lt;? extends GrantedAuthority&gt; getAuthorities() {
          return user.getRoles().stream().map(role -&gt; new SimpleGrantedAuthority(&quot;ROLE_&quot; + role.getRole())).collect(Collectors.toSet());
  
      }
  
      @Override
      public String getPassword() {
          return user.getPassword();
      }
  
      @Override
      public String getUsername() {
          return user.getUsername();
      }
  
      @Override
      public boolean isAccountNonExpired() {
          return true;
      }
  
      @Override
      public boolean isAccountNonLocked() {
          return true;
      }
  
      @Override
      public boolean isCredentialsNonExpired() {
          return true;
      }
  
      @Override
      public boolean isEnabled() {
          return true;
      }
  }
  </code></pre>
  <p><strong>UPDATE</strong></p>
  <p>I am sending my data like JSON using Postman</p>
  <pre><code>{
      &quot;username&quot;: &quot;admin&quot;,
      &quot;password&quot;: &quot;admin35&quot;      
  }
  </code></pre>