title: Spring Rest API User Authentication for One Time
body: <p>I am making Rest API. I have to do one time user authentication. They won't make any other request after login to api. I am using Spring Security for MVC authentication. </p>
  
  <pre><code>else if(!customerWithEmail.getPassword().equals(passwordEncoder.encode(password))){
          map.put("ERROR CODE", "04 - Wrong Password");
          //Doesnt work for sure.
          //TODO email password auth.
          return map;
      }
  </code></pre>
  
  <p>I have problem with user and password auth. I am using BCrypt with UserDetails in other modules.</p>
  
  <p>Our clients has static IP address and they can't login anywhere but recorded IP address in DB. But email password check will be good for future.</p>
  
  <pre><code>@RestController
  @RequestMapping(value = "/api")
  public class ApiController {
  
  @Autowired
  private CustomerDao customerDao;
  
  @Autowired
  private PasswordEncoder passwordEncoder;
  
  @RequestMapping(value = "/login", method = RequestMethod.GET)
  public @ResponseBody Map customerLogin(@RequestParam(value = "email") String email, @RequestParam(value = "password") String password,
                    HttpServletRequest request) {
  
      Map map = new HashMap();
      try {
  
          String customerIpAddress = request.getRemoteAddr();
          Customer customerWithEmail = customerDao.getUserByEmail(email);
          Customer customerWithIpAddress = customerDao.getUserByIpAddress(customerIpAddress);
  
  
          if (customerWithEmail == null) {
              map.put("ERROR CODE", "01 - User Not Found");
              return map;
          } else if (customerWithIpAddress == null) {
              map.put("ERROR CODE", "02 - IP Address Not Found");
              return map;
          } else if (!customerWithEmail.equals(customerWithIpAddress)) {
              map.put("ERROR CODE", "03 - User and IP Address Does Not Match");
              return map;
          }else if(!customerWithEmail.getPassword().equals(passwordEncoder.encode(password))){
              map.put("ERROR CODE", "04 - Wrong Password");
              //Doesnt work for sure.
              //TODO email password auth.
              return map;
          }
          else {
              map.put("Email", customerWithEmail.getEmail());
              map.put("Name", customerWithEmail.getName());
              map.put("Surname", customerWithEmail.getSurname());
              map.put("Company", customerWithEmail.getCompanyName());
              return map;
          }
      } catch (Exception e) {
          map.put("ERROR CODE", "05 - See Details");
          map.put("Error", e.toString());
          return map;
      }
  
  }
  }
  </code></pre>
  
  <p>Is my business logic true? I thing isn't.</p>