【发布时间】:2022-11-27 01:13:32
【问题描述】:
我正在尝试在没有 websecurityconfigureradapter 的情况下更新我的应用程序,我在这里需要帮助。 saml 身份验证因以下代码而失败,任何人都可以帮我解决这个问题
【问题讨论】:
标签: spring
我正在尝试在没有 websecurityconfigureradapter 的情况下更新我的应用程序,我在这里需要帮助。 saml 身份验证因以下代码而失败,任何人都可以帮我解决这个问题
【问题讨论】:
标签: spring
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Value("${saml.sp}")
private String samlAudience;
@Autowired
@Qualifier("saml")
private SavedRequestAwareAuthenticationSuccessHandler samlAuthSuccessHandler;
@Autowired
@Qualifier("saml")
private SimpleUrlAuthenticationFailureHandler samlAuthFailureHandler;
@Autowired
private SAMLEntryPoint samlEntryPoint;
@Autowired
private SAMLLogoutFilter samlLogoutFilter;
@Autowired
private SAMLLogoutProcessingFilter samlLogoutProcessingFilter;
private HealthxAuthProvider authProvider = new HealthxAuthProvider();
@Autowired
private SAMLAuthenticationProvider samlAuthenticationProvider;
@Autowired
private ExtendedMetadata extendedMetadata;
@Autowired
private KeyManager keyManager;
@Bean
SAMLDiscovery samlDiscovery() {
return new SAMLDiscovery();
}
// @Bean
// AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
// return authenticationConfiguration.getAuthenticationManager();
// }
public MetadataGenerator metadataGenerator() {
MetadataGenerator metadataGenerator = new MetadataGenerator();
metadataGenerator.setEntityId(samlAudience);
metadataGenerator.setExtendedMetadata(extendedMetadata);
metadataGenerator.setIncludeDiscoveryExtension(false);
metadataGenerator.setKeyManager(keyManager);
return metadataGenerator;
}
@Bean
SAMLProcessingFilter samlWebSSOProcessingFilter(
SavedRequestAwareAuthenticationSuccessHandler samlAuthSuccessHandler,
SimpleUrlAuthenticationFailureHandler samlAuthFailureHandler) {
SAMLProcessingFilter samlWebSSOProcessingFilter = new SAMLProcessingFilter();
samlWebSSOProcessingFilter.setAuthenticationManager(authenticationManagerBean());
samlWebSSOProcessingFilter.setAuthenticationSuccessHandler(samlAuthSuccessHandler);
samlWebSSOProcessingFilter.setAuthenticationFailureHandler(samlAuthFailureHandler);
return samlWebSSOProcessingFilter;
}
@Bean
FilterChainProxy samlFilter(SavedRequestAwareAuthenticationSuccessHandler samlAuthSuccessHandler,
SimpleUrlAuthenticationFailureHandler samlAuthFailureHandler, SAMLLogoutFilter samlLogoutFilter,
SAMLLogoutProcessingFilter samlLogoutProcessingFilter) {
List<SecurityFilterChain> chains = new ArrayList<>();
chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/SSO/**"),
samlWebSSOProcessingFilter(samlAuthSuccessHandler, samlAuthFailureHandler)));
chains.add(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/saml/discovery/**"), samlDiscovery()));
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/SingleLogout/**"),
samlLogoutProcessingFilter));
return new FilterChainProxy(chains);
}
@Bean
AuthenticationManager authenticationManagerBean() {
return new ProviderManager(Collections.singletonList((AuthenticationProvider) authProvider));
}
@Bean
MetadataGeneratorFilter metadataGeneratorFilter() {
return new MetadataGeneratorFilter(metadataGenerator());
}
@Bean
SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
httpSecurity.addFilterBefore(metadataGeneratorFilter(), ChannelProcessingFilter.class)
.addFilterAfter(samlFilter(samlAuthSuccessHandler, samlAuthFailureHandler, samlLogoutFilter,
samlLogoutProcessingFilter), BasicAuthenticationFilter.class)
.addFilterBefore(samlFilter(samlAuthSuccessHandler, samlAuthFailureHandler, samlLogoutFilter,
samlLogoutProcessingFilter), CsrfFilter.class);
/** Add this line in below for local host access- 'httpSecurity.cors().and().csrf().disable().authorizeRequests()' */
httpSecurity.cors().and().csrf().disable().authorizeRequests()
.antMatchers("https://idp-dev.nutanix.com/**", "https://nutanixdev.oktapreview.com/**",
"https://nutanix.okta.com/**", "/login/oauth2/**", "/logout", "/assets/**", "/static/**", "/",
"/index.html", "/css/**", "/js/**", "/images/**", "/report.html", "/**/favicon.png",
"sockjs-node/**", "/page/**", "/hx/**", "/mock/mockauth/**", "/**/sockjs-node/***", "/user/**",
"/dashboard/**", "/documenter/**", "/ui/**", "/export/**", "/qbrpreference/**",
"/generateqbr/**", "https://tableau.nutanix.com/**")
.permitAll().anyRequest().authenticated();
//httpSecurity.requiresChannel().anyRequest().requiresSecure();
//httpSecurity.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
httpSecurity.httpBasic().authenticationEntryPoint(samlEntryPoint);
return httpSecurity.build();
}
/** Uncomment this line in case if you are working with localhost 3000 port and 130 line add and().csrf().disable() and comment 138 line */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("*")
.allowedOriginPatterns("*").allowCredentials(true);
}
}
}
需要一个解决方案
【讨论】: