Apache oltu oauth2通过SSL询问令牌

时间:2014-12-30 15:13:11

标签: java ssl spring-security oauth-2.0 oltu

有没有办法使用Apache oltu通过SSL请求访问令牌? 如果我不使用https(端口8443)但只使用http ...

,它的效果很好

我的代码:

OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());

OAuthClientRequest request = OAuthClientRequest.tokenLocation(MessageFormat.format("https://{0}:8443/applicationId/oauth/token", host)) //
        .setGrantType(GrantType.PASSWORD) //
        .setUsername("username") //
        .setPassword("password") //
        .setClientId("clientId") //
        .buildBodyMessage();

OAuthAccessTokenResponse oAuthResponse = oAuthClient.accessToken(request);

我收到以下异常消息:

    org.apache.oltu.oauth2.common.exception.OAuthSystemException: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching localhost found
at org.apache.oltu.oauth2.client.URLConnectionClient.execute(URLConnectionClient.java:108)
at org.apache.oltu.oauth2.client.OAuthClient.accessToken(OAuthClient.java:65)
at org.apache.oltu.oauth2.client.OAuthClient.accessToken(OAuthClient.java:55)
at org.apache.oltu.oauth2.client.OAuthClient.accessToken(OAuthClient.java:71)

我知道有这种方法可以通过覆盖HttpsURLConnection的HostnameVerifier来解决这个问题,但有没有办法在apache oltu中实现这一点?:

static {
    //for localhost testing only
    javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
    new javax.net.ssl.HostnameVerifier(){

        public boolean verify(String hostname,
                javax.net.ssl.SSLSession sslSession) {
            if (hostname.equals("localhost")) {
                return true;
            }
            return false;
        }
    });
}

1 个答案:

答案 0 :(得分:1)

URLConnectionClient使用HttpsURLConnection,因此您的代码应该有效;你试过吗?