Search code examples
javaspringspring-bootsingle-sign-onsaml-2.0

Need to configure generic SAML 2.0 with springboot


I have created spring boot application and added SAML but I have to configure generic SAML in spring boot after running my application. Is there any way to add configuration for SSO after running spring boot application.

we are expecting to set up a system where user can dynamically set there SAML configuration without restarting system.


Solution

  • Answering my own question.

    If you have to configure generic SAML in spring boot in runtime. You have to implements org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository and java.lang.Iterable(Same as InMemoryRelyingPartyRegistrationRepository)

    In InMemoryRelyingPartyRegistrationRepository ther is one HashMap private final Map<String, RelyingPartyRegistration> byRegistrationId; . You have to make this map public and put new RelyingPartyRegistration in this map. Make sure that key of byRegistrationId is same while putting new RelyingPartyRegistration in Map.

    public class CustomRelyingPartyRegistrationRepository implements RelyingPartyRegistrationRepository, Iterable<RelyingPartyRegistration> {
    
        private Map<String, RelyingPartyRegistration> byRegistrationId = new HashMap<>();
    
        public CustomRelyingPartyRegistrationRepository(SsoConfigService ssoConfigService) {
            RelyingPartyRegistration relyingParty = ssoConfigService.getRelyingPartyRegistration(); // need default SAML RelyingPartyRegistration to start application 
            setByRegistrationId(relyingParty);
        }
    
        @Override
        public RelyingPartyRegistration findByRegistrationId(String registrationId) {
            return this.byRegistrationId.get(registrationId);
        }
    
        @Override
        public Iterator<RelyingPartyRegistration> iterator() {
            return this.byRegistrationId.values().iterator();
        }
    
        public void setByRegistrationId(RelyingPartyRegistration relyingPartyRegistration) {
            byRegistrationId.put("saml", relyingPartyRegistration);
        }
    }