我正在尝试使用spring-security和angular实现基于saml的sso身份验证。这里有一个很好的例子, https://www.sylvainlemoine.com/2018/03/29/spring-security-saml2.0-websso-with-angular-client/,我将其用作项目的基础。我有一个运行在8090上的Forgerock OpenAM,它是我的IDP。我有一个与示例中提到的设置类似的设置。
第一次获取令牌的调用失败,并显示401,因此我的SPA成功重定向到IDP,并且在成功进行身份验证之后,我又重定向回了我的SPA(通过中继状态),但是一旦返回,它就会尝试为了再次获得令牌,我再次获得了401,这是循环发生的。即使我已成功通过身份验证,我也无法弄清楚为什么会得到401。
我对SSO和saml还是很陌生,不知道可能是什么问题。我无法发布整个代码,但这是SecurityConfig类:
//Credits: Sylvain Lemoine (https://www.sylvainlemoine.com/2018/03/29/spring-security-saml2.0-websso-with-angular-client/)
@Configuration
@EnableWebSecurity
public class SecurityConfig {
/**
* Rest security configuration for /api/
*/
@Configuration
@Order(1)
public static class RestApiSecurityConfig extends WebSecurityConfigurerAdapter {
private static final String apiMatcher = "/api/**";
@Override
protected void configure(HttpSecurity http) throws Exception {
http.addFilterBefore(new JwtAuthenticationFilter(apiMatcher, super.authenticationManager()), UsernamePasswordAuthenticationFilter.class);
http.antMatcher(apiMatcher).authorizeRequests()
.anyRequest()
.authenticated();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) {
auth.authenticationProvider(new JwtAuthenticationProvider());
}
}
/**
* Rest security configuration for /api/
*/
@Configuration
@Order(2)
public static class AuthSecurityConfig extends WebSecurityConfigurerAdapter {
private static final String apiMatcher = "/auth/token";
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.exceptionHandling()
.authenticationEntryPoint(new Http401AuthenticationEntryPoint("SAML2.0 - WEBSSO"));
http.antMatcher(apiMatcher).authorizeRequests()
.anyRequest().authenticated();
}
}
/**
* Saml security config
*/
@Configuration
@Import(SamlSecurityConfig.class)
public static class SamlConfig {
}
}