【问题标题】:Apache oltu oauth2 ask token over SSLApache oltu oauth2 通过 SSL 询问令牌
【发布时间】:2015-02-26 18:18:54
【问题描述】:

有没有办法使用 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;
        }
    });
}

【问题讨论】:

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


    【解决方案1】:

    URLConnectionClient 使用 HttpsURLConnection 所以你的代码应该可以工作;你试过吗?

    【讨论】:

    • 设置 DefaultHostnameVerifier 时,出现以下异常:org.apache.oltu.oauth2.common.exception.OAuthSystemException: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at org.apache.oltu.oauth2.client.URLConnectionClient.execute(URLConnectionClient.java:108)。这就是为什么我认为它不起作用...
    • 好的,所以您使用自签名/不受信任的证书以及与主机名不匹配的证书;那么您不仅需要覆盖主机名验证,还需要覆盖stackoverflow.com/questions/11857417/…中所述的信任管理器
    猜你喜欢
    • 2015-11-11
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2017-12-10
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    相关资源
    最近更新 更多