嗬嗬!
由于来自 Kerberos 代码深处的异常,我尝试使用 Spring Security 5 和 Kerberos 通过 SSO 对用户进行身份验证失败。我将首先显示堆栈跟踪和导致它的代码,然后提供有关我的环境的其他信息,这可能有助于消除一些可能性。
堆栈跟踪
WARN 3932 --- [apr-8080-exec-1] w.a.SpnegoAuthenticationProcessingFilter : Negotiate Header was invalid: Negotiate YIILSwYGKwYBBQUCoIILPzCCCzugMDAuBgkqhkiC9xIBAgIGCSqGSIb3EgECAgYKKwYBBAGCN[and so on]
org.springframework.security.authentication.BadCredentialsException: Kerberos validation not successful
at org.springframework.security.kerberos.authentication.sun.SunJaasKerberosTicketValidator.validateTicket(SunJaasKerberosTicketValidator.java:71) ~[spring-security-kerberos-core-1.0.1.RELEASE.jar:1.0.1.RELEASE]
at org.springframework.security.kerberos.authentication.KerberosServiceAuthenticationProvider.authenticate(KerberosServiceAuthenticationProvider.java:64) ~[spring-security-kerberos-core-1.0.1.RELEASE.jar:1.0.1.RELEASE]
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:174) ~[spring-security-core-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:199) ~[spring-security-core-5.1.1.RELEASE.jar:5.1.1.RELEASE]
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$AuthenticationManagerDelegator.authenticate(WebSecurityConfigurerAdapter.java:512) ~[spring-security-config-5.1.1.RELEASE.jar:5.1.1.RELEASE]
...
Caused by: java.security.PrivilegedActionException: null
at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_162]
at javax.security.auth.Subject.doAs(Subject.java:422) ~[na:1.8.0_162]
at org.springframework.security.kerberos.authentication.sun.SunJaasKerberosTicketValidator.validateTicket(SunJaasKerberosTicketValidator.java:68) ~[spring-security-kerberos-core-1.0.1.RELEASE.jar:1.0.1.RELEASE]
...
Caused by: org.ietf.jgss.GSSException: No valid credentials provided (Mechanism level: Failed to find any Kerberos credentails)
at sun.security.jgss.krb5.Krb5AcceptCredential.getInstance(Krb5AcceptCredential.java:87) ~[na:1.8.0_162]
at sun.security.jgss.krb5.Krb5MechFactory.getCredentialElement(Krb5MechFactory.java:127) ~[na:1.8.0_162]
at sun.security.jgss.krb5.Krb5MechFactory.getMechanismContext(Krb5MechFactory.java:198) ~[na:1.8.0_162]
所以有一段
BadCredentialsException
时间我SunJaasKerberosTicketValidator
正在验证 SSO 票证。这只是重新抛出一个PrivilegedActionException
来自public KerberosTicketValidation validateTicket(byte[] token) { try { return Subject.doAs(this.serviceSubject, new KerberosValidateAction(token)); } catch (PrivilegedActionException e) { throw new BadCredentialsException("Kerberos validation not successful", e); }
}
PrivilegedActionException
很难跟踪,因为它来自native
方法java.security.AccessController.doPrivileged
。我不知道实施。我觉得有趣的是PrivilegedActionException
打印出来的Caused by: java.security.PrivilegedActionException: null
PrivilegedActionException.toString
方法是public String toString() { String s = getClass().getName(); return (exception != null) ? (s + ": " + exception.toString()) : s; }
所以
exception
(原因异常)不为空,但它打印为null
...然而,堆栈跟踪告诉我们问题的根源是
GSSException
来自Krb5AcceptCredential类。if (creds == null) throw new GSSException(GSSException.NO_CRED, -1,"Failed to find any Kerberos credentails");
并且
creds == null
是因为Krb5Util.getServiceCreds
(参见实现)返回null
而不会导致异常。
这就是我到现在为止的距离。现在一些额外的信息。
在我的 WebSecurityConfig 中创建票证验证器
SunJaasKerberosTicketValidator ticketValidator = new SunJaasKerberosTicketValidator();
ticketValidator.setServicePrincipal("HTTP/host@REALM");
FileSystemResource fs = new FileSystemResource("PATH_TO_KEYTAB");
ticketValidator.setKeyTabLocation(fs);
LOGGER.info(fs.exists()); // prints 'true'
创建 KerberosServiceAuthenticationProvider
这是将抛出的对象的配置BadCredentialsException
。
KerberosServiceAuthenticationProvider provider = new KerberosServiceAuthenticationProvider();
provider.setTicketValidator(sunJaasKerberosTicketValidator());
provider.setUserDetailsService(myUserDetailService);
provider.supports(KerberosServiceRequestToken.class);
我知道 SSO 有效
我有幸能够证明我公司的 SSO 基础设施有效。同一台服务器正在运行另一个应用程序(带有 Kerberos 的 Spring Security 4),其中用户可以通过 SSO 成功进行身份验证。所以我的设置很可能有问题。
顺便说一下,我用的是 Chrome,但我也用 IE 测试过。
如果您需要我的其他信息WebSecurityConfig
或其他信息,我会提供。愿原力与你同在 :-)
其他问题
这是我迄今为止发现的,但这些示例略有不同。
- GSSException:未提供有效凭据(机制级别:未能找到任何 Kerberos tgt)
- 其他错误信息
- 其他设置:我没有 krb5.conf 文件
- BadCredentialsException:Kerberos 验证不成功
- 导致的其他根异常
BadCredentialsException
- 从 2012 年开始
- 导致的其他根异常
- Kerberos 身份验证不适用于 Spring Security
- 导致的其他根异常
BadCredentialsException
- 导致的其他根异常