更改 Spring Security SAML2 登录的 URL
Posted
技术标签:
【中文标题】更改 Spring Security SAML2 登录的 URL【英文标题】:Altering URL for Spring Security SAML2 Login 【发布时间】:2022-01-17 22:32:46 【问题描述】:我有一个具有多种身份验证类型的应用程序(即基本和特殊的预授权登录)。我正在尝试在我的安全配置中添加 SAML2 RelyingParty 注册,我正在尝试从以下位置更改默认路径:
/login/saml2/sso/registrationId
到
/auth/saml2/registrationId
所以,我有以下设置:
public RelyingPartyRegistration provder1RelyingPartyRegistration()
RelyingPartyRegistration registration = RelyingPartyRegistrations
.fromMetadataLocation("classpath:provider1/metadata.xml")
.registrationId("provider1")
.assertionConsumerServiceLocation("baseUrl/auth/saml2/registrationId")
.build();
return registration;
// @Bean
public RelyingPartyRegistrationRepository relyingPartyRegistrationRepository()
Collection<RelyingPartyRegistration> registrations = Collections.unmodifiableList(Arrays.asList(provider1RelyingPartyRegistration()));
InMemoryRelyingPartyRegistrationRepository repository = new InMemoryRelyingPartyRegistrationRepository(registrations);
return repository;
// fluff
@Override
protected void configure(HttpSecurity http) throws Exception
final RequestMatcher filterRequestMatcher = new OrRequestMatcher(
new AntPathRequestMatcher("/auth/basic"),
new AntPathRequestMatcher("/auth/preauth")
);
ApplicationAuthenticationProcessingFilter filter = new ApplicationAuthenticationProcessingFilter(filterRequestMatcher, authenticationManagerBean());
filter.setAuthenticationSuccessHandler(successHandler());
filter.setAuthenticationFailureHandler(failureHandler());
http
.authorizeRequests()
.antMatchers("/**").permitAll()
.and()
.addFilterAfter(filter, LogoutFilter.class)
// fluff
.and()
.saml2Login()
.relyingPartyRegistrationRepository(relyingPartyRegistrationRepository())
.loginProcessingUrl("/auth/saml2/registrationId")
;
不幸的是,我明白了:
14 Dec 10:55:34 WARN [https-openssl-nio-127.0.0.1-444-exec-2] (DispatcherServlet.java:1278) - No mapping for POST /svc/auth/saml2/provider1
谁能告诉我试图改变这条路我做错了什么?我的应用程序不使用 Spring Boot,所以我只能手动配置。
编辑
一些调试导致这在Saml2LoginConfigurer
中达到这一行:
Map<String, String> providerUrlMap = getIdentityProviderUrlMap(
this.authenticationRequestEndpoint.filterProcessingUrl, this.relyingPartyRegistrationRepository);
不知何故,有一个默认的authenticationRequestEndpoint
(因为我没有定义)将filterProcessingUrl
设置为/saml2/authenticate/registrationId
。那么,我该如何覆盖呢?
【问题讨论】:
你能加logging.level.org.springframework.security=TRACE
看看日志里有没有什么吗?另外,请在Saml2WebSsoAuthenticationFilter
里面下一个断点,看看请求是否进来
我实际上正在这样做。为此,请求命中doFilterInternal()
,并绕过过滤器,因为检查了AntRequestMatcher
,默认情况下为"/saml2/authenticate/registrationId"
。这是使用名为 setRedirectMatcher()
的方法设置的,但在 configure()
方法中的 saml2login()
中似乎不是一个选项。
当您设置loginProcessingUrl
时,Saml2LoginConfigurer
应该将值传递给Saml2WebSsoAuthenticationFilter
。你能调试Saml2LoginConfigurer#init
方法并查看它使用的loginProcessingUrl
的值吗?
我刚刚发布了一个编辑,有一些自动配置正在进行,我不知道如何覆盖。
【参考方案1】:
loginProcessingUrl
在认证成功后被断言方调用,请求中包含SAMLResponse
参数。
您要更改的是处理身份验证请求的 URL(创建 SAMLRequest
并发送给断言方),这是由 Saml2WebSsoAuthenticationRequestFilter
类完成的。要更改redirectMatcher
,您必须提供ObjectPostProcessor
,请参阅this issue。
ObjectPostProcessor<Saml2WebSsoAuthenticationRequestFilter> p = new ObjectPostProcessor<>()
@Override
public <O extends Saml2WebSsoAuthenticationRequestFilter> O postProcess(O filter)
filter.setRedirectMatcher(new AntPathRequestMatcher("/my/custom/url"));
return filter;
;
http.saml2Login().addObjectPostProcessor(processor);
查看SAML 2.0 Login Overview 了解有关流程的更多详细信息。
【讨论】:
以上是关于更改 Spring Security SAML2 登录的 URL的主要内容,如果未能解决你的问题,请参考以下文章
Spring Security SAML2 未找到 entityID
Spring Security SAML2 服务提供者 - RelyingPartyRegistration.entityId() vs RelyingPartyRegistration.assert
配置 ADFS 3.0 / SAML 2.0 以使用 Spring Security 进行 SSO 集成