0
私のSpring Boot/Spring Securityアプリケーションで、Spring RestTemplateでログインしようとしています。Spring RestTemplateがJSESSIONIDクッキーを取得できません
これはコードです:
final KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(new FileInputStream(new File("keystore.p12")), "changeit".toCharArray());
final SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).loadKeyMaterial(keyStore, "changeit".toCharArray()).build();
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
final HttpClient httpClient = HttpClientBuilder.create().setSSLSocketFactory(socketFactory).setRedirectStrategy(new LaxRedirectStrategy()).build();
final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
final RestTemplate restTemplate = new RestTemplate(requestFactory);
final HttpHeaders headers = new HttpHeaders();
headers.add("Cookie", "JSESSIONID=" + loginResponse.getJsessionid());
headers.add("X-XSRF-TOKEN", loginResponse.getCsrf());
final MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>();
body.add("username", "username");
body.add("password", "password");
final HttpEntity<?> requestEntity = new HttpEntity<Object>(body, headers);
final ResponseEntity<String> responseEntity = restTemplate.exchange("https://localhost:" + port + "/api/login", HttpMethod.POST, requestEntity, String.class);
これは私がresponseEntity
ヘッダに受けてるものです:あなたが見ることができるように
Server = [Apache-Coyote/1.1]
X-Content-Type-Options = [nosniff]
X-XSS-Protection = [1; mode=block]
Cache-Control = [no-cache, no-store, max-age=0, must-revalidate]
Pragma = [no-cache]
Expires = [0]
Strict-Transport-Security = [max-age=31536000 ; includeSubDomains]
Set-Cookie = [XSRF-TOKEN=cf1968b0-068b-455b-be8f-10e39e0e44a4; Path=/]
X-Application-Context = [application:0]
Content-Type = [text/plain;charset=ISO-8859-1]
Content-Length = [12]
Date = [Thu, 21 Apr 2016 19:32:34 GMT]
- だけXSRF-TOKEN
クッキーが、無JSESSIONID
があります。
からhttps://localhost/api/loginで認証が成功した後にリダイレクトすることができると思いますか? JSESSIONID
クッキーを受け取る方法も?
の問題を修正した 'これは私がrequestEntityヘッダに受けてるものです:'これは、サーバー側の要求ですか?はいの場合、 'Server'ヘッダのために奇妙です。存在しない' X-XSRF-TOKEN'ヘッダ –