Created
September 16, 2014 18:13
-
-
Save anthavio/98885c6155c7ec991ec9 to your computer and use it in GitHub Desktop.
Spring OAuth2RestTemplate and Token Edpoint with self-signed certificate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SSLContextRequestFactory extends SimpleClientHttpRequestFactory { | |
private final SSLContext sslContext; | |
public SSLContextRequestFactory(SSLContext sslContext) { | |
this.sslContext = sslContext; | |
} | |
@Override | |
protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { | |
if (connection instanceof HttpsURLConnection) { | |
((HttpsURLConnection) connection).setSSLSocketFactory(sslContext.getSocketFactory()); | |
} | |
super.prepareConnection(connection, httpMethod); | |
} | |
} | |
class Dumb509TrustManager implements X509TrustManager { | |
@Override | |
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) | |
throws CertificateException { | |
} | |
@Override | |
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) | |
throws CertificateException { | |
} | |
@Override | |
public java.security.cert.X509Certificate[] getAcceptedIssuers() { | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@EnableOAuth2Client | |
public class OAuthResourceConfiguration { | |
@Resource | |
@Qualifier("accessTokenRequest") | |
private AccessTokenRequest accessTokenRequest; | |
@Bean | |
@Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES) | |
public OAuth2RestTemplate someOAuthRestTemplate() { | |
OAuth2ProtectedResourceDetails resource = ...; | |
OAuth2ClientContext context = ...; | |
OAuth2RestTemplate oauthTemplate = new OAuth2RestTemplate(resource, context); | |
disableCertificateChecking(oauthTemplate); | |
return oauthTemplate; | |
} | |
private static void disableCertificateChecks(OAuth2RestTemplate oauthTemplate) throws Exception { | |
SSLContext sslContext = SSLContext.getInstance("TLS"); | |
sslContext.init(null, new TrustManager[] { new DumbX509TrustManager() }, null); | |
ClientHttpRequestFactory requestFactory = new SSLContextRequestFactory(sslContext); | |
//This is for OAuth protected resources | |
oauthTemplate.setRequestFactory(requestFactory); | |
//AuthorizationCodeAccessTokenProvider creates it's own RestTemplate for token operations | |
AuthorizationCodeAccessTokenProvider provider = new AuthorizationCodeAccessTokenProvider(); | |
provider.setRequestFactory(requestFactory); | |
oauthTemplate.setAccessTokenProvider(provider); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey man, I'm trying to use your code but it is still not working.
Here is what I tried:
Do you have any sugestions?