1563-API -issue使用Spring的安全SAML与CSRF

问题描述:

我已经通文档中列出的步骤 -1563-API -issue使用Spring的安全SAML与CSRF

https://developer.okta.com/blog/2017/03/16/spring-boot-saml#run-the-app-and-login-with-okta

一切正常,我看到SAML响应如何产生和reditection发生在应用程序来自OKTA,但当请求到达应用程序时,出现此错误 -

type = Forbidden,status = 403)。在 请求参数'_csrf'或头部'X-CSRF-TOKEN'上找到无效的CSRF令牌'null'。

我已尝试禁用csrf,但随后它与SAML重定向进行无限循环。

这里的SecurityConfiguration.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); 
    } 
} 

任何建议表示赞赏。

我在你的代码中看到对我的博客文章的唯一区别是以下行:

.hostname(String.format("%s:%s", "10.200.10.10", this.port)) 

做的事情工作,如果你将其更改为以下?

.hostname(String.format("%s:%s", "localhost", this.port)) 
+0

为什么url需要附加/ saml/sso只?当我改变网址让我们说/ saml/xyz。我得到403错误 – theLearner

+0

因为这个值被硬编码到'SAMLConfigurer'类的'samlFilter'中:https://github.com/spring-projects/spring-security-saml-dsl/blob/master/spring-security- SAML的DSL/src目录/主/ JAVA /组织/ springframework的/安全/扩展/ SAML2 /配置/ SAMLConfigurer.java#L265 –

这个问题解决。我做了几个东西 -

在1563加入目标网址相同的网址 - 单点登录https://localhost:8443/saml/SSO

此外,在春天的一面,我有残疾CSRF保护在配置中 -

http.csrf()。disable();

但真的这个问题是错误的目标网址。