Loading src/main/java/org/eclipse/openk/portal/auth2/util/JwtHelper.java +19 −14 Original line number Diff line number Diff line Loading @@ -11,6 +11,8 @@ */ package org.eclipse.openk.portal.auth2.util; import java.io.*; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import javax.ws.rs.core.MediaType; import org.eclipse.openk.portal.auth2.model.JwtHeader; Loading @@ -20,11 +22,7 @@ import org.eclipse.openk.portal.auth2.model.KeyCloakRole; import org.eclipse.openk.portal.auth2.model.KeyCloakUser; import com.google.gson.JsonSyntaxException; import com.google.gson.reflect.TypeToken; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Type; import java.net.HttpURLConnection; import java.net.URL; Loading @@ -43,10 +41,16 @@ public class JwtHelper { } public static JwtToken login(String user, String password) throws PortalInternalServerError { String token = sendPost(BackendConfig.getInstance().getAuthServerUrl() + "auth/realms/" + String token = null; try { token = sendPost(BackendConfig.getInstance().getAuthServerUrl() + "auth/realms/" + BackendConfig.getInstance().getKeycloakRealm() + "/protocol/openid-connect/token", "username=" + user + "&password=" + password + "&client_id=" "username=" + user + "&password=" + URLEncoder.encode(password, "UTF-8") + "&client_id=" + BackendConfig.getInstance().getKeycloakClient() + "&grant_type=password"); } catch (UnsupportedEncodingException e) { logger.error( "Unsupported Encoding Exception: ", e); throw new PortalInternalServerError(e.getMessage()); } return getJwtTokenFromJson(token); } Loading Loading @@ -167,19 +171,20 @@ public class JwtHelper { try (AutoCloseable conc = con::disconnect) { con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); con.setRequestProperty("Accept", MediaType.APPLICATION_JSON); con.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length)); con.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes(StandardCharsets.UTF_8.name()).length)); con.setInstanceFollowRedirects(false); con.setDoOutput(true); // Send request try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) { wr.writeBytes(urlParameters); wr.write(urlParameters.getBytes(StandardCharsets.UTF_8.name())); } // Get Response InputStream is = con.getInputStream(); try (BufferedReader rd = new BufferedReader(new InputStreamReader(is, "UTF-8"))) { try (BufferedReader rd = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8.name()))) { String line; while ((line = rd.readLine()) != null) { response.append(line); Loading Loading
src/main/java/org/eclipse/openk/portal/auth2/util/JwtHelper.java +19 −14 Original line number Diff line number Diff line Loading @@ -11,6 +11,8 @@ */ package org.eclipse.openk.portal.auth2.util; import java.io.*; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import javax.ws.rs.core.MediaType; import org.eclipse.openk.portal.auth2.model.JwtHeader; Loading @@ -20,11 +22,7 @@ import org.eclipse.openk.portal.auth2.model.KeyCloakRole; import org.eclipse.openk.portal.auth2.model.KeyCloakUser; import com.google.gson.JsonSyntaxException; import com.google.gson.reflect.TypeToken; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Type; import java.net.HttpURLConnection; import java.net.URL; Loading @@ -43,10 +41,16 @@ public class JwtHelper { } public static JwtToken login(String user, String password) throws PortalInternalServerError { String token = sendPost(BackendConfig.getInstance().getAuthServerUrl() + "auth/realms/" + String token = null; try { token = sendPost(BackendConfig.getInstance().getAuthServerUrl() + "auth/realms/" + BackendConfig.getInstance().getKeycloakRealm() + "/protocol/openid-connect/token", "username=" + user + "&password=" + password + "&client_id=" "username=" + user + "&password=" + URLEncoder.encode(password, "UTF-8") + "&client_id=" + BackendConfig.getInstance().getKeycloakClient() + "&grant_type=password"); } catch (UnsupportedEncodingException e) { logger.error( "Unsupported Encoding Exception: ", e); throw new PortalInternalServerError(e.getMessage()); } return getJwtTokenFromJson(token); } Loading Loading @@ -167,19 +171,20 @@ public class JwtHelper { try (AutoCloseable conc = con::disconnect) { con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); con.setRequestProperty("Accept", MediaType.APPLICATION_JSON); con.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length)); con.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes(StandardCharsets.UTF_8.name()).length)); con.setInstanceFollowRedirects(false); con.setDoOutput(true); // Send request try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) { wr.writeBytes(urlParameters); wr.write(urlParameters.getBytes(StandardCharsets.UTF_8.name())); } // Get Response InputStream is = con.getInputStream(); try (BufferedReader rd = new BufferedReader(new InputStreamReader(is, "UTF-8"))) { try (BufferedReader rd = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8.name()))) { String line; while ((line = rd.readLine()) != null) { response.append(line); Loading