CAS SunCertPathBuilderException:无法找到请求目标的有效证书路径

时间:2017-07-21 12:59:50

标签: java tomcat ssl ssl-certificate cas

我已在我的计算机中配置CAS以处理特定域(例如a.com),该域工作正常。在那里,我有一对.crt和.key文件以及代码。现在需要更改域名,所以我做的是,相应地更改了源代码中的域(比如b.com)并导入了我收到的.crt和.key文件。现在,当我访问CAS登录页面时,我可以访问它。但是当我提供登录凭据并单击登录按钮时,它会失败并出现以下异常。

sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
    sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
    java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
    sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
    sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
    sun.security.validator.Validator.validate(Validator.java:260)
    sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
    sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
    sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
    sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1496)
    sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
    sun.security.ssl.Handshaker.processLoop(Handshaker.java:1026)
    sun.security.ssl.Handshaker.process_record(Handshaker.java:961)
    sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
    sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
    sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
    sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
    sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
    sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1546)
    sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
    sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    org.jasig.cas.client.util.CommonUtils.getResponseFromServer(CommonUtils.java:429)
    org.jasig.cas.client.validation.AbstractCasProtocolUrlBasedTicketValidator.retrieveResponseFromServer(AbstractCasProtocolUrlBasedTicketValidator.java:41)
    org.jasig.cas.client.validation.AbstractUrlBasedTicketValidator.validate(AbstractUrlBasedTicketValidator.java:193)
    org.springframework.security.cas.authentication.CasAuthenticationProvider.authenticateNow(CasAuthenticationProvider.java:158)
    org.springframework.security.cas.authentication.CasAuthenticationProvider.authenticate(CasAuthenticationProvider.java:143)
    org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:174)
    org.springframework.security.cas.web.CasAuthenticationFilter.attemptAuthentication(CasAuthenticationFilter.java:270)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.jasig.cas.client.session.SingleSignOutFilter.doFilter(SingleSignOutFilter.java:97)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:121)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:121)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:66)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)

有人可以告诉我这里发生了什么事吗?我看过很多类似的问题,但似乎没有一个问题可以解决我的问题。

2 个答案:

答案 0 :(得分:1)

原来我的Tomcat配置存在问题。即使我已将密钥导入密钥库,但tomcat仍未访问密钥库,导致无法读取证书。

这似乎适用于下载和提取tomcat发行版的二进制文件而不是使用<span><details></details></span> 等软件包管理器的人。

目前有两种可能的选择(可能还有更多)。

  1. 修改catalina.sh以添加属性apt-get以包含所需的信任库文件。基本上,属性和值将附加到上面脚本中的-Djavax.net.ssl.trustStore变量。例如。 JAVA_OPTS。但这只是一个快速修复。我不认为这是一个很好的解决方案,因为信任存储正在全球范围内添加。
  2. 将密钥库位置添加到JAVA_OPTS="$JAVA_OPTS -Djavax.net.ssl.trustStore=$JAVA_HOME/jre/lib/security/cacerts"配置文件中的connector属性。可以在here找到示例。
  3. 我现在意识到这是一个非常简单的基本问题。但是为了支持学习者,我想留下问题和答案。请进一步改进这个答案。

答案 1 :(得分:1)

来自CAS docs

PKIX路径构建错误是最常见的SSL错误。这里的问题是CAS客户端不信任CAS服务器提供的证书。最常见的原因是在CAS服务器上使用了自签名证书。要解决此错误,请将CAS服务器证书导入到CAS客户端的系统信任库中。如果证书是由您自己的PKI颁发的,最好将PKI的根证书导入到CAS客户端信任库中。

默认情况下,Java系统信任库位于$ JAVA_HOME / jre / lib / security / cacerts。要导入的证书必须是DER编码的文件。

因此,如果您拥有密钥存储文件,例如说<!DOCTYPE html> <html> <head> <title>Example</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="input-group mb-3"> <input id="myinput" type="text" class="form-control" name="myinput"> <div class="input-group-append" id="mySearchButton" > <button class="btn btn-primary" type="button" > <img style="" src="https://www.google.se/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"> </button> </div> </div> </div> </body> </html> ,则首先导出服务器的证书:

store.jks

[请注意,别名在您的密钥库中可能有所不同]。接下来,将证书移至keytool -exportcert -keystore store.jks -alias server -file server.crt 目录中,并将其导入客户端的信任库(此处,客户端是您的JVM):

$JAVA_HOME/jre/lib/security/cacerts

[必须以管理员权限调用最后一个命令!]

请注意,如果您在localhost上运行此证书,则证书的CN必须为“ localhost”。

就这样,现在启动服务器并享受。