Reputation: 99
I have a project on Spring Boot, in which I need to implement authorization via Twitter.
What could be simpler? - I asked myself, and then I got stuck for a few days.
The problem is that the most of the libraries I've tried (listening for /connect/twitter or /signin/twitter) return the same response
Invalid cookie header: "set-cookie: personalization_id=""; Expires=Tue, 28 Jul 2020 20:04:39 GMT; Path=/; Domain=.twitter.com". Invalid 'expires' attribute: Tue, 28 Jul 2020 20:04:39 GMT
My attention was attracted by the phrase:
Invalid 'expires' attribute
this can be solved by configuring RestTemplate, but the problem is that it is deeply created in the configurations and hidden by the private final fields. I think that there should exist a decision better than playing with reflection API, I just don't see it
Perhaps someone will be able to suggest the correct way of authorization through Twitter.
Tested Versions:
P.S. Facebook was perfectly working with spring-security-oauth2-jose and spring-security-oauth2-client, but twitter is not configured as a provider in it
Provider ID must be specified for client registration 'twitter'
Upvotes: 0
Views: 1086
Reputation: 26
Use this to configure your RestTemplate Bean to use standard cookie specs
@Bean
public RestTemplate restTemplate() {
return new RestTemplate(getClientHttpRequestFactory());
}
private HttpComponentsClientHttpRequestFactory getClientHttpRequestFactory() {
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
clientHttpRequestFactory.setHttpClient(getHttpClient());
return clientHttpRequestFactory;
}
private HttpClient getHttpClient() {
return HttpClients.custom()
.setDefaultRequestConfig(RequestConfig.custom()
.setCookieSpec(CookieSpecs.STANDARD).build())
.build();
}
Upvotes: 0
Reputation: 6771
This sounds like you need to configure the RestTemplate
to use the standard cookie spec. This could be achieved by setting the cookieSpec on the RequestConfig.Builder
and then via the HttpComponentsClientHttpRequestFactory
to finally create the RestTemplate
.
Somethning like:
@Bean
public RestTemplate restTemplate() {
RequestConfig requestConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.STANDARD)
.build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.mergeRequestConfig(requestConfig);
return new RestTemplate(factory);
}
Sorry, currently I don't have an IDE/javac available so this might not compile.
Upvotes: 0