gpt4 book ai didi

java - 如何在 Spring Security 的 SAML 扩展中注册 AuthenticationSuccessHandler 或 SAMLRelayStateSuccessHandler?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:49:39 26 4
gpt4 key购买 nike

tldr;注册自定义 AuthenticationSuccessHandler 或配置 SAMLRelayStateSuccessHandler 以在身份验证成功后设置重定向 URL Spring Security 的 SAML 扩展的合适方法是什么?

我是 Spring 和 Spring Security 的新手,但我已经成功地将 SAML 扩展用于单点登录应用程序。但是,我遇到了一个问题,即 Okta(IDP)和我的应用程序在身份验证成功后将不断循环,而不是转到原始 URL。我怀疑这是由于我同时使用 Vaadin 和 Spring 而造成的,一旦加载 Vaadin servlet,它就会做一些奇怪的事情,或者完全忽略 Spring 过滤器。 combining filter-based Spring Security with Vaadin 上的博客文章建议了一个可能的问题和解决方案。其中建议的解决方案是注册 AuthenticationSuccessHandler。

但是,使用 Spring Security SAML 扩展并不像使用简单的表单登录或 OpenID 那样简单。例如,使用表单登录注册成功处理程序就像

http.formLogin().successHandler(handler);

SAML 扩展没有这样的选项。然而,在另一篇文章 Vladimír Schäfer(参见“Spring saml - 在 SP 上启动登录时如何记住请求参数,并在 IdP 响应后处理它们”)建议将 AuthenticationSuccessHandler 更改为 SAMLRelayStateSuccessHandler。文档表明这将是完美的方法它。但是,我不希望将 SAMLEntryPoint 子类化。

我当前的 SecurityConfiguration 完全基于 Java,并遵循 Matt Raible 关于一起使用 Spring 和 Okta 的文章。 SecurityConfiguration 如下:

@Override
protected void configure(final HttpSecurity http) throws Exception {

http
.csrf().disable() // Disable Spring's CSRF protection and use Vaadin's.
.authorizeRequests() // Everything else: Do SAML SSO
// Allow everyone to attempt to login
.antMatchers("/root/saml*").permitAll()
// But make sure all other requests are authenticated
.anyRequest().authenticated()
.and()
// Create a completely new session
.sessionManagement().sessionFixation().newSession()
.and()
// Configure the saml properties
.apply(saml())
.serviceProvider()
.keyStore()
.storeFilePath("saml/keystore.jks")
.password(this.password)
.keyname(this.keyAlias)
.keyPassword(this.password)
.and()
.protocol("https")
// Do I need to adjust this localhost piece?
.hostname(String.format("%s:%s", "localhost", this.port))
.basePath("/")
.and()
// Load the metadata from the stored properties
.identityProvider().metadataFilePath(this.metadataUrl);
}

```

注册 AuthenticationSuccessHandler 或配置 SAMLRelayStateSuccessHandler 的最佳方法是什么?任何其他想法也将不胜感激!

最佳答案

我正在使用:

@Bean
public SavedRequestAwareAuthenticationSuccessHandler successRedirectHandler() {
SavedRequestAwareAuthenticationSuccessHandler successRedirectHandler =
new SavedRequestAwareAuthenticationSuccessHandler() {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
super.onAuthenticationSuccess(request, response, authentication);
}
};
successRedirectHandler.setDefaultTargetUrl("/");
return successRedirectHandler;
}

它似乎避免了需要一个单独的 securityContext.xml 文件。

关于java - 如何在 Spring Security 的 SAML 扩展中注册 AuthenticationSuccessHandler 或 SAMLRelayStateSuccessHandler?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45866524/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com