1

I am new to spring and creating backend using spring and oauth 2 so far I was able to implement outh2 with spring and I can able to get access and refresh token:

localhost:8082/oauth/token

and response i am getting :

{
  "access_token": "2b57cd84-c1fb-493e-88b0-e3da2ae66c77",
  "token_type": "bearer",
  "refresh_token": "db9e5e33-4878-4a31-8037-b7ad0107b82a",
  "expires_in": 43199,
  "scope": "read write"
} 

and on user registration i am trying to get access and refresh token on behalf of user, for which i have done implementation like below (i have added this snippet inside user controller userRegistration method after getting user object request from mobile side):

final String clientId = PropertiesReader.getInstance().getProperty("client1");
final String clientSecret = PropertiesReader.getInstance().getProperty("client1password");
final Map<String, String> params = new HashMap<String, String>();
                        params.put("grant_type", "password");
                        params.put("client_id", clientId);
                        params.put("username", userObj.getUsername());
                        params.put("password", dummyPwd);
                        final Response response = RestAssured
                                .given()
                                .auth()
                                .preemptive()
                                .basic(clientId, clientSecret)
                                .and()
                                .with()
                                .params(params)
                                .when()
                                .post(PropertiesReader.getInstance()
                                        .getProperty("oauthurl"));
                        if (CustomValidation.checkStringIsNotEmpty(response
                                .asString())) {
                            return ClientResponse.setResponse(
                                    response.asString(), HttpStatus.OK);
                        } else {
                            return ClientResponse.setResponse(PropertiesReader
                                    .getInstance().getProperty("wentwrong"),
                                    HttpStatus.INTERNAL_SERVER_ERROR);
                        }

and i am getting response :

  {"access_token":"00bfd552-c7eb-48ff-8f2bfd5cd24869be",
"token_type":"bearer",
"refresh_token":"f88be427-ea6e-4cad-8dc5-01d37e4cfdbc",
"expires_in":299,
"scope":"update read write",
"date":1599982652000,
"deviceDetailsSaved":false,
"firstname":"test",
"role":"USER",
"name":"test test",
"mobile":"8169280313",
"avatar":"1.svg",
"email":"test1@mail.com",
"lastname":"test"}

What i want to know is there any other proper way to get oauth tokens and append in user object after registration?. As of now this implementation is for only front end client (android) so thats why i kept client details static. Any Suggestion will helpful.

Rohit Maurya
  • 730
  • 1
  • 9
  • 22

0 Answers0