Android Application using Retrofit to connect to Spring Security API Rest

Im sorry if i offend someone with these questions.
I made an android application with login and register page and i prepared it to send http to call the backend wich its implemented with Spring Security.

But i got stuck because i want spring to be prepare with spring security. So, i followed so many tutorials to make a login and register but they all uses php or html forms, or even spring security default login…my question its, how can i connect my front android information to the spring security service?

Example of the code:

ApiClient service (android studio)

 public interface UsuarioService {

    @POST("/usuarios/login")
    Call<LoginResponse> loginUser(@Body LoginRequest loginRequest); // 

    @POST("/usuarios/registrar")
    Call<RegisterResponse> registerUser(@Body RegisterRequest registerRequest);
}

Activity wich calls the Login with retrofit (android studio)

 btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = editTextEmail.getText().toString().trim();
                String pass = editTextPass.getText().toString().trim();
                if (login(email, pass)) {
                    goToMain();
                    saveOnPreferences(email, pass);
                }
            }
        });

    private boolean login(String email, String password) {
        if (!Util.isValidEmail(email) || !Util.isValidPassword(password)) {
            Toast.makeText(this, "Email y/o contraseña no válidos", Toast.LENGTH_LONG).show();
            return false;
        }

       else{
            // Here its the  Call<LoginResponse> loginUser(@Body LoginRequest loginRequest)
             // these response should return the a json file containin the user information
            return true;
        }
        
    }
}

Now, my problem its here: I follow these tutorial to make the Spring Security service:
https://hellokoding.com/registration-and-login-example-with-spring-security-spring-boot-spring-data-jpa-hsql-jsp/

But in the sample they use an default login and they return a String wich its i guess its the html file containing the page they want to redirect…But i want the login to response the user data…

Does anyone know if im getting this concept wronly? or any page explaining a posible implementation…

Please im desperated to understand this…

PS: this was my first implementation (which worked), but with spring security on, obviously this crashes

@RequestMapping(value = "/login", method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<Object> getLogin(HttpServletRequest request,
                @RequestParam(value="email") String email, 
                @RequestParam(value="password") String pass) {
        
        System.out.println("Log in petion received");   
        Usuario user = usuarioService.login(email, pass);
        return new ResponseEntity<Object>(user, HttpStatus.OK);
 
    }

Source: Android Studio Questions

One Reply to “Android Application using Retrofit to connect to Spring Security API Rest”

  • Hello, I have been trying to build an android application using java and spring-boot API for login … I did spring boot login api as using spring security but really stuck with how to do the android part using retrofit without a token. Did you get any solutions?

LEAVE A COMMENT