|
|
@@ -1,7 +1,11 @@
|
|
|
package com.deliveryproject.easydelivery;
|
|
|
|
|
|
import com.deliveryproject.easydelivery.Configuration.Constants;
|
|
|
+import com.deliveryproject.easydelivery.Models.KeycloakUser.User;
|
|
|
import com.deliveryproject.easydelivery.RequestBody.Token;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.ObjectWriter;
|
|
|
+import jdk.net.SocketFlow;
|
|
|
import org.springframework.http.*;
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
@@ -63,7 +67,7 @@ public class KeycloakController {
|
|
|
|
|
|
@RequestMapping(value = "/keycloak/createUser")
|
|
|
@ResponseBody
|
|
|
- public void createUser(@RequestParam String username, @RequestParam String password) {
|
|
|
+ public ResponseEntity<String> createUser(@RequestBody User user) {
|
|
|
try {
|
|
|
URL url = new URL(KEYCLOAK_ADDRESS + "/auth/admin/realms/SpringBootKeycloak/users");
|
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
@@ -73,19 +77,12 @@ public class KeycloakController {
|
|
|
|
|
|
conn.setDoOutput(true);
|
|
|
|
|
|
- String json = "{\n" +
|
|
|
- " \"username\": \"" + username + "\",\n" +
|
|
|
- " \"enabled\": true,\n" +
|
|
|
- " \"credentials\": [\n" +
|
|
|
- " {\n" +
|
|
|
- " \"type\": \"password\",\n" +
|
|
|
- " \"value\": \"" + password + "\",\n" +
|
|
|
- " \"temporary\": false\n" +
|
|
|
- " }\n" +
|
|
|
- " ]\n" +
|
|
|
- "}";
|
|
|
+ ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
|
|
|
+ String json = ow.writeValueAsString(user);
|
|
|
+
|
|
|
try (OutputStream os = conn.getOutputStream()) {
|
|
|
byte[] input = json.getBytes("utf-8");
|
|
|
+ System.out.println(json);
|
|
|
os.write(input, 0, input.length);
|
|
|
}
|
|
|
|
|
|
@@ -96,10 +93,12 @@ public class KeycloakController {
|
|
|
response.append(responseLine.trim());
|
|
|
}
|
|
|
System.out.println(response.toString());
|
|
|
-
|
|
|
+ //TODO We need to implement adding a new user to a group
|
|
|
conn.disconnect();
|
|
|
+ return new ResponseEntity<>(HttpStatus.OK);
|
|
|
} catch (Exception e) {
|
|
|
System.err.println(e);
|
|
|
+ return new ResponseEntity<>(HttpStatus.FORBIDDEN);
|
|
|
}
|
|
|
}
|
|
|
}
|