사용자 이름과 비밀번호로 특정 REST API에서 인증하는 데 사용되는 HTTP POST 요청에서 쿠키를 얻으려고했습니다. 문제는 쿠키 저장소에 쿠키가 없다는 것입니다 (관련된 모든 숨겨진 매개 변수가 정확합니다). 사용자를 인증하는 데 사용되는 POST 메소드 (JSON)의 본체는 :Apache HttpClient 4.x가 지원되지 않는 미디어 유형
{
"username": <username>,
"password": <password>
}
I가 다음 코드를 사용하고 :
public static void main(String[] args) {
String USER_AUTHENTICATION = "/user/authentication";
String baseUrl = "http://<someIP>/<someProjectName>/rest";
HttpClient http = null;
CookieStore httpCookieStore = new BasicCookieStore();
RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("<someUsername>", "<somePassword>"));
HttpClientBuilder builder = HttpClientBuilder.create().setDefaultCookieStore(httpCookieStore)
.setDefaultRequestConfig(globalConfig).setDefaultCredentialsProvider(credentialsProvider);
http = builder.build();
HttpPost httpRequest = new HttpPost(baseUrl + USER_AUTHENTICATION);
HttpResponse httpResponse;
try {
httpResponse = http.execute(httpRequest);
} catch (Throwable error) {
throw new RuntimeException(error);
}
List<Cookie> cookies = httpCookieStore.getCookies();
System.out.println("Cookies! " + cookies);
}
콘솔 출력은 :
12:09:34.267 [main] DEBUG org.apache.http.client.protocol.RequestAddCookies - CookieSpec selected: standard
12:09:34.295 [main] DEBUG org.apache.http.client.protocol.RequestAuthCache - Auth cache not set in the context
12:09:34.298 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection request: [route: {}->http://<someIP>][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
12:09:34.343 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {}->http://<someIP>][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
12:09:34.346 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Opening connection {}->http://<someIP>
12:09:34.355 [main] DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator - Connecting to /<someIP>
12:09:34.363 [main] DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator - Connection established <someOtherIP>:<somePort><-><someIP>
12:09:34.363 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Executing request POST /<someProjectName>/rest/user/authentication HTTP/1.1
12:09:34.363 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Target auth state: UNCHALLENGED
12:09:34.365 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Proxy auth state: UNCHALLENGED
12:09:34.368 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> POST /<someProjectName>/rest/user/authentication HTTP/1.1
12:09:34.369 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-Length: 0
12:09:34.369 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Host: <someIP>
12:09:34.369 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Connection: Keep-Alive
12:09:34.369 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_91)
12:09:34.369 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Accept-Encoding: gzip,deflate
12:09:34.369 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "POST /<someProjectName>/rest/user/authentication HTTP/1.1[\r][\n]"
12:09:34.369 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Length: 0[\r][\n]"
12:09:34.369 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Host: <someIP>[\r][\n]"
12:09:34.369 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
12:09:34.369 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_91)[\r][\n]"
12:09:34.370 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"
12:09:34.370 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
12:09:34.406 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "HTTP/1.1 415 Unsupported Media Type[\r][\n]"
12:09:34.406 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Date: Tue, 10 Jan 2017 11:09:39 GMT[\r][\n]"
12:09:34.406 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Server: Apache-Coyote/1.1[\r][\n]"
12:09:34.406 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Content-Length: 0[\r][\n]"
12:09:34.406 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Access-Control-Allow-Origin: *[\r][\n]"
12:09:34.406 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Connection: close[\r][\n]"
12:09:34.406 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Content-Type: text/plain; charset=UTF-8[\r][\n]"
12:09:34.406 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "[\r][\n]"
12:09:34.413 [main] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 415 Unsupported Media Type
12:09:34.413 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Date: Tue, 10 Jan 2017 11:09:39 GMT
12:09:34.413 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Server: Apache-Coyote/1.1
12:09:34.413 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Length: 0
12:09:34.413 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Access-Control-Allow-Origin: *
12:09:34.413 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Connection: close
12:09:34.413 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Type: text/plain; charset=UTF-8
12:09:34.419 [main] DEBUG org.apache.http.impl.conn.DefaultManagedHttpClientConnection - http-outgoing-0: Close connection
12:09:34.420 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Connection discarded
12:09:34.420 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection released: [id: 0][route: {}->http://<someIP>][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
Cookies! []
을
내 생각 : 콘솔 출력에서 볼 수있는 지원되지 않는 미디어 유형에 문제가있을 수 있지만 해결 방법을 모르겠습니다. (
편집 : 앱에 대한 추가 정보를 제공하라는 조언을 받았으므로 여기서는 간략히 설명합니다. 작업은 다양한 요청에서 REST API로 전달되는 쿠키를 얻는 것입니다. 이렇게하려면 먼저 쿠키를 제공 할 사용자 이름과 암호로 (POST) 인증을 받아야합니다. 그런 다음 쿠키가 다른 모든 REST 요청에서 동일한 지 확인해야합니다 (POST 메서드를 Postman 및 그것은 작동한다). 인정하기 위해, Java에서 코딩하는 것은 처음입니다. REST 아키텍처가 포함되어 있습니다. 기본 사항에 대한 기본 지식을 알고 있지만 Google을 선택했기 때문에 여기에서 나를 안내합니다. 그래서 .. 필자가 필요로하는 코드가 아니면 단순화 할 수 있는가? 답변에 대한 타이, 난 아주 고마워요 :)
제목을 질문으로 변경해야하며 쿠키와 관련이 없습니다. –