title: Spring security Remember me and Custom Authentication provider
body: <p>issue with spring remember me</p>
  
  <p>in my spring security application i have custom authentication provider that authenticate and return custom authentication object and authentication works perfectly but i have issue with spring Remember me service it's not working correctly</p>
  
  <p>but when i authenticate through normal spring security, by using UserDetailService then Remember me works perfectly my code for both is as below</p>
  
  <pre><code>package com.websopti.wotms.auth;
  
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.Collection;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.security.authentication.AuthenticationProvider;
  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  import org.springframework.security.core.Authentication;
  import org.springframework.security.core.AuthenticationException;
  import org.springframework.security.core.GrantedAuthority;
  import org.springframework.security.core.authority.SimpleGrantedAuthority;
  import org.springframework.security.core.userdetails.UsernameNotFoundException;
  import org.springframework.stereotype.Component;
  
  import com.websopti.wotms.entity.User;
  import com.websopti.wotms.enums.ErrorKey;
  import com.websopti.wotms.service.UserService;
  
  @Component
  public class CustomAuthenticationProvider implements AuthenticationProvider {
  
      @Autowired
      public UserService userService;
  
      @Override
      public Authentication authenticate(Authentication authentication) throws AuthenticationException {
  
          String email = authentication.getPrincipal().toString();
          String password = authentication.getCredentials().toString();
  
          Collection&lt;GrantedAuthority&gt; authorities = new ArrayList&lt;GrantedAuthority&gt;();
  
          String username = null;
  
          try {
              username = userService.authenticate(email, password);
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          if(username != null &amp;&amp; !username.equals("0")){
  
              User user = userService.findByEmail(email);
  
              if (user != null) {
  
                  authorities.add(new SimpleGrantedAuthority(user.getRole().name()));
                  return (new UsernamePasswordAuthenticationToken(user, null, authorities));
  
              } else {
  
                  User newUser = new User();
                  newUser.setName(username);
                  newUser.setEmail(email);
                  newUser.setPassword(password);
  
                  userService.register(newUser);
  
                  newUser = userService.findById(newUser.getId());                
                  authorities.add(new SimpleGrantedAuthority(newUser.getRole().name()));
  
                  return (new UsernamePasswordAuthenticationToken(newUser, null, authorities));
              }
  
          } else {
  
              throw new UsernameNotFoundException(ErrorKey.USER_NOT_FOUND.name());
  
          }
  
  
      }
  
      @Override
      public boolean supports(Class&lt;?&gt; authentication) {
  
          return authentication.equals(UsernamePasswordAuthenticationToken.class);
      }
  
  }
  </code></pre>
  
  <p>if i use following code for authenticate then it works okay</p>
  
  <pre><code>package com.websopti.wotms.auth;
  
  import org.springframework.beans.factory.annotation.Autowired;
  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 com.websopti.wotms.entity.User;
  import com.websopti.wotms.enums.ErrorKey;
  import com.websopti.wotms.service.UserService;
  
  @Service
  public class AuthenticationService implements UserDetailsService {
  
      @Autowired
      public UserService userService;
  
      @Override
      public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
  
          User user = userService.findByEmail(email);
  
          if(user != null)
              return user;
          else
              throw new UsernameNotFoundException(ErrorKey.USER_NOT_FOUND.name());
      }
  
  }
  </code></pre>
  
  <p>can some one guide me what is the issue here in my code?</p>