在 Java 应用程序中使用 Spring Security 和 SAML2 服务提供程序实现无状态会话

问题描述 投票:0回答:0

我的Java应用程序使用spring security和Saml2作为服务提供者。我能够与身份提供者集成并正确获取 saml 断言。我想避免在应用程序中使用 JSESSIONID 来增加开销,因为我有自定义会话属性

x-auth
作为 cookie。

但是,我看到每次都会创建一个会话,并保持持久状态,直到用户退出。

我尝试将以下行添加到配置方法中,

http.sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

但是,通过此设置,我总是被要求登录身份提供商。

如果我登录,它会返回到循环要求我再次登录的页面。

我不确定禁用会话时这是否是正确的行为。

任何人都可以为我提供使用以下代码使用无状态会话的正确方法吗?


 @Bean
  SecurityFilterChain configure(HttpSecurity http) throws Exception {
    OpenSaml4AuthenticationProvider authenticationProvider = new OpenSaml4AuthenticationProvider();
    authenticationProvider.setResponseAuthenticationConverter(createTokenCredential());

    Saml2MetadataFilter filter = new Saml2MetadataFilter(relyingPartyRegistrationRepository,
        new OpenSamlMetadataResolver());
    http.addFilterBefore(filter, Saml2WebSsoAuthenticationFilter.class);
    loginSuccessHandler.setTargetUrlParameter(Saml2ParameterNames.RELAY_STATE);

    http.authorizeHttpRequests(requests -> requests
            .requestMatchers("/saml2/service-provider-metadata/**")
            .permitAll()
        ).addFilterAfter(authTokenSessionRestore, SecurityContextHolderFilter.class)
        .saml2Login((saml2) -> saml2
            .loginProcessingUrl("/saml/SSO")
            .authenticationManager(new ProviderManager(authenticationProvider))
            .relyingPartyRegistrationRepository(relyingPartyRegistrationRepository)
            .successHandler(loginSuccessHandler)
            .failureHandler(loginFailureHandler)

        ).saml2Logout(withDefaults())
        .logout().logoutSuccessUrl("/login")
        .logoutSuccessHandler(logoutSuccessHandler);



    http.sessionManagement(session -> {
          session.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }); // dont create a session for this configuration
}

java spring spring-security saml-2.0 spring-security-saml2
© www.soinside.com 2019 - 2024. All rights reserved.