wip: авторизация

This commit is contained in:
2025-07-17 22:04:28 +03:00
parent 8d1ffd5fa8
commit e5535156dd
9 changed files with 557 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
package ru.di9.ihc;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public record AuthResponse(
Alert alert
) {
public record Alert(
String type,
String message
) {
}
}

View File

@@ -0,0 +1,85 @@
package ru.di9.ihc;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.net.CookieManager;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
public class IhcClient {
private final String baseUrl;
private final HttpClient httpClient;
private final ObjectMapper mapper;
public IhcClient(String baseUrl) {
this.baseUrl = baseUrl;
this.httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.connectTimeout(Duration.ofSeconds(10))
.cookieHandler(new CookieManager())
.build();
this.mapper = new ObjectMapper();
}
public boolean auth(String login, String password) {
var request = HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(
"j_username=%s&j_password=%s&recaptcha=&ihccaptcha=".formatted(login, password)
))
.uri(URI.create("%s/j_spring_security_check?ajax=true".formatted(baseUrl)))
.setHeader("Accept", "application/json")
.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
.setHeader("Referer", "%s/login/auth".formatted(baseUrl))
.build();
HttpResponse<String> response;
try {
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
if (response.statusCode() != 200) {
return false;
}
AuthResponse authResponse;
try {
String json = response.body();
authResponse = mapper.readValue(json, AuthResponse.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return "none".equalsIgnoreCase(authResponse.alert().type());
}
public void getDomainList() {
var request = HttpRequest.newBuilder()
.GET()
.uri(URI.create("%s/dnsZone/list".formatted(baseUrl)))
.setHeader("Accept", "application/json")
.build();
HttpResponse<String> response;
try {
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
if (response.statusCode() != 200) {
throw new RuntimeException("not 200");
}
String body = response.body();
System.out.println(body);
}
}

View File

@@ -0,0 +1,58 @@
package ru.di9.ihc;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import org.apache.commons.lang3.RandomUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class AuthTest {
static int port;
static WireMockServer wireMockServer;
@BeforeAll
static void beforeAll() {
port = RandomUtils.nextInt(9000, 9999);
wireMockServer = new WireMockServer(port);
wireMockServer.start();
}
@AfterAll
static void afterAll() {
wireMockServer.stop();
}
@AfterEach
void after() {
wireMockServer.resetAll();
}
@Test
void correctAuthTest() {
wireMockServer.stubFor(post("/j_spring_security_check?ajax=true")
.willReturn(WireMock.okJson("""
{"redirect":{"url":"/"},"alert":{"type":"none","message":""}}""")));
var ihc = new IhcClient("http://localhost:%d".formatted(port));
boolean result = ihc.auth("user1", "passwd1");
assertTrue(result);
}
@Test
void incorrectAuthTest() {
wireMockServer.stubFor(post("/j_spring_security_check?ajax=true")
.willReturn(WireMock.status(302)));
var ihc = new IhcClient("http://localhost:%d".formatted(port));
boolean result = ihc.auth("user1", "wrong_passwd");
assertFalse(result);
}
}