【问题标题】:Spring Security XML configuration and Spring SAML in Java configurationJava 配置中的 Spring Security XML 配置和 Spring SAML
【发布时间】:2015-11-19 00:15:29
【问题描述】:

我在我的一个项目中使用 Spring Security,现在想介绍 Spring SAML。到目前为止,我已经使用了 Spring 的 XML 配置。我可以使用基于 Java 的配置集成 SAML 吗?

我是 SAML 集成的新手。

【问题讨论】:

    标签: java spring security spring-security spring-saml


    【解决方案1】:

    是的,您可以使用 Java 配置 Spring SAML,就像使用 Spring Security 的其余部分一样。

    你需要一个像这样配置类的 WebSecurityConfig 类

       protected void configure(HttpSecurity http) throws Exception {
        http
            .httpBasic()
                .authenticationEntryPoint(samlEntryPoint());
        http
            .csrf()
                .disable();
        http
            .addFilterBefore(metadataGeneratorFilter(), ChannelProcessingFilter.class)
            .addFilterAfter(samlFilter(), BasicAuthenticationFilter.class);
        http        
            .authorizeRequests()
            .antMatchers("/").permitAll()
            .antMatchers("/error").permitAll()
            .antMatchers("/saml/**").permitAll()
            .anyRequest().authenticated();
        http
            .logout()
                .logoutSuccessUrl("/");
    }
    

    您只需要使用 Java 将所有不同的 bean 一起编写,例如像这样设置 SecurityFilterChain

        public FilterChainProxy samlFilter() throws Exception {
        List<SecurityFilterChain> chains = new ArrayList<SecurityFilterChain>();
        chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/login/**"),
                samlEntryPoint()));
        chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/logout/**"),
                samlLogoutFilter()));
        chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/metadata/**"),
                metadataDisplayFilter()));
        chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/SSO/**"),
                samlWebSSOProcessingFilter()));
        chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/SSOHoK/**"),
                samlWebSSOHoKProcessingFilter()));
        chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/SingleLogout/**"),
                samlLogoutProcessingFilter()));
        chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/discovery/**"),
                samlIDPDiscovery()));
        return new FilterChainProxy(chains);
    }
    

    以这个项目https://github.com/vdenotaris/spring-boot-security-saml-sample 为例,了解它是如何完成的。 com.vdenotaris.spring.boot.security.saml.web.config.WebSecurityConfig.java 显示了秘方的成分。

    【讨论】:

    • 感谢您的回答,再问一个问题,如果我同时拥有 Spring Security(在 xml 配置中)和 Spring SAML(在 Java 配置中),什么会优先?有什么想法吗?
    • 不知道,但是你应该可以在应用程序启动时看到它。 Spring 会告诉你正在配置哪些 bean。更好的是自己控制初始化的顺序。以下是如何结合 XML 和 JavaConfig 的相关文档,并说明如何从 XML 引导 JavaConfig,反之亦然:docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/…
    【解决方案2】:

    您可以使用 xml 配置进行 SAML 集成。作为初学者很难从头开始创建它,因此我建议您下载 spring saml 示例应用程序并基于它创建您的配置。与现有应用程序的集成只是 Spring 安全集成。

    【讨论】:

    • 实际上我希望 SAML 配置基于 Java,因为这样我就可以根据一些数据库值启用/禁用它。
    • 如果您是 SAML 新手,我建议您基于现有示例创建配置存储,然后将配置更改为 java 定义。事实上,现有的官方示例是基于 xml 配置的。由于 config 基于 Spring Security 并且 security 具有 java config。我认为你可以管理它。
    猜你喜欢
    • 2016-10-24
    • 1970-01-01
    • 2017-03-03
    • 2014-12-07
    • 2017-07-17
    • 2018-07-12
    • 2014-02-25
    • 1970-01-01
    • 2018-07-24
    相关资源
    最近更新 更多