'How to Implement Okta Single Sign-On (SAML) authentication in spring boot with passing dynamic metadata url

I'm working on Okta Single Sign-On (SAML) authentication I have created SAML application in okta but in my system I want to save multiple idp and on basis of idp select user will redirect to okta authentication I can not able to find any solution for this Currently I'm storing metadata-url in application.properties

Here is my code

@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Value("${security.saml2.metadata-url}")
String metadataUrl;

@Value("${server.ssl.key-alias}")
String keyAlias;

@Value("${server.ssl.key-store-password}")
String password;

@Value("${server.port}")
String port;

@Value("${server.ssl.key-store}")
String keyStoreFilePath;

@Override
protected void configure(final HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers("/saml*").permitAll()
            .anyRequest().authenticated()
            .and()
        .apply(saml())
            .serviceProvider()
                .keyStore()
                    .storeFilePath(this.keyStoreFilePath)
                    .password(this.password)
                    .keyname(this.keyAlias)
                    .keyPassword(this.password)
                    .and()
                .protocol("https")
                .hostname(String.format("%s:%s", "localhost", this.port))
                .basePath("/")
                .and()
            .identityProvider()
            .metadataFilePath(this.metadataUrl);
}
}

@Controller
public class IndexController {

@RequestMapping("/")
public String index(ExpiringUsernameAuthenticationToken userToken, Model model) {
     System.out.println("Loading home page");
     System.out.println("*******************************************");
     
     SAMLCredential credential = (SAMLCredential) userToken.getCredentials();
     List<Attribute>  attributes = credential.getAttributes(); 
     System.out.println("userToken.getName()" + userToken.getName());
     
     for( Attribute data : attributes ) {
         System.out.println(data.getName() + " == " + credential.getAttributeAsString(data.getName()));
         
         if(!StringUtils.isBlank(data.getName())) {
             
             switch(data.getName().trim()) {
             
               case "email" :
                   model.addAttribute(data.getName(), credential.getAttributeAsString(data.getName()));
                   break;
                   
               case "firstName" :
                   model.addAttribute(data.getName(), credential.getAttributeAsString(data.getName()));
                   break;
              
               case "lastName" :
                   model.addAttribute(data.getName(), credential.getAttributeAsString(data.getName()));
                   break;
                 
               case "group" :
                   model.addAttribute(data.getName(), credential.getAttributeAsString(data.getName()));
                   break;
             
                   default:
                       break;
             }
             
             
         }
         
     }
     
    
    return "home";
}

}


server.port = 8443
server.ssl.enabled = true
server.ssl.key-alias = spring
server.ssl.key-store = classpath:saml/keystore.jks
server.ssl.key-store-password = secret
security.saml2.metadata-url = https://dev- 
99318079.okta.com/app/exk3ibo4bojjhOY6e5d7/sso/saml/metadata


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source