|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package example; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.security.cert.CertificateException; |
| 22 | +import java.security.cert.CertificateFactory; |
| 23 | +import java.security.cert.X509Certificate; |
| 24 | +import java.security.interfaces.RSAPrivateKey; |
| 25 | + |
| 26 | +import org.springframework.boot.context.properties.ConfigurationProperties; |
| 27 | +import org.springframework.boot.io.ApplicationResourceLoader; |
| 28 | +import org.springframework.core.io.Resource; |
| 29 | +import org.springframework.core.io.ResourceLoader; |
| 30 | +import org.springframework.security.saml2.core.Saml2X509Credential; |
| 31 | +import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration; |
| 32 | +import org.springframework.security.saml2.provider.service.registration.Saml2MessageBinding; |
| 33 | +import org.springframework.stereotype.Component; |
| 34 | + |
| 35 | +@Component |
| 36 | +@ConfigurationProperties("saml2") |
| 37 | +public class RelyingPartyMetadata { |
| 38 | + |
| 39 | + private final ResourceLoader resourceLoader = new ApplicationResourceLoader(); |
| 40 | + |
| 41 | + private String entityId = "{baseUrl}/saml2/metadata"; |
| 42 | + |
| 43 | + private String sso = "{baseUrl}/login/saml2/sso"; |
| 44 | + |
| 45 | + private SingleLogout slo = new SingleLogout(); |
| 46 | + |
| 47 | + private X509Certificate certificate; |
| 48 | + |
| 49 | + private RSAPrivateKey key; |
| 50 | + |
| 51 | + public RelyingPartyRegistration apply(RelyingPartyRegistration.Builder builder) { |
| 52 | + Saml2X509Credential signing = Saml2X509Credential.signing(this.key, this.certificate); |
| 53 | + return builder.entityId(this.entityId) |
| 54 | + .assertionConsumerServiceLocation(this.sso) |
| 55 | + .singleLogoutServiceBinding(this.slo.getBinding()) |
| 56 | + .singleLogoutServiceLocation(this.slo.getLocation()) |
| 57 | + .singleLogoutServiceResponseLocation(this.slo.getResponseLocation()) |
| 58 | + .signingX509Credentials((c) -> c.add(signing)) |
| 59 | + .build(); |
| 60 | + } |
| 61 | + |
| 62 | + public void setEntityId(String entityId) { |
| 63 | + this.entityId = entityId; |
| 64 | + } |
| 65 | + |
| 66 | + public void setSso(String sso) { |
| 67 | + this.sso = sso; |
| 68 | + } |
| 69 | + |
| 70 | + public void setSlo(SingleLogout slo) { |
| 71 | + this.slo = slo; |
| 72 | + } |
| 73 | + |
| 74 | + public void setCertificate(String certificate) { |
| 75 | + Resource source = this.resourceLoader.getResource(certificate); |
| 76 | + try (InputStream in = source.getInputStream()) { |
| 77 | + CertificateFactory certificates = CertificateFactory.getInstance("X.509"); |
| 78 | + this.certificate = (X509Certificate) certificates.generateCertificate(in); |
| 79 | + } |
| 80 | + catch (CertificateException | IOException ex) { |
| 81 | + throw new IllegalArgumentException(ex); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public void setKey(RSAPrivateKey key) { |
| 86 | + this.key = key; |
| 87 | + } |
| 88 | + |
| 89 | + public static class SingleLogout { |
| 90 | + |
| 91 | + private Saml2MessageBinding binding = Saml2MessageBinding.REDIRECT; |
| 92 | + |
| 93 | + private String location = "{baseUrl}/logout/saml2/slo"; |
| 94 | + |
| 95 | + private String responseLocation = "{baseUrl}/logout/saml2/slo"; |
| 96 | + |
| 97 | + public Saml2MessageBinding getBinding() { |
| 98 | + return this.binding; |
| 99 | + } |
| 100 | + |
| 101 | + public void setBinding(Saml2MessageBinding binding) { |
| 102 | + this.binding = binding; |
| 103 | + } |
| 104 | + |
| 105 | + public String getLocation() { |
| 106 | + return this.location; |
| 107 | + } |
| 108 | + |
| 109 | + public void setLocation(String location) { |
| 110 | + this.location = location; |
| 111 | + } |
| 112 | + |
| 113 | + public String getResponseLocation() { |
| 114 | + if (this.responseLocation == null) { |
| 115 | + return this.location; |
| 116 | + } |
| 117 | + return this.responseLocation; |
| 118 | + } |
| 119 | + |
| 120 | + public void setResponseLocation(String responseLocation) { |
| 121 | + this.responseLocation = responseLocation; |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments