0

Logging in via postman works fine ONLY when using application/x-www-form-urlencoded Trying to do the same request with raw-json doesn't work.

Anyway I'm trying to implement the login to my react app. When i have the request set as:

      const response = await Axios.post(`${window.ipAddress.ip}/login`,
        JSON.stringify({ email: email.toLowerCase(), password: password }),
        { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
      );

From this in springboot the UserService class gets called which shows:

    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
        User user = findUserByEmail(email);
        if ( user != null){
            Collection<SimpleGrantedAuthority> authorities = new ArrayList<>();
            user.getRoles().forEach(role -> { authorities.add(new SimpleGrantedAuthority(role.getName()));});
            return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), authorities);
        }
        else{ throw new EntityNotFoundException("Entity not found"); }
    }

It then shows email as "NONE_PROVIDED" and ultimately failing.

The second issue is I don't actually have a /login route in controller code when searching through all files so I'm unsure at which point it actually calls this method through springboot.

The only changes I made from the default spring-security implementation is the use of my own class where I use "email" in place of "userName".

Any suggestions are welcome.

Edit:

the working login function via postman enter image description here

ABpositive
  • 291
  • 1
  • 18
  • show us what you are actually sending to the server and the one that doesnt work – Toerktumlare Jun 12 '22 at 17:02
  • @Toerktumlare Added the working postman function, the data being passed in the fields is the same for non-working however, it is posting it by the first snippet of code in React via Axios – ABpositive Jun 12 '22 at 17:52

1 Answers1

0

Springboot was expecting encoded form which is different from my other requests , this answer shows how to properly outline x-www-for-urlencoded

axios post request to send form data

having the request as:

      var bodyFormData = new FormData();
      bodyFormData.append('email', email.toLowerCase());
      bodyFormData.append('password', password);

      const response = await Axios({
        method: "post",
        url: `${window.ipAddress.ip}/login`,
        data: bodyFormData,
        headers: { "Content-Type": "multipart/form-data" },
      })
ABpositive
  • 291
  • 1
  • 18