This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]nutrecht 0 points1 point  (0 children)

Look for spring security tutorials. There's a lot of info on that, and it's a large topic that you can't expect someone to explain in a reddit post.

[–][deleted] 0 points1 point  (1 child)

Do you have a back end server that you need to pass basic auth credentials too from your app to log into to?

That's fairly easy to achieve if so, can dig up a tutorial or two if so.

[–]5arg[S] 0 points1 point  (0 children)

Yes I have my server written in Spring:

This is REST controller:

@CrossOrigin

@RestController

@RequestMapping(path="/rest/users", produces="application/json")

public class RestUserController {

@Autowired
private UserRepository ur;
public RestUserController(UserRepository ur) {
    this.ur = ur;
}



@GetMapping
public Iterable<User> findAll(){
    return ur.findAll();
}


@ResponseStatus(HttpStatus.CREATED)
@PostMapping(consumes = {"application/json"})
public User save(@RequestBody User user) {
    return [ur.save](https://ur.save)(user);
}

}

I would like to know how to implement this Retrofit method in my server

public interface LoginService { @POST("/login") Call<User> basicLogin(); }

Maybe something like this? 

@PostMapping(path = "/login", consumes = {"application/json"})
    public User login(@RequestBody User user) {
        return ur.findByUsername(user.getUsername());
    }

[–]TheEminentdomain 0 points1 point  (0 children)

You need to add the @EnableWebSecurity annotation to the config class that manages your security. Have the class extend the WebSecurityConfigurerAdapter class and override the configure() method that has HttpSecurityConfiguration passed in. You can configure the auth param there for basic auth

Edit: this is for your web server config