|
|
@@ -8,6 +8,15 @@ import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
+import java.io.*;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.MalformedURLException;
|
|
|
+import java.net.ProtocolException;
|
|
|
+import java.net.URL;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+import static com.deliveryproject.easydelivery.Configuration.Constants.*;
|
|
|
+
|
|
|
@RestController
|
|
|
public class KeycloakController {
|
|
|
@RequestMapping(value = "/keycloak/login")
|
|
|
@@ -27,7 +36,71 @@ public class KeycloakController {
|
|
|
|
|
|
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(map, headers);
|
|
|
|
|
|
- ResponseEntity<Token> exchange = restTemplate.exchange("http://localhost:8180/auth/realms/SpringBootKeycloak/protocol/openid-connect/token", HttpMethod.POST, entity, Token.class);
|
|
|
+ ResponseEntity<Token> exchange = restTemplate.exchange(KEYCLOAK_ADDRESS + "/auth/realms/SpringBootKeycloak/protocol/openid-connect/token", HttpMethod.POST, entity, Token.class);
|
|
|
return exchange;
|
|
|
}
|
|
|
-}
|
|
|
+
|
|
|
+ @RequestMapping(value = "/keycloak/adminLogin")
|
|
|
+ @ResponseBody
|
|
|
+ public ResponseEntity<Token> getAdminToken() {
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
+
|
|
|
+ MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
|
|
|
+ map.add("client_id", "admin-cli");
|
|
|
+ map.add("grant_type", "password");
|
|
|
+ map.add("username", ADMIN_USERNAME);
|
|
|
+ map.add("password", ADMIN_PASSWORD);
|
|
|
+
|
|
|
+ HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(map, headers);
|
|
|
+
|
|
|
+ ResponseEntity<Token> exchange = restTemplate.exchange(KEYCLOAK_ADDRESS + "/auth/realms/master/protocol/openid-connect/token", HttpMethod.POST, entity, Token.class);
|
|
|
+ return exchange;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @RequestMapping(value = "/keycloak/createUser")
|
|
|
+ @ResponseBody
|
|
|
+ public void createUser(@RequestParam String username, @RequestParam String password) {
|
|
|
+ try {
|
|
|
+ URL url = new URL(KEYCLOAK_ADDRESS + "/auth/admin/realms/SpringBootKeycloak/users");
|
|
|
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
+ conn.setRequestMethod("POST");
|
|
|
+ conn.setRequestProperty("Content-Type", "application/json");
|
|
|
+ conn.setRequestProperty("Authorization", "Bearer " + Objects.requireNonNull(getAdminToken().getBody()).access_token);
|
|
|
+
|
|
|
+ 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" +
|
|
|
+ "}";
|
|
|
+ try (OutputStream os = conn.getOutputStream()) {
|
|
|
+ byte[] input = json.getBytes("utf-8");
|
|
|
+ os.write(input, 0, input.length);
|
|
|
+ }
|
|
|
+
|
|
|
+ BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
|
|
|
+ StringBuilder response = new StringBuilder();
|
|
|
+ String responseLine = null;
|
|
|
+ while ((responseLine = br.readLine()) != null) {
|
|
|
+ response.append(responseLine.trim());
|
|
|
+ }
|
|
|
+ System.out.println(response.toString());
|
|
|
+
|
|
|
+ conn.disconnect();
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.err.println(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|