gpt4 book ai didi

java - Spring Security SAML2 使用 G Suite 作为 Idp

转载 作者:行者123 更新时间:2023-12-04 04:06:18 25 4
gpt4 key购买 nike

我正在尝试使用 Spring Security (5.3.3.RELEASE) 在 Spring Boot 应用程序中处理 SAML2 身份验证。作为 SP 的 Spring Boot 应用程序和 G Suite 将作为 IDP。

在我的 Maven pom.xml 文件中我有:

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-saml2-service-provider</artifactId>
</dependency>

在我的代码中我有:


@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Bean
public RelyingPartyRegistration googleRegistration() throws CertificateException {

final String idpEntityId = "https://accounts.google.com/o/saml2?idpid=REDACTED";
final String webSsoEndpoint = "https://accounts.google.com/o/saml2/idp?idpid=REDACTED";
final String registrationId = "gsuite";
final String localEntityIdTemplate = "{baseUrl}/saml2/service-provider-metadata/{registrationId}";
final String acsUrlTemplate = "{baseUrl}/login/saml2/sso/{registrationId}";
final byte[] certBytes = ("-----BEGIN CERTIFICATE-----\n" +
"REDACTED\n" +
"-----END CERTIFICATE-----").getBytes();
final InputStream is = new ByteArrayInputStream(certBytes);
final CertificateFactory cf = CertificateFactory.getInstance("X.509");
final X509Certificate cert = (X509Certificate) cf.generateCertificate(is);

final Saml2X509Credential credential = new Saml2X509Credential(cert,
Saml2X509CredentialType.SIGNING); // THIS IS THE PROBLEM

return RelyingPartyRegistration.withRegistrationId(registrationId)
.providerDetails(config -> config.entityId(idpEntityId))
.providerDetails(config -> config.webSsoUrl(webSsoEndpoint))
.credentials(c -> c.add(credential))
.localEntityIdTemplate(localEntityIdTemplate)
.assertionConsumerServiceUrlTemplate(acsUrlTemplate)
.build();
}

@Bean
public RelyingPartyRegistrationRepository relyingPartyRegistrationRepository(
final RelyingPartyRegistration googleRegistration) {

return new InMemoryRelyingPartyRegistrationRepository(googleRegistration);
}

@Override
protected void configure(final HttpSecurity http) throws Exception {

http
.authorizeRequests(authorize -> authorize
.anyRequest().authenticated())
.saml2Login();
}
}

问题是我需要一个签名 key ,但是行 final Saml2X509Credential credential = new Saml2X509Credential(cert, Saml2X509CredentialType.SIGNING); 抛出一个异常,因为你必须传递一个 PrivateKey 到该构造函数中,以便将其用于 SIGNING 类型。但是,如果我使用该凭据进行验证,应用程序将失败,但需要签名 key 。

G Suite 仅提供一个元数据 XML 文件(Spring Security 不支持)和一个 .pem 文件。我将 .pem 文件中的所有文本复制到上面的字符串中以生成 X509 证书。

在 Spring Security SAML 的文档中,他们显示了 2 个证书,但 G Suite 只提供了 1 个。我应该从 .pem 文件生成一个私钥吗?如果是,怎么办?

最佳答案

成功了!

关键是禁用签名。

    @Bean
public RelyingPartyRegistration googleRegistration() throws CertificateException {

// remote IDP entity ID
final String idpEntityId = "https://accounts.google.com/o/saml2?idpid=REDACTED";
// remote WebSSO Endpoint - Where to Send AuthNRequests to
final String webSsoEndpoint = "https://accounts.google.com/o/saml2/idp?idpid=REDACTED";
// local registration ID
final String registrationId = "gsuite";
// local entity ID - autogenerated based on URL
final String localEntityIdTemplate = "{baseUrl}/saml2/service-provider-metadata/{registrationId}";
// local SSO URL - autogenerated, endpoint to receive SAML Response objects
final String acsUrlTemplate = "{baseUrl}/login/saml2/sso/{registrationId}";
// local signing (and local decryption key and remote encryption certificate)
final byte[] certBytes = ("-----BEGIN CERTIFICATE-----\n" +
"REDACTED\n" +
"-----END CERTIFICATE-----").getBytes();
final InputStream is = new ByteArrayInputStream(certBytes);
final CertificateFactory cf = CertificateFactory.getInstance("X.509");
final X509Certificate cert = (X509Certificate) cf.generateCertificate(is);

final Saml2X509Credential credential = new Saml2X509Credential(cert,
Saml2X509CredentialType.VERIFICATION, Saml2X509CredentialType.ENCRYPTION);

return RelyingPartyRegistration.withRegistrationId(registrationId)
.providerDetails(config -> config.entityId(idpEntityId))
.providerDetails(config -> config.webSsoUrl(webSsoEndpoint))
.providerDetails(config -> config.signAuthNRequest(false)) // THIS IS THE KEY
.credentials(c -> c.add(credential))
.localEntityIdTemplate(localEntityIdTemplate)
.assertionConsumerServiceUrlTemplate(acsUrlTemplate)
.build();
}

关于java - Spring Security SAML2 使用 G Suite 作为 Idp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62419768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com