如何在 Spring 配置文件中设置 WebSSOProfileConsumerImpl

Posted

技术标签:

【中文标题】如何在 Spring 配置文件中设置 WebSSOProfileConsumerImpl【英文标题】:How to set WebSSOProfileConsumerImpl in your Spring Configuration file 【发布时间】:2018-10-02 06:47:15 【问题描述】:

我面临将 WebSSOProfileConsumerImpl 指定到我的 Spring 配置文件的问题。我正在尝试修改此 bean 中的 responseSkew,但在为 WebSSOProfileConsumerImpl 添加配置后,我遇到了 MetadataManager 问题


应用程序启动失败


说明:

org.springframework.security.saml.websso.AbstractProfileBase 中方法 setMetadata 的参数 0 需要一个无法找到的 'org.springframework.security.saml.metadata.MetadataManager' 类型的 bean。

行动:

考虑定义一个 'org.springframework.security.saml.metadata.MetadataManager' 类型的 bean

谁能帮我解决这个问题?

我已经浏览了链接:http://docs.spring.io/spring-security-saml/docs/current/reference/html/configuration-advanced.html,但它没有指定如何在配置中进行设置。

我的代码

    import static org.springframework.security.extensions.saml2.config.SAMLConfigurer.saml;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.saml.websso.WebSSOProfileConsumer;
import org.springframework.security.saml.websso.WebSSOProfileConsumerImpl;

@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;

    @Value("$security.saml2.responseSkew")
    int responseSkew = 0;


    @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).and();

       /* Map<? extends Object, Object> sharedObjects = new Map<? extends Object>, Object>(http.getSharedObjects());
        sharedObjects.put(WebSSOProfileConsumer.class, webSSOprofileConsumerImpl());*/

    

    @Bean
    @Qualifier("webSSOprofileConsumer")
    public WebSSOProfileConsumer webSSOprofileConsumerImpl() 
        WebSSOProfileConsumerImpl consumerImpl = new WebSSOProfileConsumerImpl();
        consumerImpl.setResponseSkew(this.responseSkew);
        return consumerImpl;
     


【问题讨论】:

你有没有机会解决这个问题? 【参考方案1】:

如果您不需要将 WebSSOProfileConsumer 作为 bean 访问应用程序的其余部分,您可以在 configure 方法中创建它,如下所示:

@Override
protected void configure(final HttpSecurity http) throws Exception 
    WebSSOProfileConsumerImpl consumerImpl = new WebSSOProfileConsumerImpl();
    consumerImpl.setResponseSkew(this.responseSkew);

    http
        .authorizeRequests()
            .antMatchers("/saml*").permitAll()
            .anyRequest().authenticated()
            .and()
        .apply(saml())
            .serviceProvider()
                .ssoProfileConsumer(consumerImpl)  // <-- added here
                .keyStore()
                // Your method continues as before

我不记得在您的情况下常规连接失败的确切原因,但它的要点是 configure 设置了一个构建器对象而不是常规 bean。通常,ServiceProviderBuilder 会在其构建步骤中提供 WebSSOProfileConsumer 所需的 MetadataManager,但如果您自动装配配置文件使用者,您将不会获得为您提供的 MetadataManager,因为假定您的自动装配对象已经完成。

(我在寻找配置最大身份验证年龄的方法时了解到这一点,因为在某些情况下 spring-security-saml 中的两个小时默认值低于身份提供者使用的默认值。这有效地将您的用户锁定在您的应用程序,直到身份提供者的最大身份验证年龄已过。)

【讨论】:

我没有办法使用 serviceProvider 的“ssoProfileConsumer”方法。对此有何见解?

以上是关于如何在 Spring 配置文件中设置 WebSSOProfileConsumerImpl的主要内容,如果未能解决你的问题,请参考以下文章