Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

302-valid auth #22

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ build
.gradle
*.iml
.idea/*
out
out
.DS_Store
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ public String doLogin(String url, String username, String password) {
String requestBody = objectMapper.writeValueAsString(prepareLoginBody(username, password));
byte[] loginBody = requestBody.getBytes(StandardCharsets.UTF_8);

//TODO ?_format=json - added if needed
URL obj = new URL(url);
URL obj = new URL(url + "?_format=json");
//URL obj = new URL(url);
log.info("URL={}", obj);

HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
Expand All @@ -108,6 +110,18 @@ public String doLogin(String url, String username, String password) {
}

int responseCode = con.getResponseCode();
log.error("responseCode={}", responseCode);

if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP) { // 302
// Save cookie for future GET Requests
cookie = con.getHeaderField("Set-Cookie") != null ?
con.getHeaderField("Set-Cookie").split(";")[0] : "";
// with 302 the getInputStream fails
log.info("Successful login for user " + username);
log.info("cookie={}", cookie);
return "login for user" + username + " with 302";
}

if (responseCode == HttpURLConnection.HTTP_OK) { // Success
// Read from the response
StringBuilder stringBuilder = new StringBuilder();
Expand All @@ -123,9 +137,11 @@ public String doLogin(String url, String username, String password) {
con.getHeaderField("Set-Cookie").split(";")[0] : "";

log.info("Successful login for user " + username);
log.info("cookie={}", cookie);

return stringBuilder.toString();
} else {
log.error("Unsuccessful login request, Status-Code: " + responseCode);
log.error("Unsuccessful login request, Status-Code: {}", responseCode);
return null;
}
} catch (IOException e) {
Expand Down