我已经完成了文件中列出的步骤-
https://developer.okta.com/blog/2017/03/16/spring-boot-saml#run-the-app-and-login-with-okta
一切都很好,我看到SAML响应正在从OKTA的应用程序中生成和编辑,但是当请求到达应用程序时,我得到了这个错误-
type=Forbidden,status=403)。在请求参数'_csrf‘或标头’X令牌‘上发现无效的CSRF令牌'null’。
我试过禁用csrf,但随后它会以无限循环的SAML重定向。
这是SecurityConfigur.java
package com.example;
import static org.springframework.security.extensions.saml2.config.SAMLConfigurer.saml;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Value("${security.saml2.metadata-url}")
String metadataUrl;
@Value("${server.ssl.key-alias}")
String keyAlias;
@Value("${server.ssl.key-store-password}")
String password;
@Value("${server.port}")
String port;
@Value("${server.ssl.key-store}")
String keyStoreFilePath;
@Override
protected void configure(final HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/saml*").permitAll()
.anyRequest().authenticated()
.and()
.apply(saml())
.serviceProvider()
.keyStore()
.storeFilePath("saml/keystore.jks")
.password(this.password)
.keyname(this.keyAlias)
.keyPassword(this.password)
.and()
.protocol("https")
.hostname(String.format("%s:%s", "10.200.10.10", this.port))
.basePath("/")
.and()
.identityProvider()
.metadataFilePath(this.metadataUrl);
}
}如有任何建议,将不胜感激。
发布于 2017-10-12 15:21:52
我在您的代码与我的博客文章中看到的唯一不同之处是下面一行:
.hostname(String.format("%s:%s", "10.200.10.10", this.port))如果您将其更改为以下内容,事情会正常吗?
.hostname(String.format("%s:%s", "localhost", this.port))发布于 2017-10-12 19:17:56
这个问题已经解决了。我做了几件事-
在OKTA中添加了目标url,与URL- https://localhost:8443/saml/SSO上的单个符号相同。
另外,在春季方面,我已经在安全配置中禁用了CSRF保护-
http.csrf().disable();
但实际上,这个问题是错误的目的地url。
发布于 2019-08-13 06:13:20
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**").authorizeRequests().antMatchers("/saml").permitAll()
.anyRequest().authenticated().and().csrf().csrfTokenRepository(getCsrfTokenRepository());
}
private CsrfTokenRepository getCsrfTokenRepository() {
CookieCsrfTokenRepository tokenRepository = CookieCsrfTokenRepository.withHttpOnlyFalse();
tokenRepository.setCookiePath("/");
return tokenRepository;
}添加CSRF曲奇。在前端,如果您使用的是角,只需导入HttpClientXsrfModule。这将获取cookie值并设置请求报头XSRF令牌报头。
@注意: saml登录的配置仍然相同。上面的代码显示了如何添加csrf令牌。
https://stackoverflow.com/questions/46712155
复制相似问题